Home Linux Commandline Tips How to Send HTML Email Using Linux Command Line

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' receiver@domain_name

Its implementation is as follows:

$ echo "test body" | mail -s 'test subject' receiver@domain_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")" receiver@domain_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.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

2 thoughts on “How to Send HTML Email Using Linux Command Line”

  1. Thanks for the post Ravi, unfortunately this does not work for me from a RHEL 7 host:

    $ echo "HTML Message goes here" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" receiver@domain_name

    I sent to my work email and the text comes back with html codes. Does it matter if if we have mail -> mailx?

    lrwxrwxrwx. 1 root root 5 Jul 21  2018 /usr/bin/mail -> mailx
    • @Chad,

      It seems like you’re encountering an issue with the formatting of the email body when using HTML. To ensure proper rendering of HTML content in emails, you may need to use the mailx command instead of mail.

      Here’s the corrected command:

      echo "HTML Message goes here" | mailx -a 'Content-Type: text/html' -s "This is the subject" receiver@domain_name

      Using mailx should resolve the issue with HTML formatting in your emails.


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.