Tuesday, October 11, 2011

Using PHPMailer and Gmail to send email

If you are developing applications using PHP and you need to send email you can use the PHPMailer() class in PHP. Using a publicly available SMTP server to send the email is much easier than trying to setup your own email server. The following code snippet shows the various settings for the mailer.
The code assumes that you have PHP 5.x version and you have class.phpmailer.php file in the include directory.
Google uses ssl for the smtp connection. In order for this example to work with google smtp server, you need to enable ssl in your php.ini file by adding a line that says extension=php_openssl.dll

If you are not sure of the exact location of the php.ini file and you are using xampp, you can find the location of the php.ini file by navigating to http://localhost/xampp/phpinfo.php on your browser and look for the text "Loaded Configuration File". Once you find the file, edit it and look for the text "extension=php_openssl.dll". If the text is not found in your file, add a new line at the end of the file with the above text.
IsSMTP();
$mail->SMTPDebug = 1; // 1 tells it to display SMTP errors and messages, 0 turns off all errors and messages, 2 prints messages only.

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication

$mail->Username = 'user@gmail.com'; // replace this with your email acct
$mail->Password = 'userPassword'; // replace this with your password

$mail->From = 'jmeslie@gmail.com';
$mail->FromName = 'Jean Meslie';
$mail->AddAddress('receipient@yahoo.com', 'Receiver');
$mail->AddReplyTo('user@gmail.com'); // Adds a “Reply-to' address. Un-comment this to use it.
$mail->Subject = 'test message';
$mail->Body = 'message body goes here. This message was sent at '. time();

if ($mail->Send() == true) {
echo 'The message has been sent at '. time();
}
else {
echo 'The email message has NOT been sent for some reason. Please try again later.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
?>

10 comments:

hektorhamelton said...

Thanks to the author for writing the post, it was quite necessary for me and liked it. I wrote a note on the ukwritings.com review about this. I will be happy if you read it and accept it. Thank you for your concern.

Tom said...

That is very cool, idea! I love it! For me It's kind of hardunderstandig stuff but, you are doing well! If you need some help in future for the essay or homework, you can ask for help this guys! They really know how to do it! Just text to them paper writing services and everything gonna be cool! Enjoy!

Elizabeth Johanson said...

Assignment writing services are not just available in any specific region but it has now spread to every corner of the modern globe. It's reasonable because every student wants to purchase or rent an academic writing service & we are here with our Masters dissertation proposal services UK, to help them any way we can.

shopify said...

Meta faces billion pound class action case They choose our services because we deliver value for them and they have meaningful control of what information they share on Meta’s platforms and with

Agata Barnes said...

Hey guys. Looking for an artist to write my theme, I found the authors site, and I really liked it. Responsible guys know their job master papers reviews Tasks are completed on time. All at the highest level. I am very satisfied, and if necessary, always only to them. Cool, I recommend.

Unknown said...

How to Play Casino: Easy Guide to playing slots on
Casino games are played by 4 players, air jordan 18 retro from us the average time 해외 토토 사이트 넷마블 they take turns 넷마블 토토 넷마블 is around 14:20. The house is divided into jordan 18 white royal blue to us three distinct categories: the house air jordan 18 retro men red shop

Mia Wilson said...

I didn't have time to write a history essay. A college friend recommended these guys college essay service he responded very well. The application was accepted immediately, all wishes on the topic were taken into account. The work was done on time. The teacher accepted the first time. I am very pleased. I am satisfied, I recommend.

William Woodruff said...

I am developing an application using PHP and am stuck on a point. Can you guide me on whether I should use PHPMailer to send email in the contact form, or Gmail option will be best? https://www.theacademicpapers.co.uk/blog/2022/05/13/what-is-sample-study-in-research-purpose-and-techniques/

whome2249 said...

A lot of things in my life have already happened at school, I was lucky that then this site https://essaysrescue.com/bid4papers-review/ was recommended to me, on which I now often sit, and my grades have become much better, any written work will be written

phil123321 said...

Nice post, https://paperwritingservices.reviews/advanced-writers-review/