Celeb Glow
news | March 28, 2026

How to send mail from the command line?

How to send mail from the command line?

5

16 Answers

  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:

    root=
    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 
  5. Provide the message details as follows:

    To:
    From:
    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 < filename.txt
11

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

usage:

sendemail -f -t -u subject -m "message" -s smtp.gmail.com:587 -o tls=yes -xu -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

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

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' 

HELP

5

mpack is excellent commandline way of sending file attachments.

apt-get install mpack

usage:

mpack -s "file you wanted" ./data.pdf 

Install the package sendmail then type

sendmail -t receiver@example 

then write your email then press Ctrl+D

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

or

cat "afile" | mail -s "subject" someone@dest_email.com
2

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: <>
250 2.1.0 Ok
RCPT TO: <>
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

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
sudo apt-get install sharutils mailutils
uuencode filename filename | mail 

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

2

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

You can try this:

mail -s "Attached file" <<EOF
Hi
~| uuencode $HOME/filename.txt filename.txt
EOF

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

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)
root=
# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also
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::smtp.gmail.com:587

Enable "less secure apps" on Gmail:

Test it by running the following on terminal:

echo "Body of mail is abc" | mail -s "Subject is xyz" ""`

You can use cURL. Take a file like this:

From: Sunday <>
To: Monday <>
Subject: Tuesday
Wednesday

and send it:

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

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 with the email address you are sending from. This identity must be verified.

  • Replace 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:
    RCPT TO:
    DATA
    From: Sender Name <>
    To:
    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: " sleep 2 echo "RCPT TO: " sleep 2 echo "DATA" sleep 2 echo "Sender Name <>" echo "" echo "Subject: Test" echo "" echo "Lorem Ipsum" echo "." echo "QUIT" )