Sending email with PHPMailer and Gmail
PHP | Technical | Me | December 6, 2010
Tonight a friend asked me to help him to send an email with PHP as soon as possible. Right now I utilize SwiftMailer but the fastest way was with PHPMailer. Almost two years ago I utilized this mail library. I've got the code in a project but I delete it 3 months ago ... Yes, yes, I know, I should not have done that. After an hour trying to send the email I promised myself not to pass for this situation again in the future, then here are the steps to send an email with PHPMailer using a gmail account.
Download PHPMailer
Here is the link to download PHPMailer.
Code mail.php
Here it is:
require("class.phpmailer.php"); $mailer = new PHPMailer(); $mailer->IsSMTP(); $mailer->SMTPAuth = TRUE; $mailer->SMTPSecure = "ssl"; $mailer->Host = "smtp.gmail.com"; $mailer->Port = 465; $mailer->Username = "johan@gmail.com"; // Change this to your gmail adress $mailer->Password = "**********"; // Change this to your gmail password $mailer->From = "johan@gmail.com"; // This HAVE TO be your gmail adress $mailer->FromName = "Works!!"; // This is the from name in the email, you can put anything you like here $mailer->Body = "Works!!"; $mailer->Subject = "This works!!!"; $mailer->AddAddress("jonathan@hotmail.com", "Jonathan"); // This is where you put the email adress of the person you want to mail $mailer->AddAddress("karla@hotmail.com", "Karla"); $mailer->AddAddress("humberto@hotmail.com", "Sano"); if(!$mailer->Send()) { echo "Message was not sent"; echo "Mailer Error: " . $mailer->ErrorInfo; } else { echo "Message has been sent"; }
Modify php.ini
This was the step that took me 40 mins... yeah a line of code as always ... the questions were where? and why?
Where? in php.ini (that uses the Apache)
Why? to correctly authenticate your account.
Then, in your php.ini you must uncomment the following line:
extension=php_openssl.dll
Then save the file and restart the server.
Now browse: http://localhost/mail.php
And voilà, the email was sent to the mail addresses jonathan@hotmail.com, karla@hotmail.com and humberto@hotmail.com.