Using Gmail SMTP to send emails with WordPress wp_mail function

1 August, 2014   |   PHP, WordPress

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 = 'username@gmail.com';
	$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   = 'usernam@gmail.com';
 $Mailer->Password   = 'password';
 
 $Mailer->From       = 'replyto@domain.com';
 $Mailer->FromName   = 'First Last';
 $Mailer->Subject    = 'Subject';
 $Mailer->AltBody    = 'Plain text';
 
 $Mailer->MsgHTML('HTML text');
 
 $Mailer->AddAddress('user@domain.com', '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.

 

Discussion

Rizwan 15 April, 2015

Many Thanks… Good Help

Eugen Mihailescu 24 May, 2015

Thanks for sharing.

Tim 27 October, 2015
Web Design 10 December, 2015

Thanks for posting! :)

Leave a Comment