setup outlook smtp

Outlook SMTP Setup with PHPMailer: Easy Email Sending

In today’s tutorial, we’re diving into programmatically sending emails. We’ll explore how you can leverage PHPMailer, a popular PHP library, to send emails using Outlook’s SMTP service. If you’ve ever needed to integrate email functionality into your PHP application, you’re in the right place!

Getting Started with PHPMailer and Outlook SMTP

Step 1: Install or Download PHPMailer

First, let’s get PHPMailer set up and configure it to work with Outlook’s SMTP service. You can use Composer, PHP’s package manager, to install PHPMailer. If you haven’t already, make sure you have Composer installed and ready to go.

composer require phpmailer/phpmailer

Alternatively, if you’re not using Composer, you can download PHPMailer as a zip file, then copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each class file manually:

First, you need to download PHPMailer 5.2 stable version from GitHub

stable-php-mailer-5.2

then download the PHPMailder zip file from the right side by clicking on the green code button.

download-phpmailer-zip-file

After downloading extract the zip file and rename the folder to PHPMailer.

extract phpmailer 5.2 zip file

rename it to PHPMailer

Now Make a zip file after renaming the folder to PHPMailer

make a zip file

upload that zip file to your server folder from where you want to send the email using that PHPMailer library.

upload phpmailer to your server

after uploading the zip file to the server extract it and place it in the root folder or other directory according to your need.

extract uploaded PHPMailer zip file in server

Now create a PHP file in your server where you extracted the PHPMailer Folder.

create-new-php-file-for-sending-email

after creating a new PHP file edit it and add a sample code in that file. check the sample code below the screenshot.

open new php file and put sample code in

A Simple Example

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;              // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$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';
}
?>

Step 2: Find SMTP Setting

Now you need to log into your Outlook email and find the SMTP setting.

outlook email smtp setting

Copy all SMTP details into your mail.php file that we created earlier, server name, port, and encryption method.

Step 3: Enable SMTP AUTH for specific mailboxes

Use the Microsoft 365 admin center to enable or disable SMTP AUTH on specific mailboxes

  1. Open the Microsoft 365 admin center and go to Users > Active users.
  2. Select the user, and in the flyout that appears, select Mail.
  3. In the Email apps section, select Manage email apps.
  4. Verify the Authenticated SMTP setting: unchecked = disabled, checked = enabled.
  5. When you’re finished, select Save changes.

outlook active user

manage email app

make sure the authenticated SMTP box checked

check authenticated auth

Step4: Update your sample code with SMTP details

Your Outlook email, and password (The password should be the same as that you use for login to the webmail server)

<?php
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;              // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.office365.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
//$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$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';
}
?>

After adding all the necessary detail into your mail.php file try to hit the url in the web browser for example, https:mywebsite.com/mail.php and you will see that PHP script will send the email to your recipient address.

mail sent to gmail

And there you go! With these steps, you should now be able to integrate Outlook SMTP with PHPMailer to send out emails from your custom PHP forms or applications.

If you need any help regarding SMTP setup, feel free to contact me. Happy coding!

Leave a Comment