Hi,
I know you would have used mail() in PHP, then you should also know limitations of it.
Setting/configuring your outlook,mercury,thunderbird to use mail() is an hurdle and it doesn't
provide much flexibiity too.
Do you know there are many libraries to send mail? indeed power full libraries like
phpmailer, swiftmailer.
These classes or libraries have been written to make sending mail using PHP easier
and send mail with more flexibility and features.
In this demo am going to use phpmailer class as swiftmailer is also good but I prefer and
recommend phpmailer because swiftmailer class has not been updated recently and where
phpmailer has good documentation and new releases are always on the way.
Lets start now :)
First download phpmailer from here .
Extract it to your local host directory (ex: htdocs in windows xampp).
Now you have a directory called phpmailer in your web directory.
Now write a php script, first include phpmailer class.
require_once './phpmailer/class.phpmailer.php';
Now instantiate the phpmailer class.
$mail = new PHPMailer(true);
Now $mail contains instance of PHPMailer class now you can configure all the properties
of it.
$mail->CharSet = 'utf-8'; //sets character set
$to = 'wetechies.2013@gmail.com'; //from whom the mail should be sent
$mail->IsSMTP(); //To inform that we are using SMTP
$mail->SMTPDebug = 2; //Shows long message set to 1 to avoid it
$mail->Host = "smtp.gmail.com"; // As we are using gmail SMTP
$mail->Port = "465"; // smtp port number
$mail->SMTPSecure = "ssl"; //gmail requires authentication through ssl or tls
$mail->SMTPAuth = true;
// your account details for authentication provide correct user name and password
$mail->Username = "wetechies.2013@gmail.com";
$mail->Password = "mypassword";
$mail->AddReplyTo("wetechies.2013@gmail.com", "nag");
$mail->From = "wetechies.2013@gmail.com";
$mail->FromName = "wetechies blog";
$mail->AddAddress("wetechies.2013@gmail.com", "nagashayan");
$mail->Subject = "hi (PHPMailer test using SMTP)";
$body="Sending mail by using wetechies post on How to use Gmail SMTP to send mail \n";
$mail->WordWrap = 80;
$mail->MsgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images
//you can also add attachments to your mail but change file path
//$mail->AddAttachment('images/phpmailer_mini.gif', 'phpmailer_mini.gif'); // optional name
//$mail->AddAttachment('images/phpmailer.png', 'phpmailer.png'); // optional name
//call send() to send mail
$mail->Send();
PS: The above example sends mail from wetechies.2013@gmail.com to wetechies.2013@gmail.com itself.
To send to other change $mail->to =" to_whom_you have to send";
If you don't want to take hurdle of writing script don't worry phpmailer helps you in that too.
What? Yes! it generates code for you!!! Amazing right
Once you download phpmailer class go to phpmailer/examples/ check for codegenerator.phps
Change .phps extension to .php run that file, fill out all the details
Now fill up your google account details
SMTP Port - 465
SMTP Server - smtp.gmail.com
SMTP Security - ssl
SMTP Authentication check yes
Provide your username and password of google and click submit.
that's it!!! you are done it gives you the PHP script if authentication is successfull.
you can just run that code and send send the mail.
the generated script just include that in your PHP file :)
You are done, Now send mail to anyone with n no of attachments happily :)
Please leave your doubts or comments below :)
Have you ever thought how to send mail in PHP with out using mail() ?
I know you would have used mail() in PHP, then you should also know limitations of it.
Setting/configuring your outlook,mercury,thunderbird to use mail() is an hurdle and it doesn't
provide much flexibiity too.
Do you know there are many libraries to send mail? indeed power full libraries like
phpmailer, swiftmailer.
These classes or libraries have been written to make sending mail using PHP easier
and send mail with more flexibility and features.
In this demo am going to use phpmailer class as swiftmailer is also good but I prefer and
recommend phpmailer because swiftmailer class has not been updated recently and where
phpmailer has good documentation and new releases are always on the way.
Lets start now :)
First download phpmailer from here .
Extract it to your local host directory (ex: htdocs in windows xampp).
Now you have a directory called phpmailer in your web directory.
Now write a php script, first include phpmailer class.
require_once './phpmailer/class.phpmailer.php';
Now instantiate the phpmailer class.
$mail = new PHPMailer(true);
Now $mail contains instance of PHPMailer class now you can configure all the properties
of it.
$mail->CharSet = 'utf-8'; //sets character set
$to = 'wetechies.2013@gmail.com'; //from whom the mail should be sent
$mail->IsSMTP(); //To inform that we are using SMTP
$mail->SMTPDebug = 2; //Shows long message set to 1 to avoid it
$mail->Host = "smtp.gmail.com"; // As we are using gmail SMTP
$mail->Port = "465"; // smtp port number
$mail->SMTPSecure = "ssl"; //gmail requires authentication through ssl or tls
$mail->SMTPAuth = true;
// your account details for authentication provide correct user name and password
$mail->Username = "wetechies.2013@gmail.com";
$mail->Password = "mypassword";
$mail->AddReplyTo("wetechies.2013@gmail.com", "nag");
$mail->From = "wetechies.2013@gmail.com";
$mail->FromName = "wetechies blog";
$mail->AddAddress("wetechies.2013@gmail.com", "nagashayan");
$mail->Subject = "hi (PHPMailer test using SMTP)";
$body="Sending mail by using wetechies post on How to use Gmail SMTP to send mail \n";
$mail->WordWrap = 80;
$mail->MsgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images
//you can also add attachments to your mail but change file path
//$mail->AddAttachment('images/phpmailer_mini.gif', 'phpmailer_mini.gif'); // optional name
//$mail->AddAttachment('images/phpmailer.png', 'phpmailer.png'); // optional name
//call send() to send mail
$mail->Send();
PS: The above example sends mail from wetechies.2013@gmail.com to wetechies.2013@gmail.com itself.
To send to other change $mail->to =" to_whom_you have to send";
If you don't want to take hurdle of writing script don't worry phpmailer helps you in that too.
What? Yes! it generates code for you!!! Amazing right
Once you download phpmailer class go to phpmailer/examples/ check for codegenerator.phps
Change .phps extension to .php run that file, fill out all the details
Now fill up your google account details
SMTP Port - 465
SMTP Server - smtp.gmail.com
SMTP Security - ssl
SMTP Authentication check yes
Provide your username and password of google and click submit.
that's it!!! you are done it gives you the PHP script if authentication is successfull.
you can just run that code and send send the mail.
the generated script just include that in your PHP file :)
You are done, Now send mail to anyone with n no of attachments happily :)
Please leave your doubts or comments below :)
Its nice blog!!But its not working me its giving error as SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl"
ReplyDeleteplease help
Hi, shweta have you provided correct username password of your google account? and also check changing ssl with tls and you are trying on your system or using any webhosting services?
ReplyDeleteNice post.Thank you so much for sharing.Yiioverflow is a web development company.We have well expert team in Angular JS, Ionic, Yii Framework, Node JS, Laravel, PHP, MySQL, and WordPress.I you want a developer visit.. https://yiioverflow.com/
ReplyDeleteWonderful post!!Thank you for sharing this info with us.
ReplyDeleteKeep updating I would like to know more updates on this topic
Very useful content, I would like to suggest this blog to my friends.
PHP Training
php course
Hope this helps. Thanks for visiting/posting!
ReplyDeletePHP Training in Chennai
php course
Thank you for the link building list.I am going jot down this because it will help me a lot.Great blog! Please keep on posting such blog.
ReplyDeleteprivate label website builder
ReplyDeleteReally awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.PHP Developer Software in India
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
tiktok jeton hilesi
tiktok jeton hilesi
saç ekimi antalya
referans kimliği nedir
İnstagram takipçi satın al
METİN PVP
İnstagram takipçi
perde modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
ANKARA EVDEN EVE NAKLİYAT
Trafik Sigortasi
DEDEKTÖR
web sitesi kurma
Aşk Kitapları
Success Write content success. Thanks.
ReplyDeletecanlı poker siteleri
kralbet
betturkey
kıbrıs bahis siteleri
betmatik
deneme bonusu
canlı slot siteleri
Good content. You write beautiful things.
ReplyDeletevbet
sportsbet
korsan taksi
mrbahis
taksi
hacklink
mrbahis
vbet
sportsbet
artvin
ReplyDeletebitlis
niğde
hatay
tunceli
1G38L
https://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
Mİ06
شركة تنظيف منازل بجازان bezDJe8bdO
ReplyDelete