Does your job require sending a lot of emails on a daily basis? And you often wonder if or how you can send email messages from the Linux terminal.
This article explains about 6 different ways of sending emails using the Linux terminal. Let’s go through them.
Use the sendmail command to send emails to one or more people at once. Sendmail is one of the most popular SMTP servers in Linux. You can easily send emails directly from the command line using the sendmail command. To route the information, the sendmail command makes use of the network configured on your system.
Let’s execute the following commands to create a file having email content.
cat /tmp/email.txt Subject: Terminal Email Send Email Content line 1 Email Content line 2
The Subject will be the line used as a subject for the email.
Now, to send the email, use the following syntax.
sendmail firstname.lastname@example.org < /tmp/email.txt
Just like Sendmail, you can use the mail command for sending emails from the terminal. Use the below-given command for this purpose.
mail -s "Test Subject" email@example.com < /dev/null
Here -s defines the email subject.
To send an attachment included within the email, type the below-mentioned line.
mail -a /opt/backup.sql -s "Backup File" firstname.lastname@example.org < /dev/null
Here -a is used to include attachments. If yours is a Debian-based distro, use -A because it uses the mailutils package.
If you have to send emails to multiple recipients at a time, add comma-separated emails in the following manner.
mail -s "Test Email" email@example.com,firstname.lastname@example.org < /dev/null
The GNU Mailutils is a combination of multiple utility packages. All Mailutils can operate on mailboxes starting from UNIX maildrops, maildir, and all the way up to remote mailboxes. These mailboxes are accessed with IMAP4, POP3, and SMTP. Mailutils is made for developers, regular Linux users, and system administrators.
For the installation purpose, use the following command.
sudo apt install mailutils
The mailutils package is mainly made of 2 commands, mail and mailx, and they both function in a similar manner.
echo "message body" | mail -s "subject" email@example.com
Above is the syntax comprising of mail/mailx. This includes the address to send the message to, a suitable subject, and some text body. Usage of the echo command and driving the output to mail command enable you to avoid mailx’s prompts for Cc and the text message body.
The swaks command is a scriptable, flexible, transaction-oriented SMTP tool. SMTP extensions and features handle by this command are authentication, TLS, pipelining, and other versions of SMTP protocols. This Linux command also supports various transport methods, such as UNIX-domain sockets, internet-domain sockets, and drives to spawned processes.
To install this tool, type:
sudo apt install swaks
You need to connect to an SMTP account: the user -au, the server -s, the address -t, and the password -ap. You also need -tls flag in case you connect on 587 port. So the command should be:
swaks --to firstname.lastname@example.org -s smtp.gmail.com:587 -tls -au <user-account> -ap <account-password>
You can send emails using the SMTP server from the Linux terminal using ssmtp.
Use the below-mentioned lines to send emails with ssmtp.
ssmtp email@example.com Subject: Test SSMTP Email Email send test using SSMTP via SMTP server. ^d
Here we are sending an email to user firstname.lastname@example.org. You need to add a subject with the keyword “Subject”. Then you’ll type the message you want to convey to the email recipient. At the end of your message press Ctrl+D (^d) for sending the email.
The mutt command helps send and read emails from your Linux terminal using local user mailboxes. Also, you can read emails using POP/IMAP servers. Mutt resembles Mail command. To send emails, use the following syntax.
mutt -s "Test Email" email@example.com < /dev/null
You can send an email containing a file attachment using the mutt command To do that, use the command as follows.
mutt -s "Test Email" -a /opt/backup.sql firstname.lastname@example.org < /dev/null
You have learned how to send emails using the Linux terminal. Now you can send emails right away from the terminal without having to worry. Practice the methods hands-on given here so that sending emails from the command line becomes easy for you.