Send Email From Magento 2 Development Environment

Docker + Magento 2 + Mailgun

This tutorial is a direct add-on to our previous blog post: Docker for Magento 2 Development. You will need to follow that tut before you are able to send email from your local Magento 2 development environment.

How To Send Email From Your Virtual Machine

I’ve needed to send email from my local development environment many times in the past. Like most developers, I’ve tried many different things from postfix to sendmail to various types of SMTP setups. Sometimes these solutions work and sometimes they don’t. Something I’ve been vocal about, and will continue to be, is the ease and consistency of dev environment setup. What I mean is that as developers we are going to need a reliable VM setup from now until retirement. We shouldn’t have to spend hours or days or weeks trying to set them up either. We should be able to whip up a VM in the configuration of our choosing within no more than an hour or two in most cases.

After having spent cumulative weeks of my life trying to find the best way to send e-mail from my local development environment, I’ve finally landed on a solution. This solution is not only free, but it is also reliable and easy to set up.

Send Email Safely From Your Magento 2 VM

Shawn, what do you mean by “send email safely”? Will I get injured or robbed if I don’t follow these steps? No, of course not! What I mean by “send email safely” from your local development environment is a bit less dramatic than that. I often download a dump of my production database and import it into my local VM. I do this because it is important to keep your local and production environments as similar as possible. It’s also VERY helpful to have a full catalog, customers, and orders to work with. The danger I am referring to is that during the course of development it’s very easy to accidentally send unintended emails to customers from your virtual machine. I am sure that at least a few of us have done this once or twice in the past. The customer on the receiving end will either be confused by or suspicious of the erroneously sent email. That can’t be good for business and it’s also just plain embarrassing.

Create & Configure Mailgun Account

Let’s jump into the configuration so that we can get your Magento 2 virtual machine sending e-mail in no time at all. It is a relatively simple process involving a free Mailgun account and a small configuration file.


Create Free Mailgun Account

Sign up to send email FREE from MailgunHead on over to Mailgun’s website and click the Sign Up button. You may see a payment form on this page, it is optional and not required. Select the FREE plan. Also, it is important that you use a real e-mail address when creating an account because Mailgun is going to send you a confirmation link. You will need to click that confirmation link before you can start sending emails.

After you create the account, you will be presented by Mailgun with a series of steps for setup. We are not going to complete all of the steps. For example, we are not going to add and verify a domain. You really only need to add and verify a domain if you plan on using Mailgun in your production environment. Fortunately, Mailgun automatically creates a sandbox domain for us to use.

Add Authorized Email Recipients

Authorize specific email addresses to receive emails from your Magento 2 development environmentClick the Domains link in the global navigation. You should be able to see that your sandbox domain shows as active. That’s exactly what we want. Click the Authorized Recipients button and add at least one email address. This step is crucial. The sandbox domain will only allow sending email to addresses listed as authorized recipients. This is what I meant by “send email safely”. The authorized recipients list means that you won’t accidentally fire off an email to anyone that you don’t intend. After you add email addresses to the Authorized Recipients list, Mailgun will send them an invitation to receive emails. Email will not be sent to these addresses until the invitation(s) are accepted.

Grab Your SMTP Login & Password

Retrieve your Mailgun SMTP CredentialsClick the Domains link again in the global navigation. Then click on the actual sandbox domain itself. In this new screen, you will be shown all of the credentials necessary for sending emails from your local development environment. Either keep this window open or copy and paste the credentials into a text document that you can refer back to later.

Configure Your VM To Send Email


Create SMTP Configuration File

Ok, now that your Mailgun account has been created and your authorized recipients have been added, it’s time to put it into action. In this step, you’ll need the SMTP login and password that you obtained in the previous steps.

  1. Create a file on your local machine at /path/to/docker/ssmtp.conf and then open this file in the text editor of your choosing.
  2. Copy and paste the lines below into the file.
    root=[your email address here]
    hostname=[your VM's domain]
    AuthUser=[Mailgun Default SMTP Login]
    AuthPass=[Mailgun Default Password]
  3. In this file, let’s replace the placholders. The AuthUser and AuthPass fields require the credentials from Mailgun that you set aside earlier. The finished file should look something like this.

Mount Your Configuration File To Docker

We’re almost ready to start sending our emails from our Magento 2 + Docker development environment. Before we can do that, we need to mount our file to the VM to a very specific location.

  1. cd /path/to/docker
  2. docker-compose down
  3. Open your docker-compose.yml file in the text editor of your choice and add line 18 as shown below. Obviously, replace /path/to/docker with the actual path to the file on your host machine.
    version: '3'
            image: webdevops/php-apache-dev:ubuntu-16.04
            container_name: web
            restart: always
            user: application
              - WEB_DOCUMENT_ROOT=/app/pub
              - PHP_DATE_TIMEZONE=EST
              - PHP_DISPLAY_ERRORS=1
              - PHP_MEMORY_LIMIT=2048M
              - PHP_MAX_EXECUTION_TIME=300
              - PHP_POST_MAX_SIZE=500M
              - PHP_UPLOAD_MAX_FILESIZE=1024M
              - /path/to/magento:/app:cached
              - /path/to/docker/ssmtp.conf:/etc/ssmtp/ssmtp.conf
              - "80:80"
              - "443:443"
              - "32823:22"
              - mysql
            image: mariadb:10
            container_name: mysql
            restart: always
              - "3306:3306"
              - MYSQL_ROOT_PASSWORD=root
              - MYSQL_DATABASE=magento
              - db-data:/var/lib/mysql
            container_name: phpmyadmin
            restart: always
            image: phpmyadmin/phpmyadmin:latest
              - MYSQL_ROOT_PASSWORD=root
              - PMA_USER=root
              - PMA_PASSWORD=root
              - "8080:80"
              - mysql:db
              - mysql
            external: false
  4. docker-compose up -d --build

Send Email From Your Local Dev Environment

If you followed all of the steps above, you should be ready to send email from your local Magento 2 development environment. Remember the “send email safely” declaration. You will only be able to send emails to those that you added to Mailgun as authorized recipients. For the sake of simplicity, let’s set up our Magento 2 Contact Us form to send emails to one of our authorized recipients.

Configure Magento 2 to send email from your local Docker development environment

  1. Login to the admin panel of your Magento 2 instance.
  2. Navigate to Stores > Configuration > General > Contacts.
  3. Set Enable Contact Us to Yes.
  4. In the Send Emails To field, enter the authorized recipient that you added to Mailgun earlier.
  5. Click the Save Config button.
  6. Flush the cache.
  7. On the frontend of your local Magento 2 development instance, navigate to the Contact Us page.
  8. Fill out the email form and submit. If all went well, you should see an email in your inbox momentarily.

Also in the Magento 2 Development Environment Series

2 Replies to “Send Email From Magento 2 Development Environment”

  1. hi , this is great, thanks Shawn, but it looks like this require the image coming with sSmtp, I installed the docker-machine one following your another article, that one has Postfix . It is not working with this settings. Is there a way to add sSmtp to my docker VM?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.