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' 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
We can now comfortably send HTML Emails from the Linux Command Line environment.
Thanks for the post Ravi, unfortunately this does not work for me from a RHEL 7 host:
I sent to my work email and the text comes back with html codes. Does it matter if if we have 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:
Using mailx should resolve the issue with HTML formatting in your emails.