Are you facing problems with sending emails in WordPress? Under the hood, WordPress sends emails 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 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 ‘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.
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 the 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, if 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
- Send Email Using Gmail SMTP Server from PHP Script
- Sending Email Via Gmail SMTP Server in Laravel
- Send Email Using Mailjet in PHP
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
This no longer worked for me- though I think it could be an issue with the specific template we’re using as email broke entirely after modifying the template functions.php.
My issue was that our office365 email accounts would not receive email from WordPress’s phpMailer wordpress@domain.com method (not even in junk). Never did get it working, however I fixed my problem by sending the email to a mailbox that does accept phpMailer mail, creating a forward on that account to a distribution group I created which contains all of the actual O365 recipients that need to get the mail. Round-about fix but hell it saved some time trying to figure out why my wordpress won’t connect to an SMTP server to send.
Thank you so much for sharing the easiest way I have ever seen to send the email on WordPress. But, recently Google says the feature isn’t available now. So, do you have any alternative option to do this? I am looking forward to hearing from you.
Thanks again.
Could you share links or a resource where it mentioned this service is not available?
As an alternate option, you can use Gmail API(it requires following the OAuth process and generating an access token). I have written articles on this topic for plain PHP.
https://artisansweb.net/how-to-send-email-using-gmail-api-with-phpmailer
https://artisansweb.net/how-to-send-email-using-gmail-api-in-php
It helpd me a lot..Thanks..
You’re welcome, Jayanta.
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
)
);
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
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.
You can build page at the backend and give the two fields to enter username and password. In that way, these details stored in the database. Please refer to the article where we create the form at the backend https://artisansweb.net/create-wordpress-twitter-feed-plugin/