How to get HTML file as a content to mail (shell script)

7,114

Solution 1

If you need the html sent as attachment, then you can use the shell tool called mpack.

http://linux.die.net/man/1/mpack

EDIT FOR SAMPLE:

I as a simple user send a dummy mail to root with /etc/hosts attached:

$ mpack -s 'trx of /etc/hosts' /etc/hosts root@localhost

and as root, I'm checking my box:

# mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/root": 1 message 1 new
& p
Message 1:
From sgombai@localhost  Thu Feb 11 01:54:27 2016
Date: Thu, 11 Feb 2016 01:54:27 +0100
From: sgombai <sgombai@localhost>
Mime-Version: 1.0
To: root@localhost
Subject: trx of /etc/hosts
Content-Type: multipart/mixed; boundary="-"

This is a MIME encoded message.  Decode it with "munpack"
or any other MIME reading software.  Mpack/munpack is available
via anonymous FTP in ftp.andrew.cmu.edu:pub/mpack/
---
Content-Type: application/octet-stream; name="hosts"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="hosts"
Content-MD5: 9WRRh8Yr7YPb7zo1AsgwcA==

MTI3LjAuMC4xCWxvY2FsaG9zdAo5LjE1Ny4yMTQuMTc4CXZhY21mcy52YWMuaHUuaWJtLmNv
bQl2YWNtZnMKCiMgVGhlIGZvbGxvd2luZyBsaW5lcyBhcmUgZGVzaXJhYmxlIGZvciBJUHY2
IGNhcGFibGUgaG9zdHMKOjoxICAgICBsb2NhbGhvc3QgaXA2LWxvY2FsaG9zdCBpcDYtbG9v
cGJhY2sKZmUwMDo6MCBpcDYtbG9jYWxuZXQKZmYwMDo6MCBpcDYtbWNhc3RwcmVmaXgKZmYw
Mjo6MSBpcDYtYWxsbm9kZXMKZmYwMjo6MiBpcDYtYWxscm91dGVycwpmZjAyOjozIGlwNi1h
bGxob3N0cwo=

-----

So it's arrived with attachment ready to be saved/decoded by any modern mailing program.

Solution 2

Try adding -a "Content-type: text/html;" to the message like this:

mail -a "Content-type: text/html;" -s "test" abc.com <test.html

You can easily test this by running this command that echo’s simple HTML to the mail command:

echo "<html><b>Test</b></html>" | mail -a "Content-type: text/html;" -s "test" abc.com

Got this idea from this question and answer thread on Unix & Linux Stack Exchange as well as this similar thread on Stack Overflow.

Share:
7,114

Related videos on Youtube

Theja
Author by

Theja

Updated on September 18, 2022

Comments

  • Theja
    Theja over 1 year

    I am new to shell script, I have an HTML file which contains table data. Now I need to send this file to mail.

    mail -s "test" abc.com <test.html
    

    Currently I am getting the raw HTML code for the table in mail.

    I need the table content in mail as we get in Internet Explorer.

  • Abraxas
    Abraxas over 8 years
    Can you include a little more information as links are not typically acceptable as answers? Even just outlining what it is, what its prereqs are, etc. go a long way.