PHPMailer with Office365

Posted under » PHP on 1 May 2020

You should install by composer. Otherwise you will not get the vendor directory structure for this tutorial.

<¿php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '/var/www/site/vendor/phpmailer/phpmailer/src/Exception.php';
require '/var/www/site/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/var/www/site/vendor/phpmailer/phpmailer/src/SMTP.php';

$mail = new PHPMailer(true); // instantiate 1st
$mail->IsSMTP();
$mail->Host = "smtp.office365.com";
$mail->Port     = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'bill@gates.edu.sg';
$mail->Password = 'MrLK1sUgly';
$mail->SetFrom('bill@gates.edu.sg', 'FromEmail');


//From email address and name
$mail->FromName = "Bill Gates";

//To address and name
$mail->addAddress("pitt@brad.com", "Brad Pitt");

//Address to which recipient will reply
// $mail->addReplyTo("reply@yourdomain.com", "Reply");

//CC and BCC
// $mail->addCC("cc@example.com");
// $mail->addBCC("bcc@example.com");

//Send HTML or Plain Text email
$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "Mail body in HTML";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

?>

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data