Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»5 Ways to Send Email From Linux Command Line

    5 Ways to Send Email From Linux Command Line

    By RahulNovember 23, 20214 Mins Read

    We all know the importance of emails these days for information transfer. There are many free emails service providers which we used for use like Gmail, Yahoo, RediffMail etc, which provides a web interface for sending and receiving emails. But this is not enough, sometimes we also required to send emails from system command line. This tutorial will provide you multiple ways to send emails from the Linux command line. This is useful for sending email through our shell scripts, cronjobs etc.

    Advertisement

    email-banner

    There are various ways to send emails from the command line but here I am sharing few options used by most users. You can use anyone option given below to send email from Linux command line.


    1. Using ‘sendmail’ Command

    Sendmail is a most popular SMTP server used in most of Linux/Unix distribution. Sendmail allows sending email from command line. Use below instructions to send email using ‘sendmail‘ command.

    Created a file with email content:

    cat /tmp/email.txt 
    
    Subject: Terminal Email Send
    
    Email Content line 1
    Email Content line 2
    

    Subject: line will be used as subject for email.

    Now send email using the following command.

    sendmail [email protected]  < /tmp/email.txt 
    

    Read more: Install and Configure Sendmail on CentOS/RHEL


    2. Using ‘mail’ Command

    mail command is most popular command to send emails from Linux terminal. Use few of below examples to send an email.

    mail -s "Test Subject" [email protected] < /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" [email protected] < /dev/null 
    
    • 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"  [email protected],[email protected] < /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" [email protected] < /dev/null 
    

    Send an email including an attachment

    mutt  -s "Test Email" -a /opt/backup.sql [email protected] < /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 [email protected] 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 [email protected] 
    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: [email protected]
    250 2.1.0 [email protected] Sender ok
    rcpt to: [email protected]
    250 2.1.5 [email protected] 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 [email protected] -A attach.zip [email protected] <<< "This is mail body" 
    

    Thank you for using this article. We will add more ways soon with this list. We also request you to help me with more commands which you know and not listed above.

    mail mutt sendmail ssmtp telnet
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    cp Command in Linux (Copy Files Like a Pro)

    dd Command in Linux (Syntax, Options and Use Cases)

    Top 10 JQ Commands Every Linux Developer Should Know

    View 37 Comments

    37 Comments

    1. Fernando on November 19, 2021 11:37 pm

      You can send a file from bash using

      #!/bin/bash

      SUBJECT=”Test”
      FROM=”[email protected]”
      TO=”[email protected]”
      MESSAGE=”This is a test email”

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

      Reply
    2. Lur on January 7, 2021 11:06 am

      Can’t believe that cURL is not mentioned here :O
      https://ec.haxx.se/usingcurl/usingcurl-smtp

      Reply
    3. Alauddin on December 16, 2020 3:57 pm

      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.

      Reply
    4. Pankaj kumar on July 15, 2020 4:45 pm

      great articles!

      Reply
    5. Robert Gosh on April 2, 2019 3:49 pm

      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

      Reply
    6. tester on April 1, 2019 6:19 am

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

      Reply
    7. does not matter on October 8, 2018 7:34 am

      -a ist wrong, -A (uppercase!) is correct if you want to attach a file

      Reply
      • Rahul K. on October 10, 2018 9:50 am

        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.

        Reply
    8. Upendra Naidu on August 3, 2017 6:18 am

      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.

      Reply
    9. Bobic on May 4, 2017 11:04 am

      ERROR

      login as: root
      [email protected]‘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
      [email protected]:~# mail -s “Test Email Using CMD” [email protected],[email protected] < /dev/null
      /usr/bin/mail: 1: /usr/bin/mail: /usr/bin/mhparam: not found
      Usage: mhmail [-t(o)] addrs … [switches]
      [email protected]:~#

      NOT FOUND

      Reply
      • Bobic on May 4, 2017 1:41 pm

        The last line (NOT FOUND) wasn’t part of the Commandline text

        Reply
    10. CG10 on May 27, 2016 2:37 pm

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

      Reply
    11. Shabareesh TP on February 25, 2016 12:59 pm

      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?

      Reply
    12. Matthew on October 16, 2015 8:15 pm

      #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.

      Reply
    13. Sam on September 9, 2015 10:47 pm

      I tried all 5 of these methods to send email from Linux command line. None of them work.

      Reply
      • liam on April 2, 2016 2:54 am

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

        Reply
      • vipul on April 19, 2016 5:56 am

        same happening with me. none worked. can you tell me if you found any solution?

        Reply
      • Shiv on December 3, 2016 5:00 am

        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.

        Reply
      • notabletomail on July 1, 2018 7:03 pm

        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!

        Reply
    14. Alan on August 16, 2015 7:54 am

      Hi, how can use the command “mail” to make him ask the “to” parameter also?

      something like:

      >mail
      to: [email protected]
      subject: just a test

      hello word email
      .

      Reply
    15. alam on August 5, 2015 1:31 pm

      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.

      Reply
    16. Jerome on June 21, 2015 9:56 pm

      Thanks very much. You are a real life-saver.

      Reply
    17. Frank on June 21, 2015 6:30 am

      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

      Reply
    18. john on June 9, 2015 12:09 pm

      I try 1 to 4 to send mail to @live.com. Not work.

      Reply
    19. Richard on June 8, 2015 10:50 am

      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 — [email protected] < /dev/null

      Reply
    20. erm3nda on April 12, 2015 12:12 pm

      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 protected]

      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.

      Reply
    21. Vishal on November 30, 2014 8:05 am

      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

      Reply
    22. Vishal on November 30, 2014 8:03 am

      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

      Reply
    23. Arthur on November 6, 2014 1:14 pm

      whats the Linux command to disable mail forwarding from my domain, which are going into my yahoo mail account?

      Reply
    24. Jason Lewis on October 23, 2014 11:35 pm

      There are a few other options too:
      Swaks – Swiss Army Knife for SMTP
      http://www.jetmore.org/john/code/swaks/index.html
      mailx
      http://linux.die.net/man/1/mailx
      and
      smtp-cli
      http://www.logix.cz/michal/devel/smtp-cli/

      Reply
    25. newbie on September 30, 2014 3:02 am

      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?

      Reply
      • Zeb! on October 23, 2014 1:47 am

        newbie, I believe the answer to your question is the tee command. check it out:

        http://linux.101hacks.com/unix/tee-command-examples/

        Reply
    26. trevor on September 25, 2014 2:03 pm

      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.

      Reply
    27. Kevin on September 19, 2014 10:39 pm

      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.

      Reply
    28. PRABU on September 8, 2014 9:43 am

      Use Other way:

      Type in Terminal
      >mail [email protected]

      Then it’ll shows

      >Subject:

      Have to fill subject!!!

      Then Message,
      Finally finished as (.)dot

      Reply
    29. Imesh Chandrasiri on August 25, 2014 10:09 am

      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.

      Reply
    30. Shashidhar H N on May 7, 2014 10:20 am

      Very nice and useful commands.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.