Sie sind auf Seite 1von 2

Sending e-mails via SMTP with PHPmailer and GMail

Posted by Olaf 55 comments September 1, 2009 These days I tried some plugin to send e-mail message within WordPress via SMTP. Since my domains email is hosted with Google applications I decided to send my messages via the SMTP server from GMail. I found several articles and PHPMailer tutorials, but a lot of them didnt worked for me.

Why using GMail for sending mail messages?


First of all its FREE! Sure most website owners can use their own SMTP server for sending email messages from their website, but it makes sense even than to use GMail for sending mail. The chance is big that your websites IP address is on a blacklist if your site is on hosted by a shared web hosting provider. If not or you host your site on your own server, there is always a risk that your IP address get blacklisted. Because of some limitations, the SMTP server from Google is a good choice applications with less than 500 recipients a day, check this information from the Google help pages.

Requirements
You need for this PHPMailer code example a PHP5 enabled web host (I did tests only on Linux), the port 465 need to be open and of course you need a GMail or Google Apps account. Trouble sending e-mails with your normal GMail account? If you own a domain name, you can register for a Google Apps account or check below the tutorial update about an alternative SMTP service!

PHPMailer tutorial for GMail and Google Apps


1. If you dont have one, register a GMail account or setup your domain for Google applications. 2. Download a recent version of PHPMailer (Im using the version 5.02) 3. Check with your web hosting provider that port 465 (TCP out) is open, if not ask him to open that port 4. Include the PHPMailer class file: require_once('phpmailer/class.phpmailer.php'); 5. Create those two constant variables to store your GMail login and password. Use the login for your Google Apps mail account if you have one.
6. define('GUSER', 'you@gmail.com'); // GMail username define('GPWD', 'password'); // GMail password

7. Use the following function to send the e-mail messages (add the function in one of your included files):
8. function 9. global 10. 11. 12. messages 13. 14. GMail 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. } smtpmailer($to, $from, $from_name, $subject, $body) { $error; $mail = new PHPMailer(); // create a new object $mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = only $mail->SMTPAuth = true; // authentication enabled $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for $mail->Host = 'smtp.gmail.com'; $mail->Port = 465; $mail->Username = GUSER; $mail->Password = GPWD; $mail->SetFrom($from, $from_name); $mail->Subject = $subject; $mail->Body = $body; $mail->AddAddress($to); if(!$mail->Send()) { $error = 'Mail error: '.$mail->ErrorInfo; return false; } else { $error = 'Message sent!'; return true; }

Das könnte Ihnen auch gefallen