Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we’ll be looking at ways of sending emails from the terminal in Linux. We’ll start by going through the email system with a high-level overview. We’ll then learn how we can send emails from the terminal by relaying our emails to a public SMTP server. Finally, we’ll see how email clients allow us to send emails with an attachment.

2. Architecture of an Email System

Before we begin, it is essential to have a good grasp of the email system’s architecture. In particular, we’ll try to understand what components work together to facilitate the sending of email across the internet.

A typical email system consists of the following components Mail User Agent (MUA), Mail Delivery Agent (MDA), and Mail Transport Agent (MTA).

2.1. Mail User Agent (MUA)

The MUA is more commonly known as the email client. It is the user-facing piece of the email system. Usually, when we’re composing or reading emails, we’re interacting with the MUA. Some commonly known MUA include Outlook, Gmail, and mutt in Linux.

2.2. Mail Delivery Agent (MDA)

MDA is the component that is responsible for accepting the email from an MTA and saving it in the recipient mailbox. It talks to the MUA using IMAP or POP3.

2.3. Mail Transport Agent (MTA)

The MTA is the component that does the email forwarding and routing using the headers on the email. It is also known as the SMTP server as it communicates with other components in the SMTP. Its role in the email system includes (but is not limited to):

  • Accepting emails from other MTA
  • Forwarding the email to the appropriate MTA for further routing
  • Passing the email to the MDA

3. msmtp

msmtp is a simple SMTP client that allows us to forward a body of text as an email to other SMTP servers. In this tutorial, instead of setting up our own MTA server, we’ll be sending emails from the terminal using Outlook’s MTA with msmtp.

3.1. Installing msmtp

Let’s install msmtp with the package manager tool:

$ sudo apt-get install msmtp

3.2. Configuring msmtp

To configure msmtp, let’s create a file .msmtprc in the current user home directory:

$ cat - <<EOF > ~/.msmtprc
# Default settings
defaults
auth    on
tls    on
tls_trust_file    /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
EOF

The command creates a file .msmtprc in the current user home directory with some default settings that serve as the global settings.

Firstly, the defaults directive indicates that the configuration lines that follow should be applied to other accounts in .msmtprc. By setting the auth field, we’ve enabled authentication with the SMTP servers.

Then, we enable TLS for msmtp by setting tls to on, specifying the path to the certificate using tls_trust_file. Additionally, we specify the path for msmtp log file using the field logfile.

Finally, we’ll set the permission of that file to 600 using chmod:

$ chmod 600 ~/.msmtprc

3.3. Adding SMTP Server Account

Before we can send an email, we’ll need to configure accounts in the .msmtprc configuration file. These accounts are the public SMTP services we’ll be using to send our email. In this article, we’ll show as an example the configuration of the Outlook SMTP service. The same configuration is also applicable to other SMTP services.

For this demonstration, let’s assume that we’ve registered ourselves an email account with Outlook.com with the following credentials:

Let’s configure the Outlook SMTP service by adding a few more configuration lines to .msmtprc. In the end, our .msmtprc file should look like the following:

$ cat ~/.msmtprc
# Default settings
defaults
auth    on
tls    on
tls_trust_file    /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtsp.log
# outlook
account    outlook
host       smtp.office.365.com
port       587
from       [email protected]
user       [email protected]
password   baeldung-linux

account default : outlook

Firstly, the account specifies the name to identify a particular SMTP server configuration. Then, we’ll specify the host and port for the SMTP servers. The host and port for any public SMTP services are easily obtainable with a quick search online.

Additionally, we put our credentials in the field user and passwordThe field user and from has the same value, and it should be our email address that we registered with a particular email service provider.

Finally, we’ve also specified a default account that’ll be used whenever we send an email using msmtp.

Sometimes Outlook sends us a verification email, demanding verification from the account owner before that account can send any emails. We can validate our account by following the instructions sent by Outlook.com to our mailbox.

3.4. Sending a Simple Email From Linux Terminal

Let’s now try to send an email using msmtp:

$ echo -e "\nSending regards from Terminal." | msmtp -a outlook [email protected]

In the command above, we pipe the output of the email body to the msmtp command. Then, we specify that we’ll send with the outlook account using the flag -a. This is the account that we’ve set up in the .msmtprc configuration file. Finally, the recipient’s email address is specified as the last argument.

We’ve also added a new line character “\n” before the email body to separate the email body from its header. Without that new line character, the email body will not be displayed in the recipient email client.

Once we hit enter, the email will be sent to [email protected] by the Outlook SMTP server.

3.5. Adding Subject to Email

To add a subject to our email, we can specify the email subject:

$ echo -e "Subject: Regards\n\nSending regards from Terminal." | msmtp -a outlook [email protected]

In the command, we’re manually adding the email’s subject header as Subject: Regards. Then, we add two newline characters after that to separate the subject header from the email body.

3.6. Sending HTML Mail

To send HTML mail, we need to create a sample.html file and include headers that’ll interpret the input as an HTML file.

Let’s create the sample.html file with the Nano editor:

$ nano sample.html

Then we can add this content:

From: [email protected]
To: [email protected]
Subject: This is the Subject
Mime-Version: 1.0
Content-Type: text/html

<html>
  <head>This is Email Head</head>
  <body>
    <h2>This is the Main Title</h2>
    <p>This is the body text</p> 
  </body>
</html>

Let’s send this sample.html file as an HTML mail:

$ cat sample.html | msmtp [email protected]

Here, the cat command outputs the contents of the sample.html file, then we’re piping the result to the msmtp command.

We’re using Content-Type as a MIME header so that the file is processed as an HTML file. We should note that we need to specify the email header information in the HTML file to process it with msmtp.

4. mutt

In the previous section, we’ve seen how we can send an email from a Linux terminal using msmtp. While it works for simple email, we can do much more using an MUA. For instance, using msmtp alone, we can’t send attachments along with our email.

Let’s take a look at the mutt command, an MUA in Linux that can be used as a command-line tool.

4.1. Installing mutt

To install mutt, simply run the following command:

$ apt-get install mutt

4.2. Configuring mutt

Next, we’ll configure mutt to send emails using the previously configured msmtp. To create a configuration file .muttrc in the current user home directory, we’ll run the following command:

$ cat <<EOF > ~/.muttrc
set sendmail="/usr/bin/msmtp"
set use_from=yes
set [email protected]

Firstly, with the line set sendmail=/usr/bin/msmtpmutt will send email using the msmtp command. Next, we’ve also set use_from to true and set the from variable to our email address. This value must be the same as the email address we configured in msmtp. If the email varies, the server will reject delivery for that email.

4.3. Sending an Email With Attachments

Let’s create a dummy attachment file log.txt to demonstrate the process of sending an attachment in an email:

$ echo "Some log event" > log.txt

To send the log.txt as an attachment, we can run the mutt command as such:

$ echo "This is the email body" | mutt -a "./log.txt" -s "Email Subject" -- [email protected]

Firstly, the flag -a specifies the path for the attachment. Then, we specify the subject of the email as Email Subject using the flag -s. Finally, we pass the recipient’s email address as the final argument.

Note that the double dash “–” must precede the recipient’s email address to prevent the value from being consumed by the -a flag.

4.4. Sending HTML Mail

Let’s create a dummy HTML file sample.html to demonstrate the process of sending an HTML mail:

$ nano sample.html

Then, we can paste in this content:

<html>
  <head>This is Email Head</head>
    <body>
      <h2>This is the Main Title</h2>
      <p>This is the body text</p>
    </body>
</html>

Now let’s send an HTML mail using this sample.html file:

$ mutt -e "set Content-Type: text/html" [email protected] -s "Email Subject" < sample.html

We’re using Content-Type as a MIME header to instruct mutt to process the input file as an HTML document.

We need to ensure we have mutt version 1.5.x or later installed since Content-Type support wasn’t available in previous versions.

5. Conclusion

In this tutorial, we’ve briefly looked through the basic email system architecture. Then, we’ve introduced msmtp, a simple SMTP client that allows us to send emails to other MTA, using Outlook as an example. Finally, we took a step further and demonstrated how to send an email with an attachment using the email client mutt.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!