WordPress MS365 SMTP Setup
From WikiMLT
Here is a short manual how to setup WordPress 5.x to send emails via SMTP or your organization's Microsoft 356 cloud.
Microsoft 356: Account
For this task you could create a shared mailbox within your Microsoft 356 cloud as it is shown at Figure 1.
WordPress: functions.php
1. Place the following code in your functions.php
file. Or better create an autoloaded plugin, located in wp-content/mu-plugins/
.
user@host:/var/www/your-site/wp-content/mu-plugins $ cat "wp-mail-native-setup.php"
<?php
/**
* WP Mail Native Setup. References:
* - https://butlerblog.com/easy-smtp-email-wordpress-wp_mail/
* - https://gist.github.com/butlerblog/c5c5eae5ace5bdaefb5d
*
* THE_CONSTANTS used below are defined in $IP/wp-config.php
* This function will connect wp_mail to your authenticated
* SMTP server. This improves reliability of wp_mail, and
* avoids many potential problems.
*
* For instructions on the use of this script, see:
* https://butlerblog.com/easy-smtp-email-wordpress-wp_mail/
*
* Values for constants are set in wp-config.php
**/
add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
$phpmailer->isSMTP();
$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;
}
WordPress: wp-config.php
Edit your wp-config.php
file in the following way in order to define the constants used in our plugin.
user@host:/var/www/your-site $ nano "wp-config.php"
<?php // remove this tag
/**
* WP Mail Native Setup. References:
* https://butlerblog.com/easy-smtp-email-wordpress-wp_mail/
* https://gist.github.com/butlerblog/c5c5eae5ace5bdaefb5d
* Set the following constants in wp-config.php.
* These should be added somewhere BEFORE the constant ABSPATH is defined.
*
* Author: Chad Butler
* Author URI: https://butlerblog.com
*
* For more information and instructions, see: https://b.utler.co/Y3
**/
define( 'SMTP_USER', 'support@example.com' ); // Username to use for SMTP authentication
define( 'SMTP_PASS', '<password>' ); // Password to use for SMTP authentication
define( 'SMTP_HOST', 'smtp.office365.com' ); // The hostname of the mail server
define( 'SMTP_FROM', 'support@example.com' ); // SMTP From email address
define( 'SMTP_NAME', 'Support' ); // SMTP From name
define( 'SMTP_PORT', '587' ); // SMTP port number - likely to be 25, 465 or 587
define( 'SMTP_SECURE', 'tls' ); // Encryption system to use - ssl or tls
define( 'SMTP_AUTH', true ); // Use SMTP authentication (true|false)
define( 'SMTP_DEBUG', 0 ); // for debugging purposes only set to 1 or 2