How to set up SMTP for office 365 with code for WordPress

To set up SMTP on WordPress using the Office 365 server, you need to modify the default email-sending function and use the Microsoft SMTP server. This allows you to send emails through your corporate Office 365 account, which improves message deliverability and their reliability. In this guide, we’ll look at how to step-by-step configure email sending from WordPress via Office 365.

Step-by-step setup of SMTP for WordPress with Office 365:

1) Register an account in office365

To use this code, you must be registered in office365.

2) Using the hook phpmailer_init

You need to add special code to the functions.php file of your theme or in a must-use plugin. This will allow you to configure SMTP settings for WordPress.

WordPress uses PHPMailer to send emails, and we can change its settings using the phpmailer_init hook.

add_action( 'phpmailer_init', 'smtp_enable_for_office365' );

function smtp_enable_for_office365( $phpmailer ) {
	$phpmailer->isSMTP();                                   // Indicate that this is SMTP
	$phpmailer->SMTPAuth   = true;                          // Indicate that this is SMTP with authentication
	$phpmailer->Host       = 'smtp.office365.com';          // URL, change if needed
	$phpmailer->Port       = '587';                         // Port, change if needed
	$phpmailer->Username   = '[email protected]';    // Login in office365, specify your own
	$phpmailer->Password   = 'pass12345';                   // Password in office365, specify your own
	$phpmailer->SMTPSecure = 'tls';                         // Indicate that this is SMTP in tls mode
	$phpmailer->From       = '[email protected]';               // From, specify your own email
	$phpmailer->FromName   = 'My super site';              // Site name, specify your own
}
  • Security settings: Be sure to use the tls protocol to improve the security of data transmission. Authentication via Username and Password will help you make sure you really have access to the specified account.

  • Security tips: Never store the login and password in plain text in the code. Use environment variables, constants, or options (database) for this.

3) Checking functionality

After adding the code, check that emails are sent through the contact form or an SMTP testing plugin. Make sure the emails are delivered successfully.

Possible errors and their solutions:

  1. Connection Error:

    • Make sure the server supports outgoing connections on port 587.
    • Check the correctness of the login and password for the SMTP server.
  2. Deliverability issues:

    • If emails end up in spam, add SPF and DKIM records at the DNS level.
  3. Incorrect encryption protocol:
    • Make sure you use tls or ssl depending on the settings of your Office 365 account.

Conclusion:

Configuring SMTP for WordPress via Office 365 is a simple and effective way to improve email deliverability and increase their reliability. By following this guide, you can easily integrate your WordPress site with the corporate Office 365 mail and ensure consistent email sending.

Note embeded into: phpmailer_init