Linux – Send HTML email from shell CLI

emaillinuxshell

I am running the cron job where there is html formatted output.
I want to send that output my email address in html format.

Is there any way to do that

php  /home/bla/bla_bla.php | mail -s "Bla Bla"  -s "bla@bla.com"

Best Answer

You might want to take a look at mime-construct. It's handy for just this kind of thing. I use it to send a generated HTML doc as part of a daily cronjob like so:

/usr/bin/mime-construct --to "foo@bar.com" --subject "My daily html foo" --multipart multipart/alternative --type text/html --file htmlfiletosend.html

If you don't care about the messages being multipart, you can drop that portion. It can also take the html via stdin like so:

php your_script.php | mime-construct --to "foo@bar.com" --subject "Foo Report" --type text/html --file -

I don't know what distro you're using, but this is available in the main repositories for Debian and Ubuntu, I would imagine RHEL and derivatives likely have it too.