Create a Send-Only SMTP Server on macOS Using Gmail

Introduction

This post describes how I configured Postfix on my Mac to relay outbound email via Google’s SMTP service. Why? I am creating a local website development environment where PHP applications may generate email messages for contact forms or notifications. For example, after installing WordPress, it generates an email message announcing a successful install. However, we can use the built-in mail command to run a simple test after we complete our configuration work.

This post is for those who are developers and are familiar with using the command line in a Linux-like environment. However, if something is not clear, please let me know.

I am using macOS Catalina on my Mac. Postfix is already installed and ready to go. We only need to configure it to use Google’s SMTP service. You will need to have an existing Gmail account. You will also need to set up an App password to use in your Postfix configuration. Do not use your main Gmail account password. The Gmail SMTP service is free to use for limited, personal emails. I understand that the limit is about 99 emails per day. Email from your local machine will appear to originate from your Gmail account. Setting up Postfix to relay locally originated email through your Gmail account is much simpler that trying to build a full-fledge email server. If this sounds good, then read on.

macOS Postfix SMTP Configuration for Gmail

First, we need to add the following lines to /etc/postfix/main.cf using your favorite editor and sudo:

We need to create a couple of support files. The first one, generic, will create a mapping from our local machine account name to the Gmail account name. The second one, sasl/passwd, contains our Gmail account App password in a machine readable format.

The generic mapping file should exist already. Add lines similar (but use your actual account names) to the following at the end of the file and run postmap to generate the machine readable file, generic.db:

You can execute echo “whoami@hostnameto determine what the first line should be. This gives you the user at the hostname. The second line is a catchall for any user on your machine. You can tweak this file as desired.

Next, we need to create the hashed passwd file. It can go anywhere; I decided to create a subdirectory, sasl, to store it in. After we create the file, we will again use postmap to create the machine readable version:

Testing our Gmail SMTP Relay Service

And that’s all you have to do! The postfix service is automatically loaded by launchctl when your Mac starts up. The postfix service is not running, however, it’s configured to run when it receives a request. If you are interested, have a look at /System/Library/LaunchDaemons/com.apple.postfix.master.plist. To test your new postfix configuration, run this mail command:

While this does work, I like having the postfix service running all the time. It’s helpful for debugging and checking the postfix status. Some examples for using the postfix command:

That last command, mailq, can be used for debugging purposes. For example, if an email has not been received, you can quickly check to see if it’s still queued up on your local machine.

Conclusions

I have created a simple solution for testing Apps that need to generate email, such as a contact form in WordPress. This is a great solution for low-volume email when testing website Apps. So far it seems to be working well for me. As always, I will try to keep this post up to date with any additional tweaks that I make.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.