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.
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.txtSubject: Terminal Email Send Email Content line 1 Email Content line 2
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
-sis 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
-ais 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.
telnet localhost smtpTrying 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.com250 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 data354 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 quit221 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.
You can send a file from bash using
MESSAGE=”This is a test email”
mail -s “$SUBJECT” -r “$FROM” -a /here/put/a/file_path “$TO” <<< $MESSAGE
Can’t believe that cURL is not mentioned here :O
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.
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
unix command to send email with attachment and email subject also to be fetched from a text/flat file
can you please help
-a ist wrong, -A (uppercase!) is correct if you want to attach a file
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.
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.
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 188.8.131.52
[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]
The last line (NOT FOUND) wasn’t part of the Commandline text
Using telnet to send email is incredibly fustrating. SMTP servers are not made for humans. They always timeout before you’re done.
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?
#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.
I tried all 5 of these methods to send email from Linux command line. None of them work.
All of these work Sam. Perhaps you need to check your permissions. You should definitely be able to use the Telnet option.
same happening with me. none worked. can you tell me if you found any solution?
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.
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!
Hi, how can use the command “mail” to make him ask the “to” parameter also?
to: [email protected]
subject: just a test
hello word email
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.
Thanks very much. You are a real life-saver.
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
I try 1 to 4 to send mail to @live.com. Not work.
The mutt command is not entirely correct.
-a […] — attach file(s) to the message
the list of files must be terminated with the “–” sequence
mutt -s “Test Email” -a /opt/backup.sql — [email protected] < /dev/null
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.
Can anyone help me like how to configure send mail server on my system . I have installed Solaris 10 on my system .
Please help .
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 .
whats the Linux command to disable mail forwarding from my domain, which are going into my yahoo mail account?
There are a few other options too:
Swaks – Swiss Army Knife for SMTP
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?
newbie, I believe the answer to your question is the tee command. check it out:
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.
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.
Use Other way:
Type in Terminal
>mail [email protected]
Then it’ll shows
Have to fill subject!!!
Finally finished as (.)dot
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.
Very nice and useful commands.