Send an HTML file as email from the command line.

Posted by simonw on Mon 27 Jun 2005 at 20:48

Tags: none.

Do you need to send an HTML email from a script or command line? Whilst there are many different ways of doing this using mail from the mailx package is one of the simplest.

HTML email maybe evil according to some, but the simple but effective visitors package for reporting against Apache logs (ideally logs in combined logfile format to get; referer, operating system, and browser stats), produces prettier reports in HTML than it does in ASCII.

Previously we'd used a Perl script to send HTML emails, but I couldn't be bothered to port this to the new server, and it is copyright my employer so technically not free software (if I asked I'm sure they would share), so I reread the manual page for the "mail" command from the mailx package - chaces are you've already got this installed upon your server.

"mail" supports many options including the "-a" option for adding extra headers to the email. That'll do the job of making the mails it sends HTML.

visitors -A -o html /var/log/apache2/access.log.1 > ~/access.stats

mail -a "Content-type: text/html;" -s "Webstats" user@example.com < ~/access.stats

rm ~/access.stats

Hopefully that is pretty self explanatory. This of course was placed in a little script (handling multiple websites with statistics emailed to different people), executed by the 'postrotate' section of /etc/logrotate.d/apache2.

(You can read more about how logrotate works).

This is in the school of quick and dirty solutions (no locking against running concurrently etc), but it exhibits one bug less that the solution using the aforementioned Perl script (which perhaps wisely, but unnecessarily truncates lines to conform to relevant email RFC), so I'm sticking with it till it breaks. I've filed a couple of bug reports against the "visitors" package, as it has a few issues we needed to sort when using it for our clients on busy websites, but for my own personal use the unaltered package is superb.

If you need to send binary files via email, check out the package "mpack", which works in a very similar fashion, but because it packs stuff, it expects (and demands?!) more restrictive "Content-type" declarations. And yes I know "mutt" will do all this and more, I suspect Emacs can as well ;).

(You might want to look at reading HTML Mail in Mutt)

 

 


Posted by Anonymous (82.70.xx.xx) on Tue 28 Jun 2005 at 15:54
A quick improvement, to avoid faffing with temporary files:

visitors -A -o html /var/log/apache2/access.log.1 | mail -a "Content-type: text/html;" -s "Webstats" user@example.com < ~/access.stats

[ Parent | Reply to this comment ]

Posted by Anonymous (82.70.xx.xx) on Tue 28 Jun 2005 at 15:54
A quick improvement, to avoid faffing with temporary files:

visitors -A -o html /var/log/apache2/access.log.1 | mail -a "Content-type: text/html;" -s "Webstats" user@example.com

[ Parent | Reply to this comment ]

Posted by csmall (203.111.xx.xx) on Wed 29 Jun 2005 at 02:08
[ Send Message ]
Yes, html emails are evil, but sending them as attachments is less evil. An interesting idea about the mail program's headers anyway.


We use a program called nail to attach files for some automatic jobs. We used to use biabam but it mangled the PDF attachments in some strange way while with nail the md5sums match so you know its not mangling it.


To use nail you create the attachment then do something like:



echo "this is the body text of email" | nail -a /tmp/myattachment.pdf -s "My email subject" user@example.net


Instead of pdf, you could send html attachments this way.

[ Parent | Reply to this comment ]

Posted by hildeb (193.175.xx.xx) on Mon 4 Jul 2005 at 12:30
[ Send Message ]
Easier still: mpack (apt-get install mpack)

[ Parent | Reply to this comment ]

Posted by Anonymous (193.237.xx.xx) on Mon 4 Jul 2005 at 17:57
MPack won't let you set text/html as the Content-Type, thus producing an HTML some people won't be able to read.

I mention it at the end for sending binary files.

Or did I miss something?

[ Parent | Reply to this comment ]

Posted by Anonymous (202.173.xx.xx) on Tue 29 Nov 2005 at 23:52
Which version of the mailx package are you using?
I have version 8.1.1-44 and /bin/mail doesn't have the -a option to add extra headers.
Any ideas?

[ Parent | Reply to this comment ]

Posted by simonw (84.45.xx.xx) on Wed 30 Nov 2005 at 00:01
[ Send Message | View Weblogs ]
Debian Sarge has 8.1.2-0.20040524cvs-4 this is what I used.

Time to upgrade?

[ Parent | Reply to this comment ]

Posted by Anonymous (68.195.xx.xx) on Thu 31 May 2012 at 06:31
My version of mailx (CENTOS 6.2 x86_64 standard) also does not use -a for adding headers. It is for adding an attachment. This is from the mailx man page for -a :

-a file
Attach the given file to the message.

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

Which init system are you using in Debian?






( 1059 votes ~ 6 comments )