csgeek avatar

Local Email Development Service

csgeekdev

Published: 22 Apr 2021 › Updated: 22 Apr 2021Local Email Development Service

Local Email Development Service

Whenever I've worked on any application that sends out emails it's always an issue on how to mimic the behavior on my local laptop. Usually wherever the app is deployed is configured to be able to just 'work'. aka you can send email by connecting to localhost with no auth.

Now, how do I mimic locally? In the past i've tried setting up postfix etc in a docker stack, or more recently doing an smtp relay using google services.

For reference the previous pattern:

#!/usr/bin/env bash 
docker run --restart always --name mail \
    -e RELAY_HOST=smtp.gmail.com \
    -e RELAY_PORT=587 \
    -e RELAY_USERNAME=user \
    -e RELAY_PASSWORD=secret \
    -p 25:25 \ 
    -d bytemark/smtp

This pattern requires you to enable unsecured application in your google account.

The new pattern I've started using of late is leveraging the wonderful tool called MailHog.

To set it up simply add the following to your docker-compose.yml file.

  mail:
    image: mailhog/mailhog:v1.0.1
    container_name: mail
    ports:
      - 8025:8025
      - 1025:1025

If your application is running in docker you don't need to expose 1025, if you're running your app outside of docker, you need to get to 1025 to send mail.

don't forget to bring it up via docker-compose up -d mail

At this point you just need to configure your SMTP settings.

Here's an example snippet that I used for my app.

reporting:
  to: [csgeek@anyemail-host.com]
  from: "donotreply@foobar.com"
  subject: "Report for things"
  hostname: localhost
  username:
  password:
  port: 1025

Main parts you should note is the hostname is localhost, and port is 1025.

Then once everything is done you can connect to port 8025 and retrieve your email.

MailHog Screen

Leave Local Email Development Service to:

Written by

https://www.esamir.com/about/

Read more #development posts


Best Posts From csgeek

We have not curated any of csgeekdev's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From csgeek