Spiking PHPMailer against Office 365 using a Docker Container

November 15, 2019

PHP. It has been a while. I had to test sending email via a shared mailbox in Office 365 so I thought I would give the PHP Docker containers a go to avoid configuring my Windows environment.

I am using VS Code with the Docker extension. I create the following Dockerfile based on the example on Docker hub:


FROM php:7.2-cli

LABEL Name=enquiryapp Version=0.0.1

RUN mkdir /usr/local/lib/php/phpmailer

COPY ./PHPMailer-master /usr/local/lib/php/phpmailer

COPY . /usr/src/myapp

WORKDIR /usr/src/myapp

CMD [ "php", "enq.php" ]


At the time of writing, 7.2 is the latest stable version of PHP so if you copy this please consider updating that.

I have downloaded the PHPMailer extension to my project's folder where I decompress it and copy it to the location in the container that is configured as the "include_path" variable which is '/usr/local/lib/php'. This allows me to reference the extension in my PHP script, enq.php as below (taken from the PHPMailer example on github):



use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

use PHPMailer\PHPMailer\SMTP;


require '/usr/local/lib/php/phpmailer/src/Exception.php';

require '/usr/local/lib/php/phpmailer/src/PHPMailer.php';

require '/usr/local/lib/php/phpmailer/src/SMTP.php';


$mail = new PHPMailer(true);

try {

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;


    $mail->Host = "smtp.office365.com";

    $mail->Port       = 587;

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

    $mail->SMTPAuth   = true;

    $mail->Username = "meganb@#####.onmicrosoft.com";   

    $mail->Password = "*********";

    $mail->SetFrom('shared@#####.onmicrosoft.com', 'Enquiries');

    #$mail->SetFrom('meganb@#####.onmicrosoft.com', 'Megan Bowen');

    $mail->addAddress('shared@#####.onmicrosoft.com', 'Enquiries');


    $mail->Subject = "This is a test message";

    $mail->Body = "This is a test message";



    echo 'Message has been sent';

} catch (Exception $e) {

    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";


Building the container image means I can then run this using:


docker run -it --rm --name enq enquiryapp:latest

2019-11-14 22:22:34     SERVER -> CLIENT: 220 ####.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 15 Nov 2019 10:09:42 +0000
2019-11-14 22:22:34     CLIENT -> SERVER: EHLO a61aff4eb8e6
2019-11-14 22:22:34     SERVER -> CLIENT: 250-####.outlook.office365.com Hello [my ip address]
                                          250-SIZE 157286400
                                          250 SMTPUTF8
2019-11-14 22:22:34     CLIENT -> SERVER: STARTTLS
2019-11-14 22:22:34     SERVER -> CLIENT: 220 2.0.0 SMTP server ready



It was useful to drop into bash and check PHP config.

docker run -it --rm --name enq enquiryapp:latest bash

Then to find what the include_path is set to so I can import the extension.

php -i


include_path => .:/usr/local/lib/php => .:/usr/local/lib/php


There are a few issues with this. Email sent this way finds its way to recipient's Junk Email folders and I am not aware of a way around this (please let me know if you do). You can mark this as "Not Junk" but that is not ideal. One way around this might be to send from the shared mailbox to the shared mailbox i.e. to oneself. This didn't get junked when I tested it.



Share on Facebook
Share on Twitter
Please reload

Featured Posts

I'm busy working on my blog posts. Watch this space!

Please reload

Recent Posts

November 14, 2017

Please reload

Search By Tags
Please reload

  • Google+ Social Icon
  • Facebook Social Icon
  • LinkedIn Social Icon
  • Twitter Social Icon

Contact Me

  • LinkedIn Social Icon
  • Twitter Social Icon

© 2017 Matt Cowen

Wiltshire, UK