Linux Send eMailThe Linux command line can be very powerful once you know how to use it. You can parse data, monitor processes, automate backups and do a lot of other useful and cool things using it. There often comes a need to generate a report and mail it out. It could be as simple a requirement as a notification that the day’s backup went through fine, or did not.
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, some times we also required to send emails from system command line.
This article will provides you 5 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 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.
Sendmail is a most popular smtp server used in most of Linux/Unix distribution. sendmail allows to send email from command line. Use below instructions to send email using ‘sendmail‘ command.
Create a file using following content:
[root@tecadmin ~]# 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 following command:
[root@tecadmin ~]# sendmail firstname.lastname@example.org < /tmp/email.txt
mail command is most popular command to send emails from Linux terminal. Use few of below examples to send email:
[root@tecadmin ~]# mail -s "Test Subject" email@example.com < /dev/null
-s is used for defining subject for email. To send email with attachment use:
# mail -a /opt/backup.sql -s "Backup File" firstname.lastname@example.org < /dev/null
-a is used for attachments. Also we can add comma separated emails to send email to multiple recipients together.
# mail -s "Test Email" email@example.com,firstname.lastname@example.org < /dev/null
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@example.com < /dev/null
Send email including attachment:
# mutt -s "Test Email" -a /opt/backup.sql firstname.lastname@example.org < /dev/null
sSMTP allows users to send emails from SMTP server from Linux command line. For example to send email to user email@example.com use following command. Now type your subject of email as below with keyword Subject. After that type your message to be sent to user, After finishing your message press CTRL+d (^d) to send email.
# ssmtp firstname.lastname@example.org
Subject: Test SSMTP Email
Email send test using SSMTP
via SMTP server.
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.
Orange marked text is the user input and remaining is the responses of that commands.
# telnet localhost smtp
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
250 tecadmin.net Hello tecadmin.net [127.0.0.1], pleased to meet you
mail from: email@example.com
250 2.1.0 firstname.lastname@example.org... Sender ok
rcpt to: email@example.com
250 2.1.5 firstname.lastname@example.org... Recipient ok
354 Enter mail, end with "." on a line by itself
This is test email only
250 2.0.0 r9M95xgc014513 Message accepted for delivery
221 2.0.0 fbreveal.com closing connection
Connection closed by foreign host.
This is not something interesting and you would not be doing this in a real life scenario. It is just being shown for the sake of it. The mail command can be used to read mails. Just run it without an options and it would list all the mails in your inbox.
Here’s a sample output
Heirloom mailx version 12.5 6/20/10. Type ? for help.
"/var/mail/enlightened": 7 messages 3 unread
O 1 Enlightened Sat Dec 6 11:33 21/658 sendmail Subject
O 2 Enlightened Sat Dec 6 11:34 773/25549 mail Subject
O 3 Enlightened Sat Dec 6 16:43 20/633 sendmail Subject 2
O 4 Enlightened Sat Dec 6 16:44 20/633 SSMTP Subject
U 5 Mail Delivery Syst Sat Dec 6 16:50 74/2425 Undelivered Mail Returned to Sender
U 6 Enlightened Sat Dec 6 16:51 19/632 Mutts subject 1
U 7 Enlightened Sat Dec 6 16:52 19/647 Mutts subject 2
At the end is q question mark which is an interactive prompt waiting for your command. Simply enter the number of the email you want to read and hit enter. It would open up the mail then. After you are done reading the email, enter "q" and hit enter to come back. Enter z and hit enter to bring back the list of emails.
The mail command by default reads the emails from the directory "/var/mail/". So every user has a separate mail directory. This way of storing and fetching mails is not very useful or practical in real life, where mail address consist of domain name along with username and a single server could be hosting emails for multiple domains.