hello guide, we will show you how to set up Microsoft Office 365 for WordPress email.

first, we need to setup office 365 for sending mails from website and added the code to functions.php and wp-config.php.

we will show you about 2 steps of configuration:

  1. configure in wp-config.php

    // Define constants for SMTP settings
    define( 'SMTP_HOST',     'smtp.office365.com' );
    define( 'SMTP_USER',     'yourusername@gmail.com');
    define( 'SMTP_PASSWORD', 'your_email_password' );
    define( 'SMTP_FROM',     'yourusername@gmail.com' );
    define( 'SMTP_FROMNAME', 'yoursitename' );
    define( 'SMTP_PORT',     '587' );
    define( 'SMTP_AUTH',     true );
    define( 'SMTP_SECURE',   'tls' );

    here is,you define variable and value in wp-config.php

  2. configure in  functions.php
    after define variable and value of parameter,you need call them to functions.php.

    // Use wp-config.php constants in code snippet
    add_action( 'phpmailer_init', 'send_smtp_email' );
    function send_smtp_email( $phpmailer ) {
    $phpmailer->Mailer     = 'smtp';
      $phpmailer->Host =       SMTP_HOST;
      $phpmailer->Username =   SMTP_USER;
      $phpmailer->Password =   SMTP_PASSWORD;
      $phpmailer->From =       SMTP_FROM;
      $phpmailer->FromName =   SMTP_FROMNAME;
      $phpmailer->Port =       SMTP_PORT;
      $phpmailer->SMTPAuth =   SMTP_AUTH;
      $phpmailer->SMTPSecure = SMTP_SECURE;
    }
    add_filter( 'wp_mail_from', function( $email ) {
        return SMTP_FROM;
    } );

This means, “$phpmailer->From = SMTP_FROM;” instead of

“add_filter( ‘wp_mail_from’, function( $email ) { return SMTP_FROM; } );” should use because phpmailer_init may not overwrite from information.

if you don’t include or add_filter(), I think may not work.

Finally,I hope this article help you.