I need to send an email with a text file as attachment using shell script in HP-UX; I don't have mutt installed.
I am using following command but it sends the file content in body of email, I want it as an attachment.
mailx -s "Report" name@example.com < file.txt
I wrote this ksh
function a few years ago
# usage: email_attachment to cc subject body attachment_filename
email_attachment() {
to="$1"
cc="$2"
subject="$3"
body="$4"
filename="${5:-''}"
boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
{
print -- "To: $to"
print -- "Cc: $cc"
print -- "Subject: $subject"
print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
print -- "Mime-Version: 1.0"
print -- ""
print -- "This is a multi-part message in MIME format."
print -- ""
print -- "--$boundary"
print -- "Content-Type: text/plain; charset=ISO-8859-1"
print -- ""
print -- "$body"
print -- ""
if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
print -- "--$boundary"
print -- "Content-Transfer-Encoding: base64"
print -- "Content-Type: application/octet-stream; name=$filename"
print -- "Content-Disposition: attachment; filename=$filename"
print -- ""
print -- "$(perl -MMIME::Base64 -e 'undef $/; open $fh, shift; print MIME::Base64::encode(<$fh>); close $fh; ' $filename)"
print -- ""
fi
print -- "--${boundary}--"
} | /usr/lib/sendmail -oi -t
}
uuencode
is your friend.
Here is a tested example:
(uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address
I was having the same problem where the output of uuencode was being sent as part of the message body rather than as an attached file (at least when using Outlook 2010 to view the sent mail). I found the answer in this thread http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html
Adding -m causes mailx to not add MIME header lines when sending email. The OP's command would be altered to look like:
mailx -m -s "Report" name@example.com < file.txt
I also encountered the same problem few months ago.
The command I needed was ux2dos
( cat message_content_file; ux2dos file.txt | uuencode file.txt file.txt ) | mailx -m -s "subject" -r mail@sender mail@recipient
I hope it can help !
Regards
来源:https://stackoverflow.com/questions/6274325/how-to-send-simple-text-file-as-attachment-using-hp-ux-shell-script