How to install MailHog on Linux Mint

MailHog is an email testing tool that captures emails sent by your applications during development. In this tutorial, we will see how to install MailHog on Linux Mint.

Prerequisites

Before starting, we need to ensure that the following packages are installed on our system:

You can install them using the following commands:

sudo apt-get update
sudo apt-get install docker.io docker-compose

Installing MailHog

Once Docker and Docker Compose are installed, run the following commands:

sudo mkdir /opt/mailhog
sudo chmod 755 /opt/mailhog

This creates a directory /opt/mailhog where MailHog files will be stored.

Now, create a new file docker-compose.yml with the following content:

version: '3'
services:
  mailhog:
    image: mailhog/mailhog:latest
    container_name: mailhog
    ports:
      - "8025:8025"
      - "1025:1025"

Save the file and run the following command to start the MailHog container:

cd /opt/mailhog
sudo docker-compose up -d

The -d option runs the container in detached mode, which means it runs in the background.

You can check if the container is running with the following command:

sudo docker ps

The output should be similar to this:

CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS                                            NAMES
e8d22f6402c0   mailhog/mailhog      "MailHog"                2 minutes ago    Up 2 minutes    1025/tcp, 0.0.0.0:8025->8025/tcp               mailhog

Using MailHog

Now that MailHog is up and running, we can test it by sending an email.

Sending email from command line

To test MailHog from the command line, you can use the following command to send an email:

echo "Test email" | mail -s "Subject" test@example.com

This sends an email with the subject "Subject" to the email address test@example.com. The body of the email contains the text "Test email".

Now, open your web browser and enter the URL http://localhost:8025. This should open the MailHog web interface where you can see the email you just sent.

Configuring your application

To use MailHog with your application, you need to change the SMTP server settings to point to localhost:1025.

For example, if you are using PHPMailer, you can add the following code to configure it to use MailHog:

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 1025;

That's it! You have installed MailHog on Linux Mint and can now use it to test email sending from your applications.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!

Alternatively, for the best virtual desktop, try Shells!