Apple - How to send an email from command line?

There are two programs that I am aware of which will easily allow you to configure your Mac to send email from the command line.

I have written up HOWTOs for both of them:

  • mailsend
  • msmtp

Of the two, I suggest msmtp.

Configuration is complicated enough that I'm not sure if I should replicate all of the steps here, but I will mention that if you use Homebrew you can install msmtp using

brew install msmtp --with-macosx-keyring

Then the rest is just a matter of setting up the related configuration files

The first is /usr/local/etc/msmtprc

# Begin msmtprc
# Set default values for all following accounts.
defaults
tls on
logfile ~/.msmtp.log

# A first gmail address
account [email protected]
host smtp.gmail.com
port 587
protocol smtp
auth on
from [email protected]
user [email protected]
tls on
tls_starttls on

# this next line is crucial: you have to point to the correct security certificate for GMail.
# If this doesn't work, check the mstmp documentation
# at http://msmtp.sourceforge.net/documentation.html for help
#
# This next line should all be on one long line:
tls_trust_file /path/to/Thawte Roots/Thawte SSLWeb Server Roots/thawte Premium Server CA/Thawte Premium Server CA.pem

# Set a default account
# You need to set a default account for Mail
account default : [email protected]

# end msmtprc

Note that tls_trust_file line should point to wherever you have downloaded and installed the certificates from https://www.thawte.com/roots/index.html.

I put mine in ~/Dropbox/Thawte Roots so that I can have it on all of my Macs.

Then you need a ~/.mailrc file to say where the msmtp binary is located. If you use brew it will be /usr/local/bin/msmtp so the file would look like this:

set sendmail="/usr/local/bin/msmtp"

The last but crucial step is making sure your Keychain has the information exactly in the format that msmtp will expect it:

I think that covers most of the details. See http://www.tuaw.com/2010/05/04/msmtp-a-free-tool-to-send-email-from-terminal/ if you want a few more specifics.


mail -s subject [email protected] type your message, press Ctrl+D to finish


The most basic way to send mail is trough a telnet session with the smtp server of your provider/network. After you contacted the server and after every command the server will answer if it accepts the command with something like "250 OK", or if not with an error message.

All details can be found in RFC2821 - Simple Mail Transfer Protocol, Google for it. This basic way is great for testing why something goes wrong sending mail, but I think it's quite complicated to script it full proof.

First get an command-line interface on your computer, by starting Terminal. Then continue with the following commands, one after one.

Open a telnet session to port 25 of the smtp server of your provider/network

telnet name_or_ip_of_smtp_server 25

say hello plus the internetname of your provider/network, like abc.com

EHLO name_of_your_network

a from=return address is needed, the < and > are part of the command

MAIL FROM:<your_email_adress>

give one or more destinations, the < and > are part of the command

RCPT TO:<destination_email_address>
RCPT TO:<second_destination_email_address>
RCPT TO:<etc_destination_email_address>

tell the server you want start sending data

DATA

now the server should answer you can start sending your mail and goes into data-mode

your data
more data
etc

now finish data with a dot as only char on a line

.

the server goes back to command-mode so you can quit

QUIT