Using Gmail SMTP to send emails with WordPress wp_mail function
By default, WordPress uses PHP mail function to send emails from your website. It uses a pluggable wp_mail function that actually does all the parsing of parameters and then sends email. The class used by wp_mail to send actual email is PHPMailer email transport class that supports sendmail, QMail and SMTP protocol.
Many web hosts disallow sending email through localhost to prevent spamming. Even if they do allow sending email, the delivery is not guaranteed as email sending servers are not certified sources and it usually ends up in SPAM folder or is blocked altogether.
To overcome all these email sending and delivery issues you can use third party SMTP service that sends email on your behalf. Now the sender source is a certified host and so the delivery is quite assured. Gmail allows sending email through SMTP server but only for authenticated users. Therefore, if you have a Gmail account then you can use its SMTP service to send emails using your account credentials.
First, you need to configure WordPress wp_mail function to use SMTP instead of PHP mail function and then set Gmail SMTP parameters to use it as email sending source. Insert the following code to your functions.php file within your current theme and update your Gmail account email and password.
function custom_phpmailer_init($PHPMailer) { $PHPMailer->IsSMTP(); $PHPMailer->SMTPAuth = true; $PHPMailer->SMTPSecure = 'ssl'; $PHPMailer->Host = 'smtp.gmail.com'; $PHPMailer->Port = 465; $PHPMailer->Username = '[email protected]'; $PHPMailer->Password = 'password'; } add_action('phpmailer_init', 'custom_phpmailer_init');
Update: If you have enabled 2 Step Verification then you have to use App Password of your Gmail account.
If you are concerned with plain text password then you can encrypt it with mcrypt_encrypt.
What is does is intercept PHPMailer class and set it to use SMTP instead of mail and add required parameters to the class. Now all email sent using wp_mail are actually being sent through Gmail SMTP server.
If you are not using WordPress and still want to use Gmail for sending emails via web application then you can use PHPMailer class and configure it with SMTP details like following:
<?php include('class.phpmailer.php'); $Mailer = new PHPMailer(); $Mailer->IsSMTP(); $Mailer->SMTPAuth = true; $Mailer->SMTPSecure = 'ssl'; $Mailer->Host = 'smtp.gmail.com'; $Mailer->Port = 465; $Mailer->Username = '[email protected]'; $Mailer->Password = 'password'; $Mailer->From = '[email protected]'; $Mailer->FromName = 'First Last'; $Mailer->Subject = 'Subject'; $Mailer->AltBody = 'Plain text'; $Mailer->MsgHTML('HTML text'); $Mailer->AddAddress('[email protected]', 'First Last'); $Mailer->IsHTML(true); if (!$Mailer->Send()) { print 'Mailer Error: ' . $Mailer->ErrorInfo; } else { print 'Email has been sent'; } ?>
Hope this tutorial is helpful in solving your email sending issues with WordPress hosted on restrictive web hosts.
Many Thanks… Good Help
Thanks for sharing.
Your post and http://php.codeindepth.com/php-sending-mail/ saved me
Thanks for posting! :)
saved my life! Thanks!