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 the ways to send emails from Linux command line. This is useful for sending email through our shell scripts, cron jobs 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.
Create a file using following content.
[[email protected] ~]# cat /tmp/email.txt Subject: Terminal Email Send Email Content line 1 Email Content line 2
Now send email using the following command.
[[email protected] ~]# 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.
[[email protected] ~]# mail -s "Test Subject" [email protected] < /dev/null
Also, you can send the email with an attachment.
# mail -a /opt/backup.sql -s "Backup File" [email protected] < /dev/null
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 mails from Linux terminal from local user mail boxes, also useful to read emails from POP/IMAP servers. Mutt command is little similar to mail command. Use few of below examples to send email.
# mutt -s "Test Email" [email protected] < /dev/null
Send email including 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 my experience all system administrators use telnet command to test remote port connectivity test or login to server remotely. Most of newbie in Linux doesn't know that we can send email using telnet also, which is better way to trouble shoot email sending problems. Below is an example of email sending.
#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 -0400HELO yahoo.com 250 tecadmin.net Hello tecadmin.net [127.0.0.1], pleased to meet youmail from: [email protected] 250 2.1.0 [email protected] Sender okrcpt to: [email protected] 250 2.1.5 [email protected] Recipient okdata 354 Enter mail, end with "." on a line by itselfHey This is test email only Thanks . 250 2.0.0 r9M95xgc014513 Message accepted for deliveryquit 221 2.0.0 fbreveal.com closing connection Connection closed by foreign host.
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.



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.
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
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?
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.
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
.
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.
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
Thanks,
Frank
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
So
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.
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
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
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
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/
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:
http://linux.101hacks.com/unix/tee-command-examples/
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.
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
>Subject:
Have to fill subject!!!
Then Message,
Finally finished as (.)dot
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.
Very nice and useful commands.