如何通过mailx发送带有附件的电子邮件

时间:2011-06-20 07:53:08

标签: email mailx

我需要通过mailx或邮件发送文件,但我发送它作为附件而不是在正文消息中。有什么方法可以做到吗? 最终在solaris中是否还有其他可用于此类程序的工具? 感谢

5 个答案:

答案 0 :(得分:13)

您可以使用-a之类的文件将文件附加到mailx

echo "this is the body of the email" | mailx -s"Subject" -a attachment.jpg Someone@Domain.com

只要您在与附件相同的目录中应该可以正常工作。如果没有,你可以像`

那样陈述目录
samachPicsFolder/samachpic.jpg

答案 1 :(得分:4)

如果您的mailx不支持-a选项,但您无法访问mutt,并且您不想转向uuencode从20世纪80年代开始,作为最后的手段,你可以自己拼凑一个小的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
    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或{{1}}或......无数其他奇怪的地方。

这很快又脏;为了正确的MIME兼容性,您应该在必要时对主题执行RFC2047编码等,并且还要参阅代码中注释中的注释。但对于你平均以美国为中心的7位英语语言cron工作,它会做得很好。

答案 2 :(得分:1)

关于mailx,你可以在这里找到一些灵感 http://www.shelldorado.com/articles/mailattachments.html

我建议你看看mutt http://www.mutt.org/

答案 3 :(得分:1)

尝试使用此命令以使用Mailx发送附件:

uuencode source_file encoded_filename |mailx -m -s  "Subject" something@something.com

答案 4 :(得分:0)

recommend using mutt因为它的重量轻,可以在任何系统上快速安装。