How to send mail from the command line?

  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

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.


apt-get install libio-socket-ssl-perl libnet-ssleay-perl 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.