Skip to content

Instantly share code, notes, and snippets.

@coelhucas
Forked from roubles/EmailOSXCommandLine.md
Last active October 8, 2024 19:14
Show Gist options
  • Save coelhucas/bbc5fda0da9f1d9c674636ed7ccc71ab to your computer and use it in GitHub Desktop.
Save coelhucas/bbc5fda0da9f1d9c674636ed7ccc71ab to your computer and use it in GitHub Desktop.
Send email from OSX Command line using gmail as SMTP

(2FA) Setup an app password

Source: https://support.google.com/accounts/answer/185833

Update /etc/postfix/main.cf

sudo vim /etc/postfix/main.cf

Add the following lines, anywhere really, but preferably under the relayhost section:

relayhost = smtp.gmail.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes
smtp_sasl_mechanism_filter = plain

Create /etc/postfix/sasl_passwd

sudo vim /etc/postfix/sasl_passwd

Add the following information. Note update and with your information

smtp.gmail.com:587 <username>@gmail.com:<password>

Restart postfix

sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo launchctl stop org.postfix.master
sudo launchctl start org.postfix.master

Test

$ echo "Hello" | mail -s "Test" <someone>@gmail.com

Troubleshooting

tail -f /var/log/mail.log

or

log stream --predicate  '(process == "smtpd") || (process == "smtp")' --info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment