0

I am upgrading some batch programs to produce html output rather than text. The batch programs are run with the at command, which in turn sends all output to the user using the sendmail command.

Because the at command composes the headers for the e-mail I am stuck with how to indicate to either the 'at' command or 'sendmail' that the output of the batched command is in HTML format. The combination of 'at' and 'sendmail' create the headers and terminate the header with a line break so the inclusion of header information, e.g. Content-Type, are considered to be a part of the body.

My expectation is that when the mail is read by a modern client, the Content-type is correctly set to "text/html".

I am happy for any solution, including: command line options, environment variables, bash/perl/awk/ scripts, suggestions for a new batch execution program.

Tai Paul
  • 1,341
  • Why would at have anything to do with it? What are you currently trying? – Mikel Jul 14 '16 at 14:44
  • 3
  • at is not mail. The answers to the proposed duplicate may be applicable, but that doesn't make this a duplicate of that question. – user Jul 14 '16 at 18:47
  • 1
    So don't write to stdout or stderr, relying on at to send you the message. Instead, send the mail yourself (in your script) where you have proper control over it. – Chris Davies Jul 14 '16 at 20:46
  • Michael, sure, but they crux of the question is "how do I send HTML mail using command line tools" -- the final paragraph of the question explicitly confirms this -- and there's a good answer in a similar question using sendmail, so I think it's close enough. – Mikel Jul 15 '16 at 02:10
  • Tai, the answer would be the same if you'd asked how to make grep produce HTML output: don't do that, use the right tool for the job. – Mikel Jul 20 '16 at 03:29
  • @Mikel, are you planning on posting an answer and removing your comments? Also removing the suggestion this is a duplicate question, as I trust that you recognise that although you recommend an answer this question is different. – Tai Paul Jul 20 '16 at 20:53

3 Answers3

1

Send the message explicitly from your script, rather than just print the output and rely on at to send the message. Write the HTML to a file and use mutt to attach it and send the message.

Alternatively, write the message with headers, MIME structure and all, and pipe it to sendmail -t -i. Using mutt is the easier way by far.

Satō Katsura
  • 13,368
  • 2
  • 31
  • 50
0

I don't think it's possible to natively do what you are looking for. I'm not aware of any at implementation having the ability to, from within the job, make any real changes to the resultant message headers.

However, you could run the script as a sub-process that produces HTML output, redirect that output into a file, and add any surrounding headers as necessary before passing the entire resultant text blob to sendmail -bm as is (and relying on sendmail to figure out the sender and recipient fields).

Something like, a very contrived example:

>tempfile
echo 'From: nobody@localhost' >>tempfile
echo 'To: root@localhost' >>tempfile
echo 'Subject: list of files' >>tempfile
echo >>tempfile
ls -lh $HOME >>tempfile
echo 'Add more data here' >>tempfile
sendmail -bm <tempfile && rm -f tempfile

might do roughly what you want. (You would of course have to adjust the above to include everything that indicates that the output is HTML.)

user
  • 28,901
0

Reading through the 'at' source code it is a fairly trivial patch to alter the mail header that the at command creates to include a content-type field.

The complete answer may be to add an environment variable e.g. AT_CONTENT_TYPE or a command line argument e.g. -c that the at command checks and validates the value against two allowable values being "plain" as the default value and "html" as the valid alternate.

Of course this will make your implementation non-compliant with the POSIX specification POSIX specification for at so you will benefit copying and renaming the command compliant with the GNU General Public License.

Tai Paul
  • 1,341