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
    – mmmmmm
    Aug 2, 2011 at 13:43

8 Answers 8


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

  • 21
    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, 2012 at 19:55
  • 26
    -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, 2013 at 18:45
  • 3
    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, 2017 at 5:28
  • 3
    @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, 2017 at 18:31
  • 2
    @RamarajT, yes you can pipe the output of the echo command to mail, and it will then not require a Cmd-D. For example: echo This will be the body of the email | mail -s "This is the Subject" [email protected]
    – JohnEDee
    Sep 14, 2019 at 9:51

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

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

The first is /usr/local/etc/msmtprc (for Intel) or /opt/homebrew/etc/msmtprc (for ARM).

# Begin msmtprc
# Set default values for all following accounts.
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/ or /opt/homebrew/bin depending on the system architecture, so the file would look like this:

set sendmail=YOUR_HOMEBREW_PREFIX/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 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 [email protected] to your Gmail or Google Apps email address.
    – TJ Luoma
    Feb 12, 2012 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, 2018 at 17:59
  • 1
    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, 2018 at 18:04
  • @TJLuoma - I can't find the image of the keychain on Engadet. Do you know where it might be? May 9 at 20:41

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


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

your data
more data

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


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


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)


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
smtp_sasl_mechanism_filter = plain

Generate sasl_password if not already exists

sudo vim /etc/postfix/sasl_passwd

and enter in the following:-

[smtp.gmail.com]:587 [email protected]: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" [email protected]
  • 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, 2013 at 2:17
  • I got it working. See apple.stackexchange.com/questions/92406/…
    – Jonny
    May 27, 2013 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, 2015 at 19:33
  • 4
    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, 2016 at 21:44

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.

  • I can confirm that this script works on macOS 10.13.6. I set it up with my fastmail account easily. Now mail [email protected] delivers emails as intended. Jul 14, 2020 at 10:39

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



The macOS included curl can be used to send an e-mail.

See everything curl - Sending email and Using curl to send email:

curl --ssl-reqd \
  --url 'smtps://smtp.gmail.com:465' \
  --user '[email protected]:password' \
  --mail-from '[email protected]' \
  --mail-rcpt '[email protected]' \
  --upload-file mail.txt

mail.txt file contents:

From: "User Name" <[email protected]>
To: "John Smith" <[email protected]>
Subject: This is a test

Hi John,
I’m sending this mail with curl thru my gmail account.

Additional info:

  1. I’m using curl version 7.21.6 with SSL support.
  2. You don't need to use the --insecure switch, which prevents curl from performing SSL connection verification. See this online resource for further details.
  3. It’s considered a bad security practice to pass account credentials thru command line arguments. Use --netrc-file. See the documentation.
  4. You must turn on access for less secure apps or the newer App passwords.

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .