How do I install Mailhog?

Goal

Platform.Sh offers e-mail sending from your container but due to technically limitations, is unable to provide any logging capability for it. Mailhog is a golang application that allows you to catch and log emails being sent through sendmail.
This allows you to have a better overview on which mails are being sent via sendmail.

For more control over your email sending, please do use your own email server or create your own postmarkapp or sendgrid mail account.

Warning

MailHog is a wonderfully written efficient piece of software, but it does take away a bit of resources away from your main app. If you are only interested in debugging emails on your dev environment you can make the start_mailhog script conditional (see further in the article).

Assumptions

  • E-mail of your app is currently being sent through sendmail.

Steps

Install components

  • First thing to do is create a new install_mailhog.sh file. This will download MailHog from github and put it in your build.
#!/bin/bash
run() {
    if [ ! -f "$PLATFORM_CACHE_DIR/MailHog" ];
    then
        echo "Cache empty, downloading from github"
        install_mailhog
        install_mhsendmail
        copy_lib_to_cache
    else
        echo "Already see it in cache, reusing"
        copy_lib_from_cache
    fi

    # Create a php.ini file that overwrites the default sendmail_path
    echo 'sendmail_path=/app/mhsendmail' > $PLATFORM_APP_DIR/php.ini
}

install_mailhog() {
    curl -SsL https://github.com/mailhog/MailHog/releases/download/v1.0.1/MailHog_linux_amd64 -o MailHog
    chmod +x MailHog
}

install_mhsendmail() {
    curl -SsL https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 -o mhsendmail
    chmod +x mhsendmail
}

copy_lib_to_cache() {
    echo "Copy to cache..."
    cp -Rvf $PLATFORM_APP_DIR/MailHog $PLATFORM_CACHE_DIR
    cp -Rvf $PLATFORM_APP_DIR/mhsendmail $PLATFORM_CACHE_DIR
}

copy_lib_from_cache() {
    echo "Copy from cache... "
    cp -Rvf $PLATFORM_CACHE_DIR/MailHog $PLATFORM_APP_DIR
    cp -Rvf $PLATFORM_CACHE_DIR/mhsendmail $PLATFORM_APP_DIR
}


run
  • Then, tell your build hook to run it. It will automatically put the binaries in the build cache so subsequent builds should be quick. If you want to upgrade the version of MailHog later, simply clear the build cache platform project:clear-build-cache.

In .platform.app.yaml add:

hooks:
    build: |
        set -e
        bash install_mailhog.sh

Starting MailHog and PHP

  • Now we have to tell the system to start both MailHog AND your php application. Create a new file called start_mailhog_and_php.sh
#!/bin/bash
echo "Killing old processes"
pkill MailHog || true
pkill php-fpm || true
pkill php-fpm8.1-zts || true

if [ "$PLATFORM_ENVIRONMENT_TYPE" = production ]; then
    echo "Only starting php, since we're running on production environment"
    /usr/bin/start-php-app
else
    echo "Starting MailHog and php-fpm"
    ./MailHog & /usr/bin/start-php-app
fi

Note: the above script is only starting MailHog for development environments. You can change the script if you want to run it on production as well. But do be aware that this can cause additional load on the container. (although honestly, this should be fairly limited unless you send huge quantities of e-mail).

  • Then, add the start command to your .platform.app.yaml:
web:
    commands:
        start: 'bash start_mailhog_and_php.sh'

Commit

  • Commit and push the code and verify that the build correctly downloaded the MailHog binary. And verify that your site is still running.

git add start_mailhog_and_php.sh install_mailhog.sh .platform.app.yaml && git commit -m "Adding mailhog" && platform push

Accessing the MailHog web UI

  • MailHog will be running on port 8025, but this is not accesible from the outside world. This is good, because we don’t want visitors to your site to actually visit this. However, you can access it by setting up an ssh tunnel to your local machine.

The following command tells ssh to open a pipe from port 8025 on the container, to 8025 on your local machine:

ssh $(platform ssh --pipe) -n -N -L 8025:localhost:8025

Then, open firefox and go to http://localhost:8025 to see the MailHog webui in all its glory.

2 Likes