275

How to send mail from the command line?

5
  • 1
    ubuntuforums.org/showthread.php?t=780509 may help you.
    – User
    Nov 12, 2010 at 20:29
  • 1
    You can also use pine.
    – Kaveh
    Dec 31, 2013 at 3:43
  • 2
    You can also install msmtp and follow the instructions described in the ArchWiki
    – thiagowfx
    Dec 19, 2014 at 3:56
  • 2
    It's too bad the answers to this are so out of date. It's an important question that could use a good tutorial Dec 1, 2017 at 3:45
  • Re Out of date answers: I got this answer below to work, sending from a gmail account, after enabling "Less secure app access" in gmail settings, in 2020.
    – Ryan1729
    Oct 17, 2020 at 22:53

16 Answers 16

176
  1. Install ssmtp Install ssmtp:

    sudo apt-get install ssmtp
    
  2. Edit the ssmtp config file:

    gksu gedit /etc/ssmtp/ssmtp.conf
    
  3. Append the following text:

    [email protected]
    mailhub=smtp.gmail.com:465
    rewriteDomain=gmail.com
    AuthUser=username
    AuthPass=password
    FromLineOverride=YES
    UseTLS=YES
    
  4. Run ssmtp and provide the recipient email address:

    ssmtp [email protected]
    
  5. Provide the message details as follows:

    To: [email protected]
    From: [email protected]
    Subject: Sent from a terminal!
    
    Your content goes here. Lorem ipsum dolor sit amet, consectetur adipisicing.
    (Notice the blank space between the subject and the body.)
    
  6. Press Ctrl + D to send.


You can also put the text in file and send it as follows:

ssmtp [email protected] < filename.txt
11
  • 38
    It feels wierd leaving my email password exposed in some file on the computer. Is this safe?
    – oadams
    Nov 13, 2010 at 2:50
  • 2
    @oadams not very safe. Use 2-step verification to lower your risk, or use a mail gateway that doesn't require a password, like your ISP's.
    – itsadok
    Mar 25, 2012 at 13:54
  • 9
    Don't install ssmtp on a server with postfix installed. Postfix will be removed. Instead, just use sendmail [email protected] < file.txt which will work with either postfix or ssmtp. Dec 9, 2012 at 22:05
  • 2
    Another weird thing that I wanted to share here is, it didn't worked for me when I had a long/complex password but worked when I modified it to a simple password, strange but true. I also heard the same from someone else but tried it after lots of unsuccessful attempts to send mail & as soon as I simplified the password, to my surprise, it just worked ;) Mar 1, 2014 at 12:48
  • If Gmail doesn't allow your server to access, login to Gmail on the server with command line browser. askubuntu.com/questions/460022/using-terminal-as-a-web-browser Dec 28, 2014 at 14:55
105

Most of the time you shouldn't need to configure an SMTP server you can simply use mail from the commandline (if it's not already present, install with sudo apt-get install mailutils). (Or if you're on a server where sendmail is configured, etc)

marco@dagobah:~$ mail -v [email protected]
Subject: Hello World!
This is an email to myself.

Hope all is well.
.
Cc: 

You terminate messages with a single . on line. That's when mail will prompt you for Cc: enter the information (or leave blank) and mail will then print out additional information on what it is attempting to do, as well as detailing the processing of connecting, transmitting, and receiving data from the mail server.

11
  • 32
    I would append the answer to include "sudo apt-get install mailutils" since it is not present on a clean Ubuntu 10.04 install. Also, terminating the message with a . on a single line didn't work. I had to "Ctrl-D" instead. Lastly, the message didn't go through! Nov 13, 2010 at 20:40
  • 8
    @OlivierLalonde sudo apt-get install mailutils will install a SMTP server which is postfix, which might be a little overweighted for some people.
    – vaab
    Oct 4, 2013 at 7:47
  • 7
    mailutils will also install mysql!
    – chmike
    Apr 24, 2014 at 9:16
  • 20
    -v option not in my install of mail 2.99.98, also had to use Ctrl+D to end
    – markmnl
    May 17, 2014 at 17:53
  • 2
    Please update this question, it is outdated.
    – phil294
    May 18, 2017 at 15:55
59
apt-get install sendemail

usage:

sendemail -f [email protected] -t [email protected] -u subject -m "message" -s smtp.gmail.com:587 -o tls=yes -xu [email protected] -xp gmailpassword 

If you don't want to specify your password in the command line (generally not a good thing to do), you can omit that parameter and sendemail will prompt you for the password... and display it on the screen, but at least it won't be in your command line history.

6
  • 1
    Sending mail from a GMail account using sendmail most certainty very handy. Thank you. Mar 5, 2013 at 17:09
  • Find a patch here not to echo the password.
    – Arun
    Sep 9, 2015 at 16:13
  • I had to specify the port with -s smtp.gmail.com:587.
    – Joe Mornin
    Mar 31, 2016 at 23:19
  • 11
    add a space before a command line to not save it in the command line history
    – guhur
    Jan 5, 2017 at 18:03
  • another way to not display it on the bash screen: save password to a file, then from file to environment variable, ex: pass=$(cat my_password); sendemail... -xp $name ... and of course if you're calling it programmatically you can do something similar, ex: ruby system("sendemail ... -xp #{File.read 'my_password'}...") also it might fail the first time, you should get an email to that account suggesting you "enable less secure apps" then it can work.
    – rogerdpack
    Apr 25, 2017 at 21:34
39

Try to install The Mutt E-mail Client. Other option is using emacs with gnus. Others options available too... IMHO, you should use more details in your questions, or several different answers to your question you will receive :-)

2
  • 2
    Accepted this one since it doesn't require any configuration and is quite user-friendly. Nov 13, 2010 at 21:00
  • 22
    wait. mutt - user friendly? the craziest one-liner I've read today :)
    – Stann
    Apr 21, 2011 at 22:19
26

You need an MTA to send mail. For this, use postfix:

sudo apt-get install postfix

To send email:

echo "test message" | mailx -s 'test subject' [email protected]

HELP

5
  • You also need to install mailutils for mailx, right?
    – Nick
    Jul 15, 2014 at 5:26
  • 6
    I wish it were really this simple, but unless you're someone very special, this isn't going to work. 99.9999% of ISPs will ignore mail from private postfix servers, because 9.99999 times out of 10 they're spammers.
    – Cerin
    Apr 13, 2015 at 1:32
  • this didn't work for me, it asked to install mailutils when I entered this into the terminal Feb 14, 2016 at 5:59
  • 2
    Go ahead and install mailutils. You don't need postfix IIRC.
    – uav
    Jul 19, 2018 at 13:51
  • 1
    @Cerin is right. I needed very simple mailing functionality for a cron job though. As a workaround, if you're receiving via a gmail account, you can set up a filter for the email (via keywords or the from address) and gmail will allow it to be sent. Dec 3, 2018 at 21:59
23

mpack is excellent commandline way of sending file attachments.

apt-get install mpack

usage:

mpack -s "file you wanted" ./data.pdf [email protected]
16

Install the package sendmail then type

sendmail -t receiver@example 

then write your email then press Ctrl+D

11
mail -s "subjet" -a "attchedfile_name" someone@dest_email.com

or

cat "afile" | mail -s "subject" someone@dest_email.com
2
  • just to add for easy testing: echo "Hello world!" | mail -s "Hello" [email protected]
    – Bohne
    Sep 1, 2015 at 12:54
  • 4
    If someone doesn't have the mail command, just run: sudo apt-get install mailutils in Ubuntu/Debian or yum install mailx in CentOS/Redhat Sep 22, 2016 at 13:10
10

You can send an email from the command line with TelNet or NetCat.

Everything is explained here.

hanoo@hp_laptop% nc 127.0.0.1 25
220 hp_laptop.localdomain ESMTP Postfix
EHLO man
250 hp_laptop.localdomain
MAIL FROM: <[email protected]>
250 2.1.0 Ok
RCPT TO: <[email protected]>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
This is the body of my mail,
this is the second line...
.
250 2.0.0 Ok: queued as 9C12E7F404
3
  • 2
    It's a little hard to use SSL when using this method ;)
    – jacobq
    Dec 30, 2012 at 23:09
  • 1
    @iX3 just use swaks and you're fine with SSL/TLS, too ;-)
    – Germar
    Jun 22, 2015 at 23:28
  • Or openssl s_client -starttls smtp ...
    – Raman
    Mar 7, 2017 at 16:54
7

If you try to send e-mail from a system, whitch does not run an own e-mail-server (i. e. desktop system), you need to install something like nullmailer or esmtp, which forward your local mail to a "real" mail server.

As command line tools you can install mail or mailx (packages mailutils, heirloom-mailx or bsd-mailx). If you need attachments try biabam.

1
  • 1
    I like bsd-mailx for the -E command and cronjobs May 10, 2015 at 5:46
6
sudo apt-get install sharutils mailutils
uuencode filename filename | mail [email protected]

where filename is the same: it stands for input file and remote file.

2
  • sudo apt-get install sharutils for uuencode Oct 19, 2012 at 22:53
  • 3
    mailutils installs mysql! oO
    – chmike
    Apr 24, 2014 at 9:15
5

I have never tried it but there is a mail command that can send mail. See man mail.

To test local email:

echo message | mail username@localhost
2

You can try this:

mail [email protected] -s "Attached file" <<EOF                 
Hi

~| uuencode $HOME/filename.txt filename.txt

EOF

It works with GNU Mailutils, check the website for more information.

2

Run:

sudo apt-get install ssmtp
sudo -H gedit /etc/ssmtp/ssmtp.conf

The following needs to be added there:

# The user that gets all the mails (UID < 1000, usually the admin)
[email protected]

# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also https://support.google.com/mail/answer/78799
mailhub=smtp.gmail.com:587

# The address where the mail appears to come from for user authentication.
rewriteDomain=gmail.com

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
AuthUser=yourusernameofgmail
AuthPass=yourGmailPassowrd
AuthMethod=LOGIN

# Email 'From header's can override the default domain?
FromLineOverride=yes

Run:

sudo -H gedit /etc/ssmtp/revaliases

Enter there:

root:[email protected]:smtp.gmail.com:587

Enable "less secure apps" on Gmail:
https://support.google.com/accounts/answer/6010255?hl=en

Test it by running the following on terminal:

echo "Body of mail is abc" | mail -s "Subject is xyz" "[email protected]"`
2

You can use cURL. Take a file like this:

From: Sunday <[email protected]>
To: Monday <[email protected]>
Subject: Tuesday

Wednesday

and send it:

curl \
--netrc \
--mail-rcpt [email protected] \
--upload-file a.txt \
smtps://smtp.gmail.com

https://myaccount.google.com/lesssecureapps

1

I want to add another quite simple yet interesting way to do, provided by AWS (link)

So, you need to prepare this text file, save it as input.txt. Please remember to change the values:

  • Replace example.com with your sending domain.

  • Replace Base64EncodedSMTPUserName with your base64-encoded SMTP username.

  • Replace Base64EncodedSMTPPassword with your base64-encoded SMTP password.

  • Replace [email protected] with the email address you are sending from. This identity must be verified.

  • Replace [email protected] with the destination email address. If your Amazon SES account is still in the sandbox, this address must be verified.

    EHLO example.com
    AUTH LOGIN
    Base64EncodedSMTPUserName
    Base64EncodedSMTPPassword
    MAIL FROM: [email protected]
    RCPT TO: [email protected]
    DATA
    From: Sender Name <[email protected]>
    To: [email protected]
    Subject: Amazon SES SMTP Test
    
    This message was sent using the Amazon SES SMTP interface.
    .
    QUIT
    
  • To send using explicit SSL over port 587 – Enter the following command:

    openssl s_client -crlf -quiet -starttls smtp -connect smtp-server-endpoint:587 < input.txt
    
  • To send using implicit SSL over port 465 – Enter the following command:

    openssl s_client -crlf -quiet -connect smtp-server-endpoint:465 < input.txt
    

Impatient SMTP servers

If you face Client host rejected: Improper use of SMTP command pipelining errors, try waiting for the responses:

openssl s_client -crlf -quiet -starttls smtp -connect smtp.example.com:587 < \
    <(
        echo "EHLO foo.tld"
        sleep 2
        echo "AUTH LOGIN"
        sleep 2
        echo "Base64EncodedSMTPUserName"
        sleep 2
        echo "Base64EncodedSMTPPassword"
        sleep 2
        echo "MAIL FROM: [email protected]"
        sleep 2
        echo "RCPT TO: [email protected]"
        sleep 2
        echo "DATA"
        sleep 2
        echo "Sender Name <[email protected]>"
        echo "[email protected]"
        echo "Subject: Test"
        echo ""
        echo "Lorem Ipsum"
        echo "."
        echo "QUIT"
    )

You must log in to answer this question.

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