This articles provides you the ways to send email from Linux command line. This is useful for sending email through our shell scripts, cron jobs etc.
1. Using ‘sendmail’ Command
Sendmail is a most popular smtp server used in most of Linux distribution. Use below instructions to send email using ‘sendmail’ command.
Create a file using following content.
# vim /tmp/email.txt
Subject: Terminal Email Send Email Content line 1 Email Content line 2
Subject: line read as Subject of email.
Now send email using following command.
# sendmail email@example.com < /tmp/email.txt
2. Using ‘mail’ Command
mail command is most popular command to send emails from Linux terminal. Use few of below examples tosend email.
# mail -s "Test Email" firstname.lastname@example.org < /dev/null -s is used for subject
Send email with attachment.
# mail -a /opt/backup.sql -s "Backup File" email@example.com < /dev/null -a is used for attachments
Add comma separated recipients to send email to multiple recipients.
# mail -s "Test Email" firstname.lastname@example.org,email@example.com < /dev/null
3. Using ‘mutt’ command
Mutt command is little similar to mail command. Use few of below examples to send email.
# mutt -s "Test Email" firstname.lastname@example.org < /dev/null
Send email including attachment
# mutt -s "Test Email" -a /opt/backup.sql email@example.com < /dev/null
4. Using ‘telnet’ Command
Most of newbie in linux doesn’t know that we can send email using telnet, which is better way to trouble shoot 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: firstname.lastname@example.org 250 2.1.0 email@example.com... Sender ok rcpt to: firstname.lastname@example.org 250 2.1.5 email@example.com... 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.
Note: 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.