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

发布于 2024-11-16 08:41:14 字数 94 浏览 3 评论 0原文

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

I need to sent a file via mailx or mail, but I wat to sent it as attachment not in the body message. Is there any way how to do it ?
Eventually is there any other tool in solaris which can be used for such as procedure ?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

草莓味的萝莉 2024-11-23 08:41:14

您可以使用 -a 将文件附加到 mailx,只要

echo "this is the body of the email" | mailx -s"Subject" -a attachment.jpg [email protected]

您的附件与附件位于同一目录中,就可以正常工作。如果没有,您可以只指定目录,例如`

samachPicsFolder/samachpic.jpg

You can attach files to mailx using -a like so

echo "this is the body of the email" | mailx -s"Subject" -a attachment.jpg [email protected]

so long as your in the same directory as your attachment that should work fine. If not you can just state the directory like `

samachPicsFolder/samachpic.jpg
相思故 2024-11-23 08:41:14

如果您的 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
    # - 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 编码等,另请参阅代码中注释中的注释。但对于一般以美国为中心的 7 位英语 cron 作业来说,它就足够了。

If your mailx doesn't support the -a option and you don't have access to mutt, and you don't want to turn to uuencode as a fallback from the 1980s, as a last resort you can piece together a small MIME wrapper yourself.

#!/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

The path to sendmail is often system-dependent. Try /usr/sbin/sendmail or /usr/lib/sendmail or ... a myriad other weird places if it's not in your PATH.

This is quick and dirty; for proper MIME compliance, you should do RFC2047 encoding of the subject if necessary, etc, and see also the notes in the comments in the code. But for your average US-centric 7-bit English-language cron job, it will do just fine.

﹎☆浅夏丿初晴 2024-11-23 08:41:14

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

我会推荐你看看杂种狗
http://www.mutt.org/

Regarding mailx, you can find some inspiration here
http://www.shelldorado.com/articles/mailattachments.html

I would recommend you to have a look at mutt
http://www.mutt.org/

残龙傲雪 2024-11-23 08:41:14

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

uuencode source_file encoded_filename |mailx -m -s  "Subject" [email protected]

Try using this command in order to send an attachment using Mailx:

uuencode source_file encoded_filename |mailx -m -s  "Subject" [email protected]
心奴独伤 2024-11-23 08:41:14

I'd recommend using mutt for it, which is light-weight enough to quickly install on any system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文