search

Home  >  Q&A  >  body text

Configure PHPMailer to use Office 365 SMTP

I'm trying to set up PHPMailer so that one of our clients can automatically generate emails from their own account. I logged into their Office 365 account and found that the required settings for PHPMailer are:

Host: smtp.off   ice365.com
Port: 587
Auth: tls

I've applied these settings to PHPMailer, but no email is being sent (the function I'm calling is for our own mail, which is sent from an external server (not the one serving the web page)).

"host"      => "smtp.of   fice365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "clientemail@off  ice365.com",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "clientemail@of  fice365.com",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false

Does anyone know what settings are required to send PHPMailer through smtp.offi ce365.com?

P粉510127741P粉510127741424 days ago785

reply all(2)I'll reply

  • P粉176151589

    P粉1761515892023-10-17 10:00:21

    Updated: May 2022

    So I'm struggling with this issue. I can provide the answer to this question for enterprise accounts with access to Exchange Online and Microsoft Admin Center.

    TLDR: Go to the admin center and select the user you want to send mail to. Then after setting up "Authenticated SMTP" look at the settings after "Email" and "Email Application" and just enable it.

    Still not working? I've got you covered, here's how I got it to work completely.

    1. Using PHP Composer actually saves a lot of work.
    2. Replace your code with my code and change it after testing
    SMTPDebug = SMTP::DEBUG_off;
    
    //SMTP
    $mail = new PHPMailer(true); //important
    $mail->CharSet = 'UTF-8';  //not important
    $mail->isSMTP(); //important
    $mail->Host = 'smtp.office365.com'; //important
    $mail->Port       = 587; //important
    $mail->SMTPSecure = 'tls'; //important
    $mail->SMTPAuth   = true; //important, your IP get banned if not using this
    
    //Auth
    $mail->Username = 'yourname@mail.org';
    $mail->Password = 'your APP password';//Steps mentioned in last are to create App password
    
    //Set who the message is to be sent from, you need permission to that email as 'send as'
    $mail->SetFrom('hosting@mail.org', 'Hosting Group Inc.'); //you need "send to" permission on that account, if dont use yourname@mail.org
    
    //Set an alternative reply-to address
    $mail->addReplyTo('no-reply@mail.com', 'First Last');
    
    //Set who the message is to be sent to
    $mail->addAddress('customer@othermail.com', 'SIMON MÜLLER');
    //Set the subject line
    $mail->Subject = 'PHPMailer SMTP test';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML(file_get_contents('replace-with-file.html'), __DIR__);  //you can also use $mail->Body = "

    This is a body message in html

    " //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; //Attach an image file //$mail->addAttachment('../../../images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { }
    1. This may look like your file and that's okay, but now comes the easy tricky part. Like Google, Microsoft has implemented a "switch" for SMTP. Just go to your admin center from your business account, or ask someone with permission to do that:
    1. If you use MFA, make sure to use the app password mentioned in https://stackoverflow.com/ a/61359150/14148981

    2. Run script

    I hope this helps someone. It took me a long time to find this option.

    Get a summary of all steps for app passwords and authentication:

    1. Use administrator account:
    • Active Directory AD -> Properties -> Security Default: Off.
    • Active Directory Portal -> Conditional Access -> Configure MFA Trusted IPs -> Allow User Application Password: Enable
    • Admin Page User List -> "Multiple Authentication" for target users: Enable then force
    • Manage Page User List -> User Details -> Mail -> "Manage Email Application" -> "Authenticated SMTP": Enable
    1. Use user account:
    • User Account Profile -> Security -> Add Login Method: Apply Password
    1. PHP Mail Program Settings:
    • smtp.office365.com, 587, tls, email, appPassword

    reply
    0
  • P粉395056196

    P粉3950561962023-10-17 00:05:06

    @nitin's code doesn't work for me because it's missing "tls" in the SMTPSecure parameter.

    This is a working version. I've also added two commented out lines that you can use if problems arise.

    isSMTP();
    $mail->Host = 'smtp.office365.com';
    $mail->Port       = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth   = true;
    $mail->Username = 'somebody@somewhere.com';
    $mail->Password = 'YourPassword';
    $mail->SetFrom('somebody@somewhere.com', 'FromEmail');
    $mail->addAddress('recipient@domain.com', 'ToEmail');
    //$mail->SMTPDebug  = 3;
    //$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; //$mail->Debugoutput = 'echo';
    $mail->IsHTML(true);
    
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body in bold!';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

    reply
    0
  • Cancelreply