5 Easy Ways to Send An email from the Linux Command Line [ Ultimate Guide ]
Table of Contents
But this is not often enough; we still need to send emails from the system command line on occasion. This guide will show you 5 Ways to Send An email from the Linux Command-Line. This is useful for sending emails from shell scripts, cronjobs, and other automated processes.
There are many ways to send emails from the command line, so I will go through a few that most people use. To send an email from the Linux command line, you can use either of the options mentioned below.
1. Using the Linux Command Line ‘sendmail’
Sendmail is a commonly used SMTP server that is included with almost every Linux/Unix distribution. Sendmail is a Linux command-line program that helps you to send an email. To send an email using the Linux command ‘sendmail‘, follow the steps below.
Create a file with the following email content:
cat /tmp/email.txt Subject: Terminal Email Send Email Content line 1 Email Content line 2
Subject: The subject line will be used as the email’s subject.
Now use the following command to send an email.
sendmail email@example.com < /tmp/email.txt
2. Using the Linux ‘mail’ Command
The most popular command for sending emails from a Linux terminal is mail. To send an email, use a few of the examples below.
mail -s "Test Subject" firstname.lastname@example.org < /dev/null
mail -a /opt/backup.sql -s "Backup File" email@example.com < /dev/null
Attachments are indicated by the
-a suffix. For Debian-based systems that use the mailutils package, use -A.
You might run into the following issue: Bash: mail: command not found We may also use comma-separated emails in order to send the email to several recipients at once.
mail -s “Test Email” firstname.lastname@example.org,email@example.com < /dev/null
3. Using the Linux ‘mutt’ Command
Mutt is mostly used to read emails from local user mailboxes from a Linux terminal, but it can also be used to read emails from POP/IMAP servers. The mutt order is identical to the mail command. To send an email, use a few of the examples below.
mutt -s "Test Email" firstname.lastname@example.org < /dev/null
Send an email with a file attachment.
mutt -s “Test Email” -a /opt/backup.sql email@example.com < /dev/null
4. Using the Linux ‘SSMTP’ Command
sSMTP allows users to send emails from a Linux command line to an SMTP server. Using the following command to send an email to the user firstname.lastname@example.org. Now, enter your email’s subject as seen below, using the keyword Subject. Then type the message you want to send to the recipient. To send the email, click CTRL+d (d) after you have finished your message.
ssmtp email@example.com Subject: Test SSMTP Email Email send test using SSMTP via SMTP server. ^d
5. Using the Linux ‘telnet’ command
All system administrators, in my experience, use the telnet command to test remote port connectivity or log in to the server remotely. Most Linux newbies are unaware that we can also send email via telnet, which is a safer way to troubleshoot email sending issues.
Here’s an example of how to send an email using telnet. The user input is highlighted in red, and the remaining text represents the commands’ responses.
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.
Getting Around: 9 Top Keyboard Shortcuts for Mac
As easy to use as Macs are, there’s always room for improvement in the user-friendly department. Case in point: Mac…