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 on 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 important 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)
- 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 into 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 email from other MTA
- Forwarding the email to the appropriate MTA for further routing
- Passing the email to the MDA
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:
$ 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 for ourselves an email account with Outlook.com with the following credentials:
- email address: [email protected]
- password: baeldung-linux
Let’s configure the Outlook SMTP service by adding 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 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 password. The 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 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\Sending regards from Terminal." | msmtp -a outlook [email protected]
In the command, we’re manually adding the subject header of the email as Subject: Regards. Then, we add two newline characters after that to separate the subject header from the email body.
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 attachment 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/msmtp, mutt 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 an 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 email address as the final argument.
Note that the double dash “–” must precede the recipient email address to prevent the value from being consumed by the -a flag.
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 email 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.