Configure PHPMailer with Office365 SMTP

I am trying to configure PHPMailer so that one of our clients can automatically receive emails from their account. I logged into my Office 365 account and found that the required settings for PHPMailer are:

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

I applied these settings to PHPMailer, however no email is sent (the function that 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" => " clientemail@office365.com ", "password" => "clientpass", "to" => "myemail", "from" => " clientemail@office365.com ", "fromname" => "clientname", "subject" => $subject, "body" => $body, "altbody" => $body, "message" => "", "debug" => false 

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

+6
source share
3 answers

The @nitin code didn’t work for me, because there was no "tls" in the SMTPSecure parameter.

Here is the working version. I also added two numbered lines that you can use if 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 = ' 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 <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'; } 
+5
source

Try this, it works great for me, I have used it for so long

 $mail = new PHPMailer(true); $mail->Host = "smtp.office365.com"; $mail->Port = 587; $mail->SMTPSecure = ''; $mail->SMTPAuth = true; $mail->Username = "email"; $mail->Password = "password"; $mail->SetFrom('email', 'Name'); $mail->addReplyTo('email', 'Name'); $mail->SMTPDebug = 2; $mail->IsHTML(true); $mail->MsgHTML($message); $mail->Send(); 
+4
source

I had the same problem when we switched from Gmail to Office365.

You must first install the connector (either an open SMTP relay or Client Send). Read this and he will tell you everything you need to know about letting Office365 send emails:

https://technet.microsoft.com/en-us/library/dn554323.aspx

+2
source

Source: https://habr.com/ru/post/972808/


All Articles