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 = '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 {

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

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
4,770 questions
Microsoft Exchange Online
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
542 questions
{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.