title: Not able to send email via PHP using office 365 smtp. Works on localhost but fails online
body: <pre><code>$name = $_POST["name"];
          $email =  $_POST["email"];
          $phone =  $_POST["phone"];
          $message = $_POST["message"];
  
          $mailbody="";
          $mailbody.="Hello,&lt;br/&gt;&lt;br/&gt;You have a new contact on your website:&lt;br/&gt;&lt;br/&gt;";
          $mailbody.="Name: ".$name."&lt;br /&gt;\r\n"; 
          $mailbody.="Email: ".$email."&lt;br /&gt;\r\n";
          $mailbody.="Phone: ".$phone."&lt;br /&gt;\r\n";
          $mailbody.="Message: ".$message."&lt;br /&gt;\r\n";
  
          require_once('./bat/phpmailer/PHPMailerAutoload.php');
          $mail = new PHPMailer();
  
          $mail-&gt;IsSMTP(); // telling the class to use SMTP
  
          // SMTP Configuration
          $mail-&gt;SMTPAuth = true;            // enable SMTP authentication
          $mail-&gt;SMTPSecure = 'tls';        
          $mail-&gt;Host = "smtp.office365.com";  // SMTP server
          $mail-&gt;Username = "email@domain.com";
          $mail-&gt;Password = "****"; 
  
          $mail-&gt;Port = 587; // optional if you don't want to use the default 
  
          $mail-&gt;IsHTML(true);
          $mail-&gt;From = "email@domain.com";
          $mail-&gt;FromName = "Website Name";
          $mail-&gt;Subject = "Subject";
          $mail-&gt;AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
          $mail-&gt;MsgHTML($mailbody);
  
          // Add as many as you want
          $mail-&gt;AddAddress("email@domain.com", "Name");
  
          if(!$mail-&gt;Send()) {
                //header("Location:contact.php?sent=0#sent");
                echo "Mailer Error: " . $mail-&gt;ErrorInfo;
          } else {
               header("Location:contact.php?sent=1#sent");
          }
  </code></pre>
  
  <blockquote>
    <p>When I test on localhost the email sends successfully and gets delivered.<br/>
    When I test online it throws the following error: Mailer Error: SMTP connect() failed.<br/></p>
    
    <p>I tried to remove $mail->IsSMTP(); as mentioned in one of the suggestions. The scripts runs online and returns true but the email never gets delivered.</p>
  </blockquote>