avatarAllen Kim

Summary

The article provides a guide for Mac users to set up and run a simple SMTP server locally using MailHog and Postfix for testing email functionality.

Abstract

The article outlines a step-by-step process for Mac users to install and configure a local SMTP server using MailHog. It begins with instructions for installing MailHog via Homebrew, starting the MailHog service, and accessing the MailHog interface through a web browser. The guide then proceeds to explain how to configure Postfix to relay emails through MailHog by editing the Postfix main configuration file and restarting the Postfix service. Finally, it demonstrates how to send a test email using both the command line and a Node.js script utilizing the Nodemailer library. The article concludes with a sign-off from the author, Allen Kim, indicating the end of the tutorial.

Opinions

  • The author assumes the reader has Homebrew installed and is comfortable using the command line interface.
  • The use of MailHog is presented as a straightforward solution for capturing and viewing outbound emails during development without delivering them to the actual recipients.
  • The article suggests that configuring Postfix to work with MailHog is a preferred method for local email testing on Mac systems.
  • By providing a Node.js example, the author acknowledges the popularity of JavaScript and Node.js in web development and provides an alternative method for sending test emails programmatically.
  • The inclusion of the author's name and a sign-off indicates a personal touch and accountability for the provided instructions.

Run a Simple SMTP server locally for Mac users

Photo by Ryul Davidson on Unsplash
  1. Install MailHog
$ brew install mailhog

$ brew services start mailhog

$ open http://127.0.0.1:8025

2. Config postfix

$ sudo vi /etc/postfix/main.cf
# Add this at the bottom for MailHog
myhostname = localhost
relayhost = [localhost]:1025

$ sudo postfix stop && sudo postfix start

3. Send Mail

$ date | mail -s "Test Email" [email protected]

Or. with NodeJS

const nodemailer = require('nodemailer');

async function main() {
  let transporter = nodemailer.createTransport({
    host: 'localhost',
    port: 25,
  });

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '[email protected]', // sender address
    to: '[email protected]', // list of receivers
    subject: 'This is test email', // Subject line
    text: 'Hello world?', // plain text body
    html: '<b>Hello world?</b>', // html body
  });

  console.log("Message sent: %s", info.messageId);
}();

Regards

Allen Kim

Smtp
Mac
Mailhog
Nodejs
Recommended from ReadMedium