If you have followed our guide on How to Easily Set Up Email with SMTP on Ubuntu Server and want WordPress to send you email then you need to perform some extra steps. Make sure that you have completely followed our previous guide. Do not worry, this part is quite easy.
Check Whether PHP Can Send Emails Using mSMTP
We need to test whether the php mail()
function is working or not. Create a php
script like the one below on your server, edit the required fields and save it as mail.php
Execute this script:
---
1 | php mail.php |
You should be able to receive an email. If you do not receive any email, check our guide How to Easily Set Up Email with SMTP on Ubuntu Server again and your email credentials. Also, check the log at /var/log/msmtp/msmtp.log
.
Configure WordPress SMTP Settings to Send Emails Using mSMTP
I have created a small WordPress plugin for you with this content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php /** * Plugin Name: msmtp mail plugin * Plugin URI: https://thecustomizewindows.com * Description: Helps WordPress to send email using msmtp configuration on server * Author: Abhishek_Ghosh * Author URI: https://thecustomizewindows.com * Version: 1.0 */ /** * This function will connect wp_mail to your authenticated mSMTP server. */ add_action( 'phpmailer_init', 'send_smtp_email' ); function send_smtp_email( $phpmailer ) { if ( ! is_object( $phpmailer ) ) { $phpmailer = (object) $phpmailer; } $phpmailer->Mailer = 'smtp'; $phpmailer->Host = SMTP_HOST; $phpmailer->SMTPAuth = SMTP_AUTH; $phpmailer->Port = SMTP_PORT; $phpmailer->Username = SMTP_USER; $phpmailer->Password = SMTP_PASS; $phpmailer->SMTPSecure = SMTP_SECURE; $phpmailer->From = SMTP_FROM; $phpmailer->FromName = SMTP_NAME; } |
You will get the plugin here on my GitHub repository. Simply copy this php file as a new file under /wp-content/plugins/
directory. Name the file as msmtp-mail-plugin.php
.
Activate the plugin from WordPress Admin.
Open wp-config.php
and add these constants before the line which says “That’s all, stop editing! Happy blogging”:
1 2 3 4 5 6 7 8 9 10 11 12 13 | // add these before the line which says /* That's all, stop editing! Happy blogging. */ define( 'SMTP_HOST', 'smtp.office365.com' ); define( 'SMTP_PORT', '587' ); define( 'SMTP_SECURE', 'tls' ); define( 'SMTP_AUTH', true ); define( 'SMTP_USER', 'username@hotmail.com' ); define( 'SMTP_PASS', 'your-password' ); define( 'SMTP_FROM', 'username@hotmail.com' ); define( 'SMTP_NAME', 'microsoft' ); /** Absolute path to the WordPress directory. */ |
Edit it with real value. This is a working example with our website’s setup:
Now your WordPress will be able to send email.