WordPress MS365 SMTP Setup

From WikiMLT
Revision as of 20:51, 17 June 2022 by Spas (talk | contribs) (Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:WordPress)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Here is a short man­u­al how to set­up Word­Press 5.x to send emails via SMTP or your organization's Mi­crosoft 356 cloud.

Mi­crosoft 356: Ac­count

For this task you could cre­ate a shared mail­box with­in your Mi­crosoft 356 cloud as it is shown at Fig­ure 1.

Word­Press: functions.php

1. Place the fol­low­ing code in your functions.php file. Or bet­ter cre­ate an au­toloaded plu­g­in, lo­cat­ed in wp-con­tent/­mu-plu­g­in­s/.

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;
}

Word­Press: wp-config.php

Ed­it your wp-config.php file in the fol­low­ing way in or­der to de­fine the con­stants used in our plu­g­in.

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