How to run send email with attachments on HP-UX

At my office, we use many HP-UX systems. Sometimes it is desirable to be able to send email with attachments from those boxes, usually from scripts. Searching google for 'mailx attachment' produced many, sometimes contradictory, hits. All the methods I found, other than using some thrid party too, which I didn't want to do, involve using uuencode to encode the attachment, and then piping that into mail or mailx. Some pages said it was possible to use mailx, but when I tried it, I just got garbage in the body of the message and no attachment.

However, other pages suggested using just the mail command instead. That seems to work for me. So, here are my notes about how.

To send an email with an attachment from one of the HP boxes, one must uuencode the attached file first, and then pipe that to the mail, NOT the mailx, command. For example:

(uuencode somefile.txt somefile.txt) | mail

This will send you a message with somefile.txt as an attachment. But there will be nothing in the body of the message, nor will there be a subject header. If you want some words in the body of the message, put the words into a separate file, and then include that in the pipe into mail, like so:

(cat messagebody.txt; uuencode somefile.txt somefile.txt) | mail

This will get you an attachment and a message, but still no subject. To add the subject, in the messagebody.txt file, put the subject header in the very first line followed by a blank line, followed by the text you want in the body of the message. For example:

Subject: Output from some process attached

Attached to this message is the output from some process that you wanted to monitor. This message was automatically generated, so don't try to reply to it.