In the modern digital era, email communication is essential for both personal and professional interactions.. While graphical email clients are commonly used, the Linux terminal offers a powerful alternative for managing emails efficiently. In this blog post, we will explore how to send emails from the Linux terminal using Outlook, one of the popular email service providers
Prerequisites:
Before diving into the steps, make sure you have the following requirements fulfilled:
- A Linux-based operating system.
msmtp
command-line tool installed.mailx
ormutt
command-line email client installed.- Your Outlook email address and associated SMTP server details.
Step 1: Installing and Configuring msmtp
: To send emails via Outlook, we need to configure the msmtp
tool.
Follow these steps to set it up:
-
- Install
msmtp
by running the appropriate command based on your Linux distribution:-
- Debian/Ubuntu:
sudo apt-get install msmtp
- Debian/Ubuntu:
-
- Fedora/RHEL:
sudo dnf install msmtp
- Fedora/RHEL:
-
- Arch:
sudo pacman -S msmtp
- Arch:
-
- To configure msmtp, let’s create a file .msmtprc in the current user home directory:
- Install
cat - <<EOF > ~/.msmtprc
#Default settings
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
EOF
This command will create a file .msmtprc in user home directory with some default settings that serve as the global settings.
The defaults directive in this config file specifies that the subsequent configuration lines should be applied to all other accounts in .msmtprc. By setting the auth field, we’ve enabled authentication with the SMTP servers.
Next, we set the tls option to on in msmtp to enable TLS, along with specifying the path to the certificate using tls_trust_file. Additionally, we specify the path for msmtp log file using logfile field.
Step 2. Now we’ll set permission of this file to 600 using below command
chmod 600 ~/.msmtprc
Step 3. Configure accounts
Now, we’ll need to configure accounts in the .msmtprc configuration file. In this article, we will configure two accounts (default and outlook) we’ll show as an example the configuration of the Outlook SMTP service and Default SMTP service. The same configuration is also applicable to other SMTP services.
By appending some extra configuration lines to .msmtprc, we can effectively configure the Outlook SMTP service. In the end, our .msmtprc file should look like the following:
# Default settings
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
# Default Account
account default
host smtp.office365.com
port 587
from email@outlook.com
user email@outlook.com
password your-password
# Outlook Account
account outlook
host smtp.office365.com
port 587
from email@outlook.com
user email@outlook.com
password your-password
Remember to replace email@outlook.com
and your-password
with your Outlook credentials.
Step 4. Now let’s try to send an email using msmtp:
echo -e "Subject: Regards\n\nSending regards from Terminal." | msmtp -a outlook recipient@mail.com
In order to send HTML mail, you can create a file named sample.html and include appropriate headers that will ensure the content is interpreted as HTML
Let’s create the sample.html file
vim sample.html
and add the following content
From: email@outlook.com
To: recipient@mail.com
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 -a outlook email@outlook.com
Here, the cat command outputs the contents of the sample.html file, then we’re piping the result to the msmtp command.
By employing the Content-Type MIME header, we ensure that the file is treated as an HTML file. It is crucial to remember that the email header information must be specified within the HTML file for proper processing with msmtp.
Till now, we’ve seen how we can send an email from a Linux terminal using msmtp. While msmtp serves its purpose for simple emails, it lacks the capability to include attachments. To address this limitation, we can turn to mutt, a powerful MUA available in Linux that can be used from the command line and offers extensive features.
Step 5: Installing the Email Client:
Install it by executing below command
sudo apt-get install mutt (Debian/Ubuntu)
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 from=email@outlook.com
EOF
Firstly, with the line set sendmail=/usr/bin/msmtp, mutt will send email using the msmtp command. Additionally, we have enabled the use_from option and set the from variable to match our email address. It is crucial to ensure that this value matches the email address configured in msmtp. If the email address differs, the server will reject the delivery of the email.
Step 6: Composing and Sending Emails:
Now that everything is set up, you can compose and send emails.
-
- Sending an Email With Attachments
echo "This is the email body" | mutt -a "./log.txt" -s "Email Subject" -- recipient@mail.com
Note that the double dash “–” must precede the recipient’s email address to prevent the value from being consumed by the -a flag.
B. Sending HTML Mail
Let’s create sample.html file for sending an HTML mail: vim sample.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>
To send mail we can use following command:
mutt -e "set content_type=text/plain" -s "Attachement" -a "test.pdf" -- email@outlook.com < file.txt
Conclusion:
Sending emails from the Linux terminal using Outlook can be a convenient and efficient way to handle email communication. By configuring msmtp
and utilizing the mailx
or mutt
command-line email clients, you can compose and send emails directly from your terminal window. This method is especially useful for automation scripts or for those who prefer the command line interface. So give it a try, and streamline your email workflows with ease.