Recently I was working with something and got into a situation where I wanted to embed some content to an email body and trigger it from a Linux server via command line inputs. We can do it using any of the Email-Clients until the file is an Excel(CSV) format and needed to send it as an Email body.

Sending the attachment via email is not a tough task, we can achieve it using any of the email-clients like Mutt, Alpine, Sup, Not much, Sendmail, Mail, etc. The complexity increases when the CSV has to be embedded in the Email.

Now, to embed any file in the email body, the file has to be converted to HTML format. We can achieve this using small Linux scripts where you use awk command to format the file into an HTML. Secondly, we can use different shell utilities to do the same where we install some third-party applications and do the conversion.

CSV FILE

Let’s take an example. Our excel file here is EXCEL_CSV.csv. The content of the file is displayed below:

cat EXCEL_CSV.csv

subscription,location,resourceGroupName,name,size,publisher,offer,sku,provisioningState,powerState,privateIP,publicIP

sunscription1, eastus,RG1,NAME1, standard_d12_v2, canonical, ubuntuserver, 16.04-lts, succeeded," ""DELETED""",0.0.0.0,0.0.0.0

sunscription2, eastus,RG2,NAME2, standard_ds1_v2, canonical, ubuntuserver, 16.04-lts, succeeded," ""DELETED""",0.0.0.0,0.0.0.0

sunscription3, eastus,RG3,NAME3, standard_a1, canonical, ubuntuserver, 14.04.4-lts, succeeded," ""vm deallocated""",0.0.0.0, null

sunscription4, eastus,RG4,NAME4, standard_a1, microsoftwindowsserver, windowsserver, 2008-r2-sp1, succeeded," ""vm deallocated""",0.0.0.0, null

CSV to HTML (Using CSV2HTML) in Linux

The next step is to convert the excel file to HTML format. To do this, we are going to install a third party utility tool named csv2html.

This command line utility and reusable Python package converts CSV files to HTML tables or complete HTML documents. It requires Python 2.7 or Python 3.2 or later. By default, it uses the first row of the CSV file as the header of the HTML table.

This is one of the best tools for the conversion as it keeps the format and content of CSV in place. It even makes sure the alignment of the table doesn’t mess with the original.

To install this utility, we need NodeJs to be installed first and then the below command installs csv2html.

npm install csv2html –g

Once installed, we can convert the CSV into HTML using a very simple command. Here our source file is EXCEL_CSV.csv and we want to convert it to EXCEL_HTML.html.

Syntax: csv2html <source-file> > <export-file>

csv2html EXCEL_CSV.csv > EXCEL_HTML.html

ls

 

Use MUTT command to send attachment in Linux

The third step is to install an email client. We are going to use mutt as it is one of the best email-clients in the market.

Mutt is a small but very powerful text-based mail client for Unix operating systems. It supports most mail storing formats (notably both mbox and Maildir) and protocols (POP3, IMAP, etc.). It also includes MIME support, notably full PGP/GPG and S/MIME integration.

[Taken from www.mutt.org]

Some of Linux mail client Mutt’s features include:

  • color support
  • message threading
  • various features to support mailing lists, including list-reply
  • active development community
  • POP3 support, IMAP support
  • full control of message headers when composing
  • highly customizable, including keybindings and macros
  • Delivery Status Notification (DSN) support
  • easily include attachments when composing, even from the command line
  • reply to or forward multiple messages at once
  • easy to install (uses GNU Autoconf)
  • translation into at least 20 languages
  • small and efficient
  • It’s free! (no cost and GPL’ed)

Installing the Mutt is very easy. It comes with the package installation in RHEL and apt-get in Ubuntu. It is rich with all the attributes which offer almost all the email functionalities. By executing “mutt -h”, you can see all the attributes which can be utilized with mutt command. Below is the installation command.

yum install mutt

mutt –h

SENDING EMAIL through CLI in Linux

Now, we have done converting our CSV to HTML file, it’s the time to embed it in the email body and send it. Below is the command where

  • -c : Carbon-copy address
  • -s : Subject line

Syntax:

mutt -e ‘set content_type=”text/html”‘ <Email-Address-1> <Email-Addess-2> <Email-Address-n> -c <Email-Address-1> <Email-Addess-2> <Email-Address-n> -s <”Subject Line”> <  <HTML File Location>

mutt -e 'set content_type="text/html"' ankesh@linuxnix.com sahil@linuxnix.com -c surendra@linuxnix.com -s "AZURE INVENTORY LIST"  <  EXCEL_HTML.html

The email is being sent and you can check the email as below:

If you want to send an attachment with the email, you need to add an attribute “-a” followed with the file location.

Syntax: mutt -e ‘set content_type=”text/html”‘ <Email-Address-1> <Email-Addess-2> <Email-Address-n> -c <Email-Address-1> <Email-Addess-2> <Email-Address-n> -s <”Subject-Line”> -a <Attachment-Location>  <  <HTML-File-Location>

mutt -e 'set content_type="text/html"'ankesh@linuxnix.com sahil@linuxnix.com -c surendra@linuxnix.com -s "AZURE INVENTORY LIST" -a EXCEL_CSV.csv  <  EXCEL_HTML.html

Interested in such posts, stay tuned to get updates.

The following two tabs change content below.
Hello, my name is Ankesh and I work as a Sr. DevOps Engineer in an IT firm where I have hands-on experience in supporting and automating deployments in the cloud, leveraging configuration management and DevOps process. I like to keep myself always in the learning path and love to share knowledge with others. One of my other interests is Graphics Designing and my hobbies are Cooking and Playing Cricket.