How To Send WordPress Email Using SMTP Server

Are you facing problems of sending WordPress emails? WordPress email sends using the PHP’s mail() function. Sometimes, your web hosting does not configure mail settings correctly. As a result, no emails can be sent from your website.

We can solve this problem by using SMTP server to send our WordPress emails. Some web hosting allows creating an email address with the domain. We use Bluehost and Bluehost allow us to create email like [email protected]. We can use these emails as SMTP server for sending our emails from WordPress website.

It depends on you actually, whether to use hosting SMTP server or most popular Gmail SMTP server.

All we need is some details about SMTP server which we will see in a moment.

That being said, let’s study how to use SMTP server for sending WordPress email.

WordPress PHPMailer

WordPress includes PHPMailer class in its core. If you want to check it, you will find this file under wp-includes/class-phpmailer.php.

WordPress wp_mail function uses this PHPMailer class to send email through PHP’s mail function. It means, WordPress uses SMTP server if set for sending email. And if it is not found then go for default hosting settings for sending emails.

We will also use the same class for sending our emails. As it is included already, we don’t need to install PHPMailer library separately.

One can use the plugin WP Mail SMTP which also send emails using SMTP servers. But we always recommend if something is achievable by writing a little piece of code then go for it. It is a good practice to use as much fewer plugins on your WordPress website. By using more plugins on the website also increases the extra load on the server. And sometimes plugin gets conflicted with each other which results in site break.

Send WordPress Email Through SMTP Server

Before proceeding, you should have ready with the details like SMTP host, SMTP port, SMTP username and SMTP password.

If you planned to use Gmail SMTP host then you need to change some settings on our Google account. Login to your Google account and click on My Account. Once you are on My Account page then click on Sign-in & Security.

Sign In Security Gmail

On the next page, scroll down to the bottom and you will find ‘Allow less secure apps’ settings. Set it to ON.

Less Secure Apps Gmail

WordPress provides a hook phpmailer_init. Using phpmailer_init we can hook into the PHPMailer object and pass our own arguments.

Now, open your active theme’s functions.php file and place the below code in it at the end of a file.

add_action( 'phpmailer_init', 'set_phpmailer_details' );
function set_phpmailer_details( $phpmailer ) {
    $phpmailer->Host = 'YOUR_SMTP HOST';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 'SMTP_PORT'; //25 or 465
    $phpmailer->Username = 'SMTP_USERNAME';
	$phpmailer->Password = 'SMTP_PASSWORD';
	$phpmailer->SMTPSecure = 'ssl'; //ssl or tls
	$phpmailer->From = get_option('admin_email');
    $phpmailer->FromName = "FROM_NAME";

Make sure to replace the placeholders with the actual values. Let’s say we are using Gmail SMTP mail server so our code will look as below:

add_action( 'phpmailer_init', 'set_phpmailer_details' );
function set_phpmailer_details( $phpmailer ) {
    $phpmailer->Host = ''; //gmail smtp host
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 465;
    $phpmailer->Username = 'GMAIL_USERNAME';
	$phpmailer->Password = 'GMAIL_PASSWORD';
	$phpmailer->SMTPSecure = 'ssl';
	$phpmailer->From = get_option('admin_email');
    $phpmailer->FromName = "Artisans Web";

You need to pass your actual username and password of Gmail account in the above code.

We hope you understand how to send WordPress email Using SMTP server. We would like to hear your thoughts in the comment section below.

Related Articles

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Recommended Tutorials For You

Leave a Reply

Your email address will not be published. Required fields are marked *