Sending Emails with Postfix
On macOS, you can send emails directly from the terminal using Postfix and Gmail’s SMTP. Here’s how I set it up and tested it.
1. Checking Installed Mail Utilities
First, I checked which mail-related utilities were installed using Homebrew:
bashCopybrew list | grep -E 'msmtp|ssmtp|mutt|sendmail|postfix'
The output showed that mutt
was installed, but it turned out that I was actually using Postfix.
2. Checking Postfix Configuration
To verify my Postfix settings, I ran:
bashCopypostconf -n
The important settings included:
bashCopyrelayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_use_tls = yes
inet_interfaces = loopback-only
3. Sending Emails from Terminal
Once I confirmed Postfix was configured properly, I tested sending an email using different methods.
Using the mail
command
bashCopyecho "This is a test email body." | mail -s "Test Subject" recipient@example.com
Using the mail
command with multi-line input
bashCopymail -s "Test Email" recipient@example.com <<EOF
Hello,
This is a test email from my Mac's terminal using Postfix.
Best,
Me
EOF
Sending an attachment
bashCopyecho "See attached file." | mail -s "Attached File" -A /path/to/file.pdf recipient@example.com
Using sendmail
bashCopyecho -e "Subject: Test Email\n\nThis is a test." | sendmail recipient@example.com
4. Checking the Mail Queue
If an email is stuck in the queue, you can check it with:
bashCopymailq
To manually flush the queue:
bashCopypostfix flush
Conclusion
With this setup, I was able to send emails from my Mac’s terminal using Postfix and Gmail’s SMTP relay. This can be useful for automating emails, debugging mail issues, or integrating with scripts.