0

I have a script that uses phpmailer to send emails via office 365 smtp. It had been working for the last year or so. Now I'm getting an error connecting to the server.

PHP Fatal error:  Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php:1898
Stack trace:
#0 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php(1725): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array)
#1 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php(1481): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Fri, 14 F...', 'This is a multi...')
#2 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php(1320): PHPMailer\PHPMailer\PHPMailer->postSend()
#3 C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\submitform2.php(195): PHPMailer\PHPMailer\PHPMailer->send()
#4 {main}
thrown in C:\inetpub\wwwroot\Parishes_staging\ChristTheKing\vendor\phpmailer\phpmailer\src\PHPMailer.php on line 1898

The script is as follows:

require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth   = true;
$mail->Username = "user@company.org";                 
$mail->Password = "password";                           
$mail->SetFrom('sentfromemail@company.org', 'FromEmail');
//$mail->addAddress('user@company.org', 'ToEmail');
$mail->addAddress('user2@company.org', 'ToEmail');
$mail->addBCC('user2@company.org', 'ToBCCEmail');
$mail->SMTPDebug  = 3;
$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; 
$mail->Debugoutput = 'echo';
$mail->IsHTML(true);

$mail->Subject = 'Parish Registration Form';
$mail->Body    = $body;
$mail->AltBody = $body;

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} 
else {
    echo 'Message has been sent';
}

I've searched the internet for the connection settings associated with Office 365 and I see nothing wrong. Did something change on the Microsoft servers that I should know about but don't?

RCDAWebmaster
  • 319
  • 5
  • 17
  • Does this answer your question? [Setting up PHPMailer with Office365 SMTP](https://stackoverflow.com/questions/24947434/setting-up-phpmailer-with-office365-smtp) – Simone Rossaini Feb 14 '20 at 15:37
  • Have a read of [the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting). – Synchro Feb 14 '20 at 20:14

2 Answers2

0

There is nothing wrong on your end.

Check your firewall for port 587 blocking. If not then the issue might be from SMTP server-side. Contact Microsoft support.

mail2bapi
  • 1,547
  • 1
  • 12
  • 18
  • I checked and the port is open. is there anything else I can try before contacting Microsoft? maybe a way to test settings with the server. – RCDAWebmaster Feb 14 '20 at 14:41
  • I don't think there is any issue your server. – mail2bapi Feb 14 '20 at 14:47
  • I downloaded SocketLabs SMTP console and am able to connect and authenticate if I use the same server, port 25 and no encryption. I changed the port in my code to 25 and SMTPSecure = 'none'; the page just times out without any output. – RCDAWebmaster Feb 14 '20 at 16:22
  • Are you sure it's connecting to Office 365? I doubt very much that MS allows authentication without encryption. – Synchro Feb 14 '20 at 20:16
  • The server uses TLS encryption but with smtp console, I had to connect no encryption then starttls after establishing a connection prior to authentication.. – RCDAWebmaster Feb 18 '20 at 01:37
0

It turns out to be a certificate error. When no certificate file is specified in php.ini the root certificate is supposed to be used. but it was not being used.

I had to add the following code to get it working:

$mail->SMTPOptions = array(
    'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
);
RCDAWebmaster
  • 319
  • 5
  • 17