【问题标题】:how to send an email via mailx with enclosed file如何通过带有附件文件的mailx发送电子邮件
【发布时间】:2023-03-25 01:46:01
【问题描述】:

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

【问题讨论】:

标签: email mailx


【解决方案1】:

你可以像这样使用 -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 平台移植,您将不得不编写几行脚本。
【解决方案2】:

如果您的 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: 标头中。跨度>
【解决方案3】:

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

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

【讨论】:

  • 谢谢,但是 mutt 没有安装在那里,我没有安装它的权限。那么有没有其他方法可以解决呢?
【解决方案4】:

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

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

【讨论】:

  • 你能解释一下你的答案吗?特别是,当电子邮件主要使用 base64 作为附件时,为什么要使用 uuencode?为什么不使用-a 选项来mailx?
  • uuencode 是一个较旧的伪标准,比 MIME 和 base64 早了至少十年。这并不漂亮,但如果你被困在没有更新可用的恐龙上,它就能完成工作。诚然,这些天来使用最多的可能是那些不太了解的人。至于带有特定选项的mailx(除了像-s 这样的基本选项)它不是可移植的——OP 可能只有缺少该选项的版本。
【解决方案5】:

我愿意 recommend using mutt 购买它,它足够轻巧,可以快速安装在任何系统上。

【讨论】:

  • 我没有 mutt 也没有可能安装它。有什么办法可以通过 mailx 或 mail 发送该文件?我已经通过邮件工具尝试过,但它不起作用,我的意思是文件是在邮件正文中发送的,这对我来说还不够。有什么想法吗?
  • @slafik:哪个系统和操作系统,有什么可用的? Perl?
猜你喜欢
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 2011-10-30
相关资源
最近更新 更多