【发布时间】:2023-03-25 01:46:01
【问题描述】:
我需要通过 mailx 或邮件发送文件,但我希望将其作为附件发送,而不是在正文消息中。有什么办法吗? 最终,solaris 中是否还有其他工具可用于诸如程序之类的工具? 谢谢
【问题讨论】:
我需要通过 mailx 或邮件发送文件,但我希望将其作为附件发送,而不是在正文消息中。有什么办法吗? 最终,solaris 中是否还有其他工具可用于诸如程序之类的工具? 谢谢
【问题讨论】:
你可以像这样使用 -a 将文件附加到 mailx
echo "this is the body of the email" | mailx -s"Subject" -a attachment.jpg Someone@Domain.com
只要您在与附件相同的目录中就可以正常工作。如果没有,你可以像 `
这样声明目录samachPicsFolder/samachpic.jpg
【讨论】:
-a的mailx,对你有好处。如果你不这样做,也许看看mutt。如果您不想要外部依赖并且需要跨 U*x 平台移植,您将不得不编写几行脚本。
如果您的 mailx 不支持 -a 选项,并且您无权访问 mutt,并且您不想转向 uuencode 作为 1980 年代的后备,作为不得已,您可以自己拼凑一个小的 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 的路径通常取决于系统。试试/usr/sbin/sendmail 或/usr/lib/sendmail 或......如果它不在你的PATH 中,还有其他无数奇怪的地方。
这又快又脏;为了正确符合 MIME,您应该在必要时对主题进行 RFC2047 编码等,另请参阅代码中 cmets 中的注释。但是对于你的以美国为中心的 7 位英语 cron 任务,它会做得很好。
【讨论】:
base64 应该适用于任何类型的内容,预期的复杂性主要是如果您无法确定将正确值放入附件正文部分的 Content-Type: 标头中。跨度>
关于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
【讨论】:
-a 选项来mailx?
uuencode 是一个较旧的伪标准,比 MIME 和 base64 早了至少十年。这并不漂亮,但如果你被困在没有更新可用的恐龙上,它就能完成工作。诚然,这些天来使用最多的可能是那些不太了解的人。至于带有特定选项的mailx(除了像-s 这样的基本选项)它不是可移植的——OP 可能只有缺少该选项的版本。
我愿意 recommend using mutt 购买它,它足够轻巧,可以快速安装在任何系统上。
【讨论】: