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.
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 [email protected] < /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" [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.
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.
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"
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.
37 Comments
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
Can’t believe that cURL is not mentioned here :O
https://ec.haxx.se/usingcurl/usingcurl-smtp
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.
great articles!
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.
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
root@ubuntu-1gb-lon1-01:~# 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]
root@ubuntu-1gb-lon1-01:~#
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.
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?
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.