Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up PHPMailer with Office365 SMTP

I am attempting to set up PHPMailer so that one of our clients is able to have the automatically generated emails come from their own account. I have logged into their Office 365 account, and found that the required settings for PHPMailer are:

Host: smtp.office365.com
Port: 587
Auth: tls

I have applied these settings to PHPMailer, however no email gets sent (The function I call works fine for our own mail, which is sent from an external server (Not the server serving the web pages)).

"host"      => "smtp.office365.com",
"port"      => 587,
"auth"      => true,
"secure"    => "tls",
"username"  => "[email protected]",
"password"  => "clientpass",
"to"        => "myemail",
"from"      => "[email protected]",
"fromname"  => "clientname",
"subject"   => $subject,
"body"      => $body,
"altbody"   => $body,
"message"   => "",
"debug"     => false

Does anyone know what settings are required to get PHPMailer to send via smtp.office365.com?

like image 762
JosephGarrone Avatar asked Jul 25 '14 03:07

JosephGarrone


People also ask

Does PHPMailer use SMTP?

PHPMailer provides powerful functionality to create HTML emails with attachments and send them to multiple recipients via SMTP or a local webserver.

Can I use Office 365 as an SMTP server?

With the Microsoft 365/Office 365 SMTP server, you can configure email clients, your WordPress site, or other applications to send emails using your Microsoft 365 email account.


2 Answers

@nitin's code was not working for me, as it was missing 'tls' in the SMTPSecure param.

Here is a working version. I've also added two commented out lines, which you can use in case something is not working.

<?php
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username = '[email protected]';
$mail->Password = 'YourPassword';
$mail->SetFrom('[email protected]', 'FromEmail');
$mail->addAddress('[email protected]', '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 <b>in bold!</b>';
$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';
}
like image 160
bgazzera Avatar answered Oct 04 '22 19:10

bgazzera


UPDATE: April 2020

Using the accepted answer for sending email using Office 365 has the high chance of not working since Microsoft is pushing for their Microsoft Graph (the only supported PHP framework right now is Laravel). If fortunately you were still able to make it work in your application, email will either go to the recipient's Junk, Trash, or Spam folder, which you don't want to happen.

Common errors I encountered were:

Failed to authenticate password. // REALLY FRUSTRATED WITH THIS ERROR! WHY IS MY PASSWORD WRONG?!

or

Failed to send AUTH LOGIN command.

or

Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.

In order to still make it work with the accepted answer, we just have to change a single line, which is the Password parameter line:

$mail->Password = 'YourOffice365Password';

Instead of setting the password with the one you use when you login to your Office365 account, you have to use an App Password instead.


Create App Password

  • First, in order to create an App Password, the Multi-Factor Authentication of your Office 365 account should be enabled (you may have to contact your administrator for this to be enabled).

  • After that, login your Office 365 in your favorite browser

  • Go to My Account page (you will see the link to this page when you click your name's initials on the upper right)
  • Choose Security & Privacy then Additional security verification
  • At the top of the page, choose App Passwords
  • Choose create to get an app password
  • If prompted, type a name for your app password, and click Next
  • You will then see the password generated by Office 365 as your App Password
  • Copy the password

After copying the password, go back to your working code and replace the Password parameter with the copied password. Your application should now be able to properly send email using Office 365.


Reference:

Create an app password for Microsoft 365

like image 26
Logan Wayne Avatar answered Oct 04 '22 20:10

Logan Wayne