How to Send HTML Email Using Linux Command Line

It is highly probable that all users of the internet have or are still using mailing platforms to communicate with one another from electronic gadgets like laptops. With a stable internet connection, Linux users on different Linux distributions can instantaneously send and receive electronic messages.

[ You might also like: How to Send An Email With File Attachment from Command Line ]

Structure of an Email

Since this article will be demonstrating to us how to send an Email via the Linux command-line environment, it is important for us to understand the raw structure of an email.

This raw structure can be broken down into the following segments:

  • Sender – The sender is a unique user-identified email address that acts as the source of the mail yet to be sent.
  • Receiver – The receiver is a unique user-identified email address that receives the emailed message from the sender.
  • Subject – This portion of an email details the summarized purpose of the mail.
  • Message – Message details a composition of the electronic message to be sent to the Receiver from the Sender.

HTML Structure

Also, since we will be sending HTML emails, the following preview of an HTML document structure is important:

<!DOCTYPE HTML> 
<html>
<head>
<title></title>
</head>
<body>

</body>
</html>

In reference to the above HTML document skeleton, this tutorial is primarily concerned with HTML elements applicable between the <body> and </body> tags.

The need to know how to send an HTML email via the Linux terminal environment is an exceptional advantage to Linux users not bound to the Linux desktop environment. This user could mostly be on a server environment with limited GUI interaction.

Sending HTML Email Using Mail Command in Linux

This protocol-independent mail framework is rich and powerful enough to handle electronic mail transfer via the Linux terminal. It can be installed on various Linux OS distributions as follows:

$ sudo apt install mailutils        [On Debian, Ubuntu and Mint]
$ sudo yum install mailx            [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a mail-client/mailx  [On Gentoo Linux]
$ sudo pacman -S mailutils          [On Arch Linux]
$ sudo zypper install mailutils     [On OpenSUSE]    

To send mail via mail command, reference the following syntax:

$ echo "MAIL BODY" | mail -s 'MAIL SUBJECT' [email protected]_name

Its implementation is as follows:

$ echo "test body" | mail -s 'test subject' [email protected]_name

We can add some HTML touch to this email.

$ echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" [email protected]_name
Send HTML Mail in Linux Commandline
Send HTML Mail in Linux Commandline

We can now comfortably send HTML Emails from the Linux Command Line environment.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.