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
Head 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
Click 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
Click 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.
- 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.
- Copy and paste the lines below into the file.
root=[your email address here] mailhub=smtp.mailgun.org:587 hostname=[your VM's domain] AuthUser=[Mailgun Default SMTP Login] AuthPass=[Mailgun Default Password] FromLineOverride=yes
- 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.
firstname.lastname@example.org mailhub=smtp.mailgun.org:587 hostname=domain.com AuthUseremail@example.com AuthPass=987304a78f5921bf1b0c9fbaee1291e1-770f44c4-f03eb96f FromLineOverride=yes
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.
- 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' services: web: image: webdevops/php-apache-dev:ubuntu-16.04 container_name: web restart: always user: application environment: - WEB_ALIAS_DOMAIN=local.domain.com - 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 volumes: - /path/to/magento:/app:cached - /path/to/docker/ssmtp.conf:/etc/ssmtp/ssmtp.conf ports: - "80:80" - "443:443" - "32823:22" links: - mysql mysql: image: mariadb:10 container_name: mysql restart: always ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=magento volumes: - db-data:/var/lib/mysql phpmyadmin: container_name: phpmyadmin restart: always image: phpmyadmin/phpmyadmin:latest environment: - MYSQL_ROOT_PASSWORD=root - PMA_USER=root - PMA_PASSWORD=root ports: - "8080:80" links: - mysql:db depends_on: - mysql volumes: db-data: external: false
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.
- Login to the admin panel of your Magento 2 instance.
- Navigate to Stores > Configuration > General > Contacts.
- Set Enable Contact Us to Yes.
- In the Send Emails To field, enter the authorized recipient that you added to Mailgun earlier.
- Click the Save Config button.
- Flush the cache.
- On the frontend of your local Magento 2 development instance, navigate to the Contact Us page.
- 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