I've had a look on Google and the man pages for mail, but I can't figure out how to set it up. All I want to do is set up my email address so that I can send email via the terminal. How do I do that?


Traditionally, Unix mail programs, including mail, rely on a MTA (Mail Transfer Agent) to do the actual transmission.

You can use either a full-size MTA (postfix, exim4, opensmtpd) by configuring it with a 'smarthost' (relayhost), or a lightweight outgoing-only MTA (msmtp, ssmtp, esmtp, nullmailer) which always uses one.

For personal use, msmtp will be easiest to set up – it can only send mail, not receive, and allows user-specific configuration.

Install msmtp, then add your Gmail account into ~/.msmtprc:

    tls on
    # the path below may need to be adjusted
    tls_trust_file /etc/ssl/certs/ca-certificates.crt

account gmail
    from youraddress@gmail.com
    host smtp.gmail.com
    port 587
    auth plain
    user youraddress@gmail.com

account default : gmail

Now tell mail to use msmtp, by editing ~/.mailrc:

set sendmail="/usr/bin/msmtp"

(If this does not work, run ln -s /usr/bin/msmtp /usr/sbin/sendmail as root.)

Finally, if you want mail/msmtp to remember your Gmail password, it goes to ~/.netrc:

machine smtp.gmail.com
    login youraddress@gmail.com
    password "your password here"

Unless your system admin has already set up the mail system, you must configure a Mail Transfer Agent. That is sendmail traditionally, modern systems use postfix or exim4.


On many systems you will find a mailx command which can be used to send email. This may be a link to the mail command, but changes the behavior of the command. As other have noted, this assumes you have a MTA (Mail Transfer Agent) configured on your server. It can be used interactively using a command like:

mailx -s "This is a message" user@example.com
This is a message

Or with a pipe to send the output of a command:

cat myfile.txt | mailx -s "This is the file" user@example.com

Running it as mailx -h should give you usage instructions.


If you're not bound to the mail command, nail offers a similar interface but can send mail using SMTP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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