How to Send WordPress Email Using SMTP Server

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

You can solve this problem by using the SMTP server. Usually, web hosting companies provide their own SMTP server which you can use for sending your website emails.

Alternatively, you can go for other SMTP servers like Gmail, Mailjet, etc. All it requires basic details about the SMTP server like Host, Port, etc.

That being said, let’s see how to use the SMTP server for sending WordPress emails.

PHPMailer in WordPress

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

If we configure PHPMailer in WordPress, wp_mail function sends emails through this PHPMailer class. In that case, the wp_mail method does not depend on the hosting settings. Instead, they use your SMTP server settings.

I am going to use this PHPMailer class for the emails. As PHPMailer is available in WordPress core, we don’t need to install the PHPMailer library separately.

One can use the WP Mail SMTP plugin which also sends emails using SMTP servers. But I always recommend if something is achievable by writing a little piece of code then go for it. It is a good practice to use fewer plugins as possible. Using more plugins on the website increases the extra load on the server. And sometimes plugins get conflicted with each other which can end up in a broken site.

Send WordPress Email through SMTP Server

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

If you planned to use the Gmail SMTP server then you need to change some settings on your Google account. For this, log in to your Google account and click on the ‘Account’. Once you are on the ‘Account’ page click on ‘Security’. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.

less-secure-apps

To configure PHPMailer in WordPress, there is a hook available which is phpmailer_init. Using phpmailer_init hook, we can access the PHPMailer object and set the arguments to it.

Open your active theme’s functions.php file and place the below code at the end of a file.

add_action( 'phpmailer_init', 'set_phpmailer_details' );
function set_phpmailer_details( $phpmailer ) {
    $phpmailer->isSMTP();     
    $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
}

Make sure to replace the placeholders with the actual values. Let’s say you are using Gmail SMTP server then your code will be written as follows:

add_action( 'phpmailer_init', 'set_phpmailer_details' );
function set_phpmailer_details( $phpmailer ) {
    $phpmailer->isSMTP();     
    $phpmailer->Host = 'smtp.googlemail.com'; //gmail smtp host
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 465;
    $phpmailer->Username = 'GMAIL_USERNAME';
    $phpmailer->Password = 'GMAIL_PASSWORD';
    $phpmailer->SMTPSecure = 'ssl';
}

You need to pass the actual username and password of your Gmail account in the above code. Finally, you have to set the ‘From’ address for your outgoing emails. You can do so as follows.

function replace_user_mail_from( $from_email ) {
	return 'FROM_EMAIL_ADDRESS';
}
add_filter( 'wp_mail_from', 'replace_user_mail_from' );

Now try to send email from your WordPress website, email should start working.

Debug the Email Issue

At this moment, even after using PHPMailer your emails are not working then using the wp_mail_failed action you can get the cause of the problem. Use the below code to debug the issue.

add_action('wp_mail_failed', 'log_mailer_errors', 10, 1);
function log_mailer_errors( $wp_error ){
    $fn = ABSPATH . '/mail.log'; // say you've got a mail.log file in your server root
    $fp = fopen($fn, 'a');
    fputs($fp, "Mailer Error: " . $wp_error->get_error_message() ."\n");
    fclose($fp);
}

I hope you understand how to send WordPress emails using the SMTP server. Please share your thoughts and suggestions in the comment section below.

Related Articles

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

4 thoughts on “How to Send WordPress Email Using SMTP Server

  1. May be failing because certificates not setup properly on WP server. Temporary workaround:

    $phpmailer->SMTPOptions = array(
    ‘ssl’ => array(
    ‘verify_peer’ => false,
    ‘verify_peer_name’ => false,
    ‘allow_self_signed’ => true
    )
    );

  2. had some error in customized code.
    thx to mentioning wp includes php mailer and the path :
    require ‘wp-includes/class-phpmailer.php’;
    saved me some time

    THX

  3. Hi Sir, I am dinesh patil. I am doing practice on wordpress for making websites. one day i had stuck at contact form because my submitted data was not send to respected email on gmail. That time I have read your article i.e. https://artisansweb.net/how-to-send-wordpress-email-using-smtp-server/
    I have applied this to my website and really works. Thank you for that help. My question is that can we hide our gmail password which we have to mention in function.php file in phpmailer function..? (As given in your above article……i,e, $phpmailer->Password = ‘your gmail password’; ). Because gmail password is very important. So if any solution is their for this query please reply me on my gmail account.

Leave a Reply

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