mails play a crucial role in modern communication, and while services like Gmail, Yahoo, and RediffMail offer user-friendly web interfaces, there are times when sending emails directly from the command line is necessary. This guide introduces various methods to send emails from the Linux command line, ideal for integrating into shell scripts, cron jobs, and more.

Advertisement

email-banner

Here, we explore several command-line methods for sending emails, focusing on the most popular choices among users. Choose any of the following methods to efficiently send emails from the Linux command line.


1. Using ‘sendmail’ Command

Sendmail, a widely used SMTP server in Linux/Unix systems, allows email sending via the command line. Follow these instructions to use the ‘sendmail’ command:

First, create a file with the email content:

cat /tmp/email.txt 

Subject: Terminal Email Send

Email Content line 1
Email Content line 2

The Subject line will be used as subject for the email.

Now send email using the following command.

sendmail user@example.com  < /tmp/email.txt 

Read more: Install and Configure Sendmail on CentOS/RHEL


2. Using ‘mail’ Command

The ‘mail’ command is a popular choice for sending emails from the Linux terminal. Here are some examples:

mail -s "Test Subject" user@example.com < /dev/null 
  • -s is used for defining subject for email.

Also, you can send an attachment with this command. Use -a for mailx and -A for mailutils.

mail -a /opt/backup.sql -s "Backup File" user@example.com 
  • Here -a is used for attachments. Use -A for debian based systems, which uses mailutils package.

You may face issue: Bash: mail: command not found

Also, we can add comma separated emails to send the email to multiple recipients together.

mail -s "Test Email"  user@example.com,user2@example.com < /dev/null 

3. Using 'mutt' command

Mutt is basically used for reading emails from Linux terminal from local user mailboxes, also useful to read emails from POP/IMAP servers. Mutt command is little similar to mail command. Use few of below examples to send an email.

mutt -s "Test Email" user@example.com 

Send an email including an attachment

mutt  -s "Test Email" -a /opt/backup.sql user@example.com < /dev/null 

4. Using 'SSMTP' Command

sSMTP allows users to send emails from SMTP server from Linux command line. For example to send an email to user admin@example.com use following command. Now type your subject of the email as below with keyword Subject. After that type your message to be sent to the user, After finishing your message press CTRL+d (^d) to send the email.

ssmtp admin@example.com 
Subject: Test SSMTP Email
Email send test using SSMTP
via SMTP server.
^d

Read more: How to Setup SSMTP Server on Linux


5. Using 'telnet' Command

As per my experience, all system administrators use telnet command to test remote port connectivity test or login to the server remotely. Most of the newbie in Linux doesn't know that we can send email using telnet also, which is the better way to troubleshoot email sending problems. Below is an example of email sending.

Red marked text is the user input and remaining is the responses of that commands.

telnet localhost smtp


Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 fbreveal.com ESMTP Sendmail 8.13.8/8.13.8; Tue, 22 Oct 2013 05:05:59 -0400
HELO yahoo.com
250 tecadmin.net Hello tecadmin.net [127.0.0.1], pleased to meet you
mail from: sender@tecadmin.net
250 2.1.0 sender@tecadmin.net... Sender ok
rcpt to: myemail@ymail.com
250 2.1.5 myemail@ymail.com... Recipient ok
data
354 Enter mail, end with "." on a line by itself
Hey
This is test email only

Thanks
.
250 2.0.0 r9M95xgc014513 Message accepted for delivery
quit
221 2.0.0 fbreveal.com closing connection
Connection closed by foreign host.

Bonus Tips - Attach a file to Email from Shell

You can also send email with a attachment from Linux shell. Use -A option to provide a file path with mail command.

mail -s "Test Mail" -r mail-from@example.net -A attach.zip mail-to@example.com 

Conclusion

Sending emails from the Linux command line can be a huge time-saver for those who work extensively in terminal environments. Whether it's through simple commands like mail and mutt, or more complex methods using sendmail or telnet, there's a solution that fits every need. Mastering these methods can significantly enhance your efficiency and broaden your command-line skills.

Share.

37 Comments

  1. You can send a file from bash using

    #!/bin/bash

    SUBJECT=”Test”
    FROM=”anymail@something.com”
    TO=”anymail@something.com”
    MESSAGE=”This is a test email”

    mail -s “$SUBJECT” -r “$FROM” -a /here/put/a/file_path “$TO” <<< $MESSAGE

  2. I have a .txt file which contains the email id of users. I want to send an email to all the users keeping them in bcc and in TO i need my admin id only which will be visible to all instead of everyone’s id visible to everyone. I tried BCC(-b) with mailx but it is not working as expected. Only the first person is getting the mail and he is in BCC which is correct but rest are not getting the email.

  3. Use bulk-mail CLI, an efficient and powerful tool to send dynamic emails to a mailing list by just one easy command: `bulkmail mail`!

    Do quick, mini, hassle-free email marketing with this small but Powerful tool ? https://bulkmail.now.sh

  4. unix command to send email with attachment and email subject also to be fetched from a text/flat file
    can you please help

    • Basically, mail command is available multiple packages and some differences. Debian based systems use mailutils and Redhat based systems use mailx as default package for mail.

      -A is used with mail command installed via mailutils package.

      -a is used with mail command installed via mailx package.

  5. Upendra Naidu on

    Please help me. Installed mantis in my cent os 6 .5 but if i raised the ticket mails are not working…Can you please guys help me.

  6. ERROR

    login as: root
    root@188.166.137.66‘s password:
    Welcome to Ubuntu 15.10 (GNU/Linux 4.2.0-27-generic x86_64)

    * Documentation: https://help.ubuntu.com/
    You have new mail.
    Last login: Mon May 1 02:23:55 2017 from 41.138.220.242
    root@ubuntu-1gb-lon1-01:~# mail -s “Test Email Using CMD” sembob2000@yahoo.com,sembobic@gmail.com < /dev/null
    /usr/bin/mail: 1: /usr/bin/mail: /usr/bin/mhparam: not found
    Usage: mhmail [-t(o)] addrs … [switches]
    root@ubuntu-1gb-lon1-01:~#

    NOT FOUND

  7. Using telnet to send email is incredibly fustrating. SMTP servers are not made for humans. They always timeout before you’re done.

  8. psemail -subject “$Steps” -body “$body1 $body2” -from “$From_Addr” to “To_Addr”

    I need the $body2 to be printed in next line of the $body 1 in email.Is there any command for this?

  9. #2 leaves out a critical step: When you are done with your message hit CTRL+D. That is how the command knows to send. Otherwise it keeps waiting for you to add more to the body.

    • All of these work Sam. Perhaps you need to check your permissions. You should definitely be able to use the Telnet option.

    • Hi,

      You should first check if the rpms- sendmail , mail are installed on the hosts if you are running from Red-hat/Linux hosts. you can query(yum list mail or yum list sendmail) to see if they are installed or not, if not then install them first then try again.
      I tried both the options and worked successfully.

    • notabletomail on

      Yup. None of this works even on Debian 9. It used to be so simple to send mail via command line in Linux. Also, how to specify the smtp server?? None of the newer send mail commands seem to allow this anymore. I know busybox has a sendmail command that works great on embedded systems, but on my Debian distro they took out the sendmail command from busybox!

  10. Hello ,

    I want to configure sendmail in my linux host and send email from linux host to my microsoft outlook email id. I tried few configurations but all are not working.

    Can you please help me by providing detail steps to do the configuration and send email from linux machine.

  11. Hello, which version of mail accepts attachments from command line? -a option on the version I have on my linux machine is for appending a header:
    -a, –append=HEADER: VALUE append given header to the message being sent

    Thanks,
    Frank

  12. The mutt command is not entirely correct.

    -a […] — attach file(s) to the message
    the list of files must be terminated with the “–” sequence

    So

    mutt -s “Test Email” -a /opt/backup.sql — user@example.com < /dev/null

  13. I know you post the knowledge of the manuals.
    Don’t forget that sometimes the method exposed on the help is not the best.

    You example of sendmail needs a temp file to load the content using <.
    Instead of that, use the sendmail the same way that you use mail. The problem of doing that is that you have to choose SUBJECT or content.

    To be able to send both, you need to write out 1 line for SUBJECT and more lines to BODY.

    A quick example of notifier without message body
    echo "SUBJECT:task X is running" | sendmail email@domain.com

    For anyone that can choose sendmail and mail, mail is better. Im sorry but is the truth, its more flexible.

    Best regards and thx for that post.

  14. Hi ,
    Can anyone help me like how to configure send mail server on my system . I have installed Solaris 10 on my system .

    Please help .

    Thanks

  15. Hi ,
    Can you please help me , like how to configure sendmail for sending mails , I have installed Solaris 10 on.my system . I need help to configure send mail server .

    Please help.

    Thanks

  16. I need to run a command and the result will display on the screen. How do I run the command and get the output to be sent to my email as well?

  17. No, Kevin.

    The Telnet example does not assume an MTA is installed and could be run from the linux command line of something like a Samsung phone.

  18. All of these examples assume that an MTA is installed on localhost. That’s not something I’d assume, and is a less than desirable/optimal situation in many cases.

  19. Imesh Chandrasiri on

    Hi,

    I tried the 5th way to send an email. But I got the following error.

    “Connection closed by foreign host”

    Could you please let me know how to avoid this.

Leave A Reply

Exit mobile version