【发布时间】:2011-06-08 04:49:59
【问题描述】:
我需要在 HP-UX 中使用 shell 脚本发送带有文本文件作为附件的电子邮件;我没有安装 mutt。
我正在使用以下命令,但它以电子邮件正文发送文件内容,我希望它作为附件。
mailx -s "Report" name@example.com < file.txt
【问题讨论】:
我需要在 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
希望对你有帮助!
问候
【讨论】: