Sending HTML Email from the Linux Command Line
Posted 41 days ago by Alex in Code, Randomness, Technology
Just a little tip that I found useful for a project I was working on today. I had to send the results of a MySQL query via email. Since the results are printed in a table, the formatting in the email had to use a monospace font. The easiest way to use a monospace font in an email is to enable HTML and wrap everything in a <pre> tag.
#!/bin/bash mailto=email@awesome.tld subject="The awesome subject line of your email message" ( echo "Subject: $subject" echo "MIME-Version: 1.0" echo "Content-Type: text/html" echo "Content-Disposition: inline" echo "<html><body><pre>" mysql db -uawesome -pawesomer --table < some-sql-to-run.sql echo "</pre></body></html>" ) | /usr/sbin/sendmail $mailto
The output looks like this:
To: email@email.tld From: system@awesome.tld Subject: The awesome subject line of your email message +----------+----------+----------+ | Column 1 | Column 2 | Column 3 | +----------+----------+----------+ | Apples | 2 | 14.00 | | Oranges | 2 | 0.5000 | +----------+----------+----------+