I have some scripts that need to send mail from the command line. While mail works out of the box, it will not work if your ISP blocks port 25, or if your ISP's network address range is on a blacklist. You could use Mail.app and AppleScript, but that requires that the user in question be logged in, and may not work for scripts run by root.
This solution configures postfix, the service used by mail and sendmail, to relay messages through a third-party server (ideally your ISP), optionally using authentication and TLS. You'll need to be root to create/edit the files and run the commands. So, without further delay, enjoy.
Edit /etc/postfix/main.cf and add the following to the end:Create /etc/postfix/smtp_sasl_passwords with the following contents:
Create /etc/postfix/smtp_tls_sites with the following contents:
Then run the following commands:
To test, try:
$ cd /etc/postfix $ chmod go-rx smtp_sasl_passwords $ postmap smtp_sasl_passwords $ postmap smtp_tls_sites
The above test may not work if your provider requires a valid source e-mail address. If that's the case, try:
echo "Hello" | mail -s "Test" firstname.lastname@example.org
This second test form specifies the "from" address as you, but can be changed to anything you want as long as it passes muster with your provider's server. You can now check the logs:
printf "Subject: TestnHello" | sendmail -f email@example.com firstname.lastname@example.org
Or, if you're crafty, you'll open a second Terminal window, and before running the tests in the first window, do:
Here are some notes about options that you see in the content above:
tail -f /var/log/mail.log
Mac OS X Hints