title: How to send email with smtp.office365 using PHPMailer
body: <p>I'm using code below to send mail but I get Authentication unsuccessful. The same code works with smtp.gmail.com, but not with smtp.office365.com.</p>
  
  <pre><code>&lt;?php
  /**
   * This example shows settings to use when sending via Google's Gmail servers.
   */
  
  //SMTP needs accurate times, and the PHP time zone MUST be set
  //This should be done in your php.ini, but this is how to do it if you don't have access to that
  date_default_timezone_set('Etc/UTC');
  
  require '../PHPMailerAutoload.php';
  
  //Create a new PHPMailer instance
  $mail = new PHPMailer;
  
  //Tell PHPMailer to use SMTP
  $mail-&gt;isSMTP();
  
  //Enable SMTP debugging
  // 0 = off (for production use)
  // 1 = client messages
  // 2 = client and server messages
  $mail-&gt;SMTPDebug = 4;
  
  //Ask for HTML-friendly debug output
  $mail-&gt;Debugoutput = 'html';
  
  //Set the hostname of the mail server
  $mail-&gt;Host = 'tls://smtp.office365.com';
  // use
  // $mail-&gt;Host = gethostbyname('smtp.gmail.com');
  // if your network does not support SMTP over IPv6
  
  //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  $mail-&gt;Port = 587;
  
  //Set the encryption system to use - ssl (deprecated) or tls
  $mail-&gt;SMTPSecure = 'tls';
  
  //Whether to use SMTP authentication
  $mail-&gt;SMTPAuth = true;
  
  //Username to use for SMTP authentication - use full email address for gmail
  $mail-&gt;Username = "anonymized@anonymized.com.br";
  
  //Password to use for SMTP authentication
  $mail-&gt;Password = "anonymized";
  
  //Set who the message is to be sent from
  $mail-&gt;setFrom('anonymized@anonymized.com.br', 'First Last');
  
  //Set an alternative reply-to address
  $mail-&gt;addReplyTo('anonymized@anonymized.com.br', 'First Last');
  
  //Set who the message is to be sent to
  $mail-&gt;addAddress('anonymized@anonymized.com.br', 'John Doe');
  
  //Set the subject line
  $mail-&gt;Subject = 'PHPMailer GMail 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-&gt;msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  
  //Replace the plain text body with one created manually
  $mail-&gt;AltBody = 'This is a plain-text message body';
  
  //Attach an image file
  $mail-&gt;addAttachment('images/phpmailer_mini.png');
  
  //send the message, check for errors
  if (!$mail-&gt;send()) {
      echo "Mailer Error: " . $mail-&gt;ErrorInfo;
  } else {
      echo "Message sent!";
  }
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code>2017-06-06 18:24:34 SERVER -&gt; CLIENT: 220 FR1PR80CA0087.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 6 Jun 2017 18:24:25 +0000
  2017-06-06 18:24:34 CLIENT -&gt; SERVER: EHLO desenv
  2017-06-06 18:24:34 SERVER -&gt; CLIENT: 250-FR1PR80CA0087.outlook.office365.com Hello [201.47.213.100]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250 CHUNKING
  2017-06-06 18:24:34 CLIENT -&gt; SERVER: STARTTLS
  2017-06-06 18:24:34 SERVER -&gt; CLIENT: 220 2.0.0 SMTP server ready
  2017-06-06 18:24:35 CLIENT -&gt; SERVER: EHLO desenv
  2017-06-06 18:24:35 SERVER -&gt; CLIENT: 250-FR1PR80CA0087.outlook.office365.com Hello [201.47.213.100]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN250-8BITMIME250-BINARYMIME250 CHUNKING
  2017-06-06 18:24:35 CLIENT -&gt; SERVER: AUTH LOGIN
  2017-06-06 18:24:35 SERVER -&gt; CLIENT: 334 VXNlcm5hbWU6
  2017-06-06 18:24:35 CLIENT -&gt; SERVER: xxx
  2017-06-06 18:24:35 SERVER -&gt; CLIENT: 334 UGFzc3dvcmQ6
  2017-06-06 18:24:35 CLIENT -&gt; SERVER: xxx
  2017-06-06 18:24:40 SERVER -&gt; CLIENT: 535 5.7.3 Authentication unsuccessful [FR1PR80CA0087.lamprd80.prod.outlook.com]
  2017-06-06 18:24:40 SMTP ERROR: Password command failed: 535 5.7.3 Authentication unsuccessful [FR1PR80CA0087.lamprd80.prod.outlook.com]
  SMTP Error: Could not authenticate.
  2017-06-06 18:24:40 CLIENT -&gt; SERVER: QUIT
  2017-06-06 18:24:40 SERVER -&gt; CLIENT: 221 2.0.0 Service closing transmission channel
  SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  </code></pre>
  
  <p>I tried many things. Changing file encoding because in this question <a href="https://stackoverflow.com/questions/38602682/smtplib-smtpauthenticationerror-535-5-7-3-authentication-unsuccessful">smtplib.SMTPAuthenticationError: (535, &#39;5.7.3 Authentication unsuccessful&#39;)</a> the problem was the encoding of the password.</p>
  
  <p>I tried using this, as in <a href="https://stackoverflow.com/questions/33532334/phpmailer-authentication-unsuccessful">PHPMailer Authentication unsuccessful</a>:</p>
  
  <pre><code>$mail-&gt;SMTPAuth = False;
  </code></pre>
  
  <p>But I got:</p>
  
  <pre><code>2017-06-06 18:28:59 SERVER -&gt; CLIENT: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM]
  2017-06-06 18:28:59 SMTP ERROR: MAIL FROM command failed: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM]
  The following From address failed: rrjuridico@grupopetropolis.com.br : MAIL FROM command failed,5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM],530,SMTP server error: MAIL FROM command failed Detail: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] SMTP code: 530
  Mailer Error: The following From address failed: rrjuridico@grupopetropolis.com.br : MAIL FROM command failed,5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] ,530,SMTP server error: MAIL FROM command failed Detail: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] SMTP code: 530SMTP server error: MAIL FROM command failed Detail: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [RO1P215CA0017.LAMP215.PROD.OUTLOOK.COM] SMTP code: 5302017-06-06 18:28:59 CLIENT -&gt; SERVER: QUIT
  2017-06-06 18:28:59 SERVER -&gt; CLIENT:
  2017-06-06 18:28:59 SMTP ERROR: QUIT command failed: 
  </code></pre>
  
  <p>I tried removing line below:</p>
  
  <pre><code>$mail-&gt;isSMTP();
  </code></pre>
  
  <p>I got Message sent!, but email wasn't sent.</p>
  
  <p>Any Idea, any help would be very much apreciated.</p>