How to send an email from command line or script? I want to be able to run the script programmatically by passing the receiver and the message.

  • If you are writing a program many languages have libraries that deal with email – Mark Aug 2 '11 at 13:43
up vote 19 down vote accepted

mail -s subject someone@example.com type your message, press Ctrl+D to finish

  • 7
    If you haven't configured your system to tell it where to send email, that will probably not actually send email, it will just pile up on my Mac. – TJ Luoma Feb 12 '12 at 19:55
  • 13
    -1: This answer is incomplete. Unless system was configured prior to executing this command, as @TJLuoma noted, this command will not work. – melvynkim Jun 17 '13 at 18:45
  • 1
    TJ Luoma and melvkim are incorrect: a standard OS X system out of the box will send email just fine with no configuration needed. The OS X machine will act as its own SMTP server, looking up the DNS MX for the destination domain and sending the mail there. – JohnEDee Jan 28 '17 at 5:28
  • @JohnEDee At least as you don't expect the receiver to answer to a mail coming from username@users_mac.local... – nohillside Jan 28 '17 at 9:07
  • 1
    @patrix, Correct, it uses the logged-in account's long username and the Mac's self-generated ".local" address to form a "From". I use it all the time for notification emails to me and my staff from shell-scripted processes. TJ Luoma and melvkim were saying it "will not work" and "not actually send email", however, both of which are incorrect. – JohnEDee Jan 29 '17 at 18:31

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:

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 example@gmail.com
host smtp.gmail.com
port 587
protocol smtp
auth on
from example@gmail.com
user example@gmail.com
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 : example@gmail.com

# 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.

  • Obviously my examples in the above script assume that you are using Gmail, but it can be used for other servers as well. Change example@gmail.com to your Gmail or Google Apps email address. – TJ Luoma Feb 12 '12 at 20:16
  • I was finally able to get this to work by using the Google Roots available here: pki.google.com/roots.pem. If you are stuck on issues with the trust file and Gmail, this may unblock you. – codewise Mar 22 at 17:59
  • If you are having issues with the location of the config (msmtprc) file, after installing with Homebrew on macOS 10.13.3, msmtp looked for it in my home directory: ~/.msmtprc – codewise Mar 22 at 18:04

This worked for me, it was written with Lion in mind but works for Mountain Lion. Btw this is using Gmail so if you're not...

You don't need to download anything! (just setup a gmail account)

http://www.anujgakhar.com/2011/12/09/using-macosx-lion-command-line-mail-with-gmail-as-smtp/

Configure Postfix for Gmail SMTP Edit file /etc/postfix/main.cf

sudo vim /etc/postfix/main.cf

and add in the following below the commented out relayhosts :-

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

Generate sasl_password if not already exists

sudo vim /etc/postfix/sasl_passwd

and enter in the following:-

[smtp.gmail.com]:587 username@gmail.com:password

Run the following commands

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

And you are done….
Now, you should be able to send emails from within the command line e.g. to send the contents of a directory as a tree to an email address

tree /var/www/somefolder | mail -s "contents" your@yourdomain.com
  • PS: I got this to work great when running this manually from Terminal or in scripts. However at this point I'm stuck with the problem that mails are never sent when launchd launches the script. I guess it's a problem with launchd job setup. – Jonny May 27 '13 at 2:17
  • I got it working. See apple.stackexchange.com/questions/92406/… – Jonny May 27 '13 at 3:09
  • +1 Thanks for adding this. Really helped me out. All I really needed was the relayhost entry in main.conf (it's my own SMTP server on my LAN). – boot13 Feb 7 '15 at 19:33
  • 3
    The above may be out of date. I had to do add "smtp_sasl_mechanism_filter = plain" as per stackoverflow.com/a/26451135/197789 to get it to work. Otherwise I got a "generic failure" error. – Von Jun 19 '16 at 21:44

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

mtcmail is another option: http://www.macupdate.com/app/mac/33505/mtcmail-cli.

[Ignore this additional text. Added because: body must be at least 30 characters; you entered 27.]

You may want to check out postfixconf that enables and configures OS X to send emails from the command line (using your input).

It is based on the postfix configuration that are well documented on the web here, here and here.

Since it's Unix based, you can use sendmail command.

  • -1: sendmail is the SMTP server command – vartec Apr 19 '11 at 10:32

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.