We all know the importance of emails these days for the transfer of information. There are many free email service providers that we use for your use, such as Gmail, Yahoo, RediffMail, etc., which provides a web interface for sending and receiving emails. But this is not enough, sometimes it is also required to send emails from the system command line. This tutorial will provide you with several ways to send emails from the Linux command line. This is useful for sending email through our shell scripts, cronjobs, etc.
There are several ways to send emails from the command line, but here I am sharing some options used by most users. You can use any of the following options to send email from the Linux command line.
1. Using the ‘
sendmail’ command Sendmail
is the most popular SMTP server used in most Linux/Unix distributions. Sendmail allows you to send email from the command line. Use the instructions below to send emails using the ‘sendmail‘ command.
A file has been created with email content
:cat /tmp/email.txt Subject: Terminal Email Send email Content line 1 Content of the email line 2
Subject: The line will be used as the subject for
Now send email using the following command
. sendmail [email protected] < /tmp/email.txt
Read more: Install and configure Sendmail on CentOS/RHEL
2. Using the ‘mail’ command is
the most popular command for sending emails from the Linux terminal. Use some of the following examples to send an email.
mail -s “Test Subject” [email protected] < /dev/null
- -s is used to define the subject of the email
Additionally, 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 the mailutils package.
You may face a problem: Bash: mail: command not found
In addition, 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 the ‘mutt’ command Mutt
is basically used to read
Linux terminal emails from local user mailboxes, also useful for reading emails from POP/IMAP servers. The Mutt command is little similar to the mail command. Use some of the following examples to send an email.
mutt -s “Test Email” [email protected] < /dev/null
Send an email that includes an attachment
mutt -s “Test Email” -a /opt/backup.sql [email protected] < /dev/null
4. Using the ‘SSMTP’
sSMTP command allows users to send emails from the SMTP server from the Linux command line. For example, to send an email to the user [email protected] use the following command. Now type the subject of the email as shown below with the 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 Send email test using SSMTP through SMTP server. ^d
Read more: How to configure SSMTP server on Linux
the ‘telnet’ command
In my experience, all system administrators use the telnet command to test the remote port connectivity test or log on to the server remotely. Most Linux newbies don’t know that we can also send email using telnet, which is the best way to troubleshoot sending email. Below is an example of sending email.
The text marked in red is the user’s input and the remaining are the responses of those commands.
telnet localhost smtp Trying 127.0.0.1… Connected to localhost.localdomain (127.0.0.1). The 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], nice 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 data 354 Enter mail, end with “.” on one line by yourself Hey This is just test email Thank you. 250 2.0.0 r9M95xgc014513 Message accepted for delivery exit 221 2.0.0 fbreveal.com close connection Connection closed by external host.
Additional tips: Attach
a file to email from the Shell
You can also send email with an attachment from the Linux shell. Use the -A option to provide a file path with the 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’ll be adding more shapes soon with this list. We also ask you to help me with more commands that you know and that are not listed above.