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:



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.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

Got Something to Say? Join the Discussion...

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.