我需要通过mailx或mail发送一个文件,但我想将它作为附件而不是在正文消息中发送。有没有办法实现这个功能?最后,是否有任何其他可以用于此类操作的Solaris工具?谢谢
echo "this is the body of the email" | mailx -s"Subject" -a attachment.jpg Someone@Domain.com
只要您在与附件相同的目录中,那么这应该可以很好地工作。如果不是,您只需指定目录即可,例如:路径/附件名
。samachPicsFolder/samachpic.jpg
mailx
,那就太好了。如果没有,可以考虑使用mutt
。如果你不想依赖外部库并且需要在多个Unix平台上移植,那么你将不得不编写几行脚本。 - tripleee如果你的mailx
不支持-a
选项,又没有使用mutt
的权限,也不想退回到上个世纪80年代的uuencode
作为备选方案,那么最后的选择是自己拼凑一个小的MIME包装器。
#!/bin/sh
# ... do some option processing here. The rest of the code
# assumes you have subject in $subject, file to be attached
# in $file, recipients in $recipients
boundary="${RANDOM}_${RANDOM}_${RANDOM}"
(
cat <<____HERE
Subject: $subject
To: $recipients
Mime-Version: 1.0
Content-type: multipart/related; boundary="$boundary"
--$boundary
Content-type: text/plain
Content-transfer-encoding: 7bit
____HERE
# Read message body from stdin
# Maybe apply quoted-printable encoding if you anticipate
# overlong lines and/or 8-bit character codes
# - then you should change the last body part header above to
# Content-Transfer-Encoding: quoted-printable
cat
cat <<____HERE
--$boundary
Content-type: application/octet-stream; name="$file"
Content-disposition: attachment; filename="$file"
Content-transfer-encoding: base64
____HERE
# If you don't have base64 you will have to reimplement that, too /-:
base64 "$file"
cat <<____HERE
--$boundary--
____HERE
) | sendmail -oi -t
sendmail
路径通常是依赖于系统的。如果在你的PATH
中找不到,可以尝试使用/usr/sbin/sendmail
或/usr/lib/sendmail
等其他奇怪的位置。
这只是一个快速而简单的解决方法;为了符合MIME规范,如果需要,应该对主题进行RFC2047编码,并查看代码注释中的注意事项。但是对于平均的美国本土7位英语语言cron工作,它也可以正常运行。
base64
应该适用于任何类型的内容,预期的复杂性主要在于无法确定附件正文的 Content-Type:
标头中应放置的正确值。 - tripleee关于mailx,你可以在这里找到一些灵感 http://www.shelldorado.com/articles/mailattachments.html
我建议你看看mutt http://www.mutt.org/
尝试使用以下命令使用Mailx发送附件:
uuencode source_file encoded_filename |mailx -m -s "Subject" something@something.com
uuencode
是一种比MIME和base64早至少十年的老式伪标准。它不太美观,但如果你被困在一个没有更新版本的恐龙中,它可以完成工作。当然,现在大多数人使用它可能只是因为他们不知道更好的选择。至于mailx
,除了一些非常基本的选项(如-s
)之外,特定选项并不可移植 - OP可能只有缺少该选项的版本。 - tripleee我建议使用 mutt
,它足够轻量级,可以快速安装在任何系统上。