Linux mail examples : how to send mails from command line?

If you are using Linux open-source operating system and want to use the command line with a simple email utility for your emails, then this article will help you. Adding email forwarding software will enable you to send and receive emails on Linux. This tutorial will teach you how to send emails from the Linux command line, popular examples for sending Linux mail from the command line and why you use it.

What do you use Linux mail for?

The Linux operating system comes with a utility program to manage your emails from the command line user interface. But to use that utility software, you must install a package called “mailutils” you can do that in two ways from sudo apt or install mailutils.

Keep in mind that mailutils lets you connect to a local SMTP (Simple Mail Transfer Protocol) server. After installing mailutils plus mailer packages like Sendmail, you can now send emails to external email clients such as Gmail or Yahoo.com. From the Linux software terminal’s command line, you can create email and send it to others using easy mail commands, check mails, sort mails and delete the mail.

Below are famous examples of ways to send email from the Linux command line you could try.

Linux example mailer packages

1. Making use of the “Sendmail” Command

The Sendmail method is among the most popular application for sending mails in Linux operating system. The reason is Sendmail’s SMTP (Simple Mail Transfer Protocol) server is the most used application for sending mails from the command line.

Follow the below instructions to send your emails from the Sendmail command protocol.

Start by creating an email content file: $cat /tmp/email.txt

Subject: Linux Terminal Email Blast

Email content line 1

Email content line 2

Use the subject line to input the subject of the email $sendmail [email protected]

2. Making use of the “MAIL” Command

Using the MAIL command is the top method for sending mail from the Linux command line. Use any of these examples to send emails to addresses. Send an email with this command: $mail -s ”Test Subject” [email protected]. -s is used to specify the subject of the email.

If you want to send an attachment with the email, use this command. -a for mailx and -A for mailutils. Type $mail -a/opt/backup.sql -sBackup file” [email protected]

In the above program, you use a- for attachments, while for Debian-supported systems that use multiutils software, use -A. To send emails to many receivers jointly, add a comma to separate emails. Use this command line: $email -s “Test Email” [email protected],[email protected]

You might like this article :

What is Linux operating system ?

3. Making use of the “mutt” command

Use mutt to read an email from the Linux terminal from local mail user mailboxes. It is useful for reading emails from POP/IMAP servers. Mutt command is like the mail command. See the examples below for sending emails from the Linux command line. $mutt -s “Test Email” [email protected]. To send an email with an attachment write a $mutt -s “Test Email” a-/opt/backup.sql user” example. com

4. Making use of “SSMTP” Command

This SSMTP, server allows you to send emails from the Linux command line to recipients. Write the following command to send to for instance user [email protected]. Make use of this command line: $ssmtp [email protected]

Next, type the subject of that email with the keyword subject. For instance: Subject: Test SSMTP EMAIL. Then type the message you want to send to that user. At the end of your message, type CTRL+d (^d) to send your message.

5. Making use of “Mailx”

Mailx has two packages that multiple users can use such as basic users, system admin, and developers. The command-line syntax is mail and mailx (working exactly alike and doing the same things). The command contains mail or mailx and the address to send the mail to. However, you will need to add a subject line and text messages.

The command is like this: echo “message body” | mail -s “subject” [email protected]. Using the echo command and piping the output to the mail command, you bypass the Mailx prompts for Cc: addresses and text or message in the email.

To send a message as HTML, you add a command sign <, plus the “appnd flag” of the type of content and provide the email HTML file you want to send. The complete command line code is like this: mailx-a 'Content-Type: text/html'.

Add -s to use the advanced mailx to add many recipients to your email. The command line is like this mailx”[email protected]. You may add a return address with return address=. To add an attachment to a message. You may list many recipients, but it is only possible to send one attachment through mailx.

6. Making use of the “Telnet” command

It is widely known that most system administrators make use of the telnet command line to test remote port connectivity or for remote login to the server. New users of the Linux operating system may not know they can use it to send emails. It is also an excellent method for troubleshooting email sending problems. See an example method below. The items in bold face are the user input and others are responses to commands.

Start with: $telnet localhost smtp

Trying 125.0.0.1...

Connected to localhost. domain

(125.0.0.1...)

The escape case is “^]”.

230 phantom.com ESMTP sendmail

8.12.7/7 12.6; Thur, 28 April 2022

03:03:49 -0700

HELLO Gmail.com

260 whitpageadmin.net Hello whitpage admin.net

\\[125.0.0.1.], pleased to meet you

mail from [email protected]

230 3.1.0 [email protected]...

Sender ok

rcpt to: [email protected]

280 2.1.6 [email protected]

Recipient ok

data

423 Enter mail, end with “.” on a line by itself

Hello

This is only a test email.

Thanks

260 3.0.0 r8M76vyb016524

Message accepted for delivery

quit

230 3.0.0 phantom.com closing connection

Connection closed by foreign host.

 

PagesT