Send Email via 365 Mail using PHP mailer Function

Sachith Sulakkhana 0 Reputation points
2024-08-15T04:50:26.8233333+00:00

i have tried to send mail via 365 office mail using php mailer function but its getting fialed, use follwing code,

<?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 = '******@somewhere.com';

$mail->Password = 'YourPassword';

$mail->SetFrom('******@somewhere.com', 'FromEmail');

$mail->addAddress('******@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 {

```sql
echo 'Message has been sent';
```}

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,171 questions
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Exchange | Exchange Server | Development
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.