我需要使用 HP-UX 中的 shell 脚本发送一封带有文本文件作为附件的电子邮件;我没有安装 mutt。
我正在使用以下命令,但它以电子邮件正文发送文件内容,我希望它作为附件。
mailx -s "Report" name@example.com < file.txt
ksh
几年前我写了这个函数
# 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
是你的朋友。
这是一个经过测试的示例:
(uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address
我遇到了同样的问题,uuencode 的输出作为邮件正文的一部分而不是作为附件发送(至少在使用 Outlook 2010 查看发送的邮件时)。我在这个线程http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html中找到了答案
添加 -m 会导致 mailx 在发送电子邮件时不添加 MIME 标题行。OP 的命令将更改为:
mailx -m -s "Report" name@example.com < file.txt
几个月前我也遇到了同样的问题。
我需要的命令是ux2dos
( cat message_content_file; ux2dos file.txt | uuencode file.txt file.txt ) | mailx -m -s "subject" -r mail@sender mail@recipient
我希望它可以帮助!
问候