Skip to content

Instantly share code, notes, and snippets.

@haccks
Last active March 29, 2024 23:27
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save haccks/7ec2fe500332a6d23ee68d1c1a2279d5 to your computer and use it in GitHub Desktop.
Save haccks/7ec2fe500332a6d23ee68d1c1a2279d5 to your computer and use it in GitHub Desktop.
Send mail from local host using postfix on MacOS High Sierra.

Setting up postfix on MacOS High Sierra

MacOS Sierra High comes with preinstalled postfix. Follow these steps to configure it on a local system:

1. Create a sasl_passwd file

sudo nano /etc/postfix/sasl_passwd
  • Add this to the file
smtp.gmail.com:587 EMAIL@gmail.com:PASSWORD

Replace EMAIL with your gmail username and replace PASSWORD with your gmail password.

  • Create a lookup table
sudo postmap /etc/postfix/sasl_passwd

2. Edit the main.cf file

  • Open postfix's main.cf
sudo nano /etc/postfix/main.cf
  • Edit these values if they are not set already
mail_owner = _postfix
setgid_group = _postdrop
  • Add these fields at the end of the file (I am choosing mtp.gmail.com as myrelayhost to send email from gmail account. You can choose any other SMTP relayhost)
#Gmail SMTP
relayhost=smtp.gmail.com:587
# Enable SASL authentication in the Postfix SMTP client.
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options=noanonymous
smtp_sasl_mechanism_filter=plain
# Enable Transport Layer Security (TLS), i.e. SSL.
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

3. Enable less secure app access

If you are using gmail then you must have to enable less secure app access.

4. Set postfix to autorun on reboot

  • Disable SIP

  • Open /System/Library/LaunchDaemons/com.apple.postfix.master.plist

sudo nano /System/Library/LaunchDaemons/com.apple.postfix.master.plist
  • Remove these lines
<string>-e</string>
<string>60</string>

and add these lines

<key>KeepAlive</key>
<true/>
  • Reload and relaunch the deamon
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.postfix.master.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.postfix.master.plist

5. Send mail

Test by sending a mail

echo "Postfix setting test" | mail -s "Hello Postfix" "EMAIL@HOST.com"

Replace EMAIL@HOST.com with the email id of recipent.

Some useful commands

  • mailq : Check mails in the queue and errors if mail not sent.
  • sudo postfix flush: Flush the queue.
  • sudo postsuper -d ALL Delete mails in the queue.
  • sudo rm /var/mail/$USER Remove all mails from local host.
  • sudo postfix start Start postfix mail system.
  • sudo postfix stop Stop postfix mail system.
  • sudo postfix reload Reload postfix mail system.

References:

  1. How to send emails from localhost.
  2. Postfix auto start on Sierra for smtp relay.
@rjwhobbs
Copy link

Thank you for sharing this!

@saumyasinghal747
Copy link

Is it possible to receive mail using this email address?

@haccks
Copy link
Author

haccks commented Oct 8, 2022

@saumyasinghal747 what email address?

@saumyasinghal747
Copy link

@haccks Thank you for responding so quickly!

The email address that I enter in step 1, let's say me@example.com. I can send emails as me@example.com using these instructions. However when I type mail, it says I don't have new mail whereas my gmail app says I do. It also says no mail for <my computer username>, so maybe it isn't configured to check for mail from my Gmail?

Any help would be great! Thank you so much!

@haccks
Copy link
Author

haccks commented Oct 10, 2022

@saumyasinghal747 This setup was to send email. To receive email you need some more configuration. Check this out.

@saumyasinghal747
Copy link

Unfortunately clicking on that link brings me to this page. Is that what you intended?

@haccks
Copy link
Author

haccks commented Oct 10, 2022

@saumyasinghal747 Sorry. Updated the link.

@raarts
Copy link

raarts commented Nov 22, 2022

I'm kinda worried that you need to disable SIP. Does that mean that Apple installs postfix, but it's not intended to be used?

@haccks
Copy link
Author

haccks commented Nov 22, 2022

@raarts
"Does that mean that Apple installs postfix, but it's not intended to be used?": No. It doesn't actually mean that. If you are doing any changes to /System directory then apple expects that you know what you are doing and it forces you to disable SIP to make any changes (as SIP is intended to enhance security of Mac).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment