【问题标题】:Sending email with HTML body and attachement with mailx使用 mailx 发送带有 HTML 正文和附件的电子邮件
【发布时间】:2014-03-10 18:13:01
【问题描述】:

我目前正在使用 mailx 从我的脚本中发送 html 格式的邮件:

cat body.html | /usr/bin/mailx -a "From: Me <me@domain>" -a "Content-type: text/html" -s "My subject" $RECIPIENTS

现在我想添加一个附件(png 图像),但我不知道怎么做。 在迁移到 mutt 或其他东西之前,我想尝试使用 mailx。 非常感谢

【问题讨论】:

  • 看看HERE
  • @Dinesh 这不是一个很好的答案。 HTML 和 uuencode 不是一个合理的组合。

标签: html linux email attachment mailx


【解决方案1】:

如果您的要求很简单,您可以使用裸 Sendmail。这不是我特别推荐的,但因为你想避免mutt...

# Now that we actually concatenate two files (well, stdin and a file),
# we are no longer eligible for a Useless Use of Cat Award
( cat - body.html <<HERE
Subject: My subject
Mime-Version: 1.0
Content-type: multipart/related; boundary="foooobar"

--foooobar
Content-type: text/html

HERE

cat <<HERE

--foooobar
Content-type: image/png
Content-disposition: inline
Content-transfer-encoding: base64

HERE

base64 image.png

echo; echo '--foooobar--' ) | sendmail -oi $RECIPIENTS

我确实希望有一个简单的标准实用程序来解决这个问题,但可惜的是,有许多或多或少相互不兼容和模糊不清的实用程序。同样,如果您可以使用 mutt,那可能是您希望得到的最广泛支持和最标准的工具。

【讨论】:

  • 感谢tripleee,但是(这是一个不同的问题)在使用sendmail 时,邮件会进入(gmail)收件人邮箱的垃圾邮件。
  • 这可能是您的内容的功能,而不是您用来发送它的程序。
【解决方案2】:

试试这个:

uuncode input_file2.jpg attachment2.jpg >>tempfile
cat tempfile | mailx -s "subject" <email>

Uuencode 读取文件(或默认为标准输入)并将编码版本写入标准输出。编码仅使用打印 ASCII 字符并包括文件的模式和 uudecode 使用的操作数名称。如果名称是 /dev/stdout,则结果将被写入标准输出。默认情况下,将使用标准 UU 编码格式。如果在命令行中给出了 -m 选项,则使用 base64 编码。

【讨论】:

  • 虽然代码很受欢迎,但它应该始终有一个附带的解释。这不必很长,但在意料之中。
  • 评论中提到的命令在执行之前可以很容易地检查,我看不出需要进一步解释什么,因为询问的用户已经在使用 mailx,并且知道“cat”。 uunicode,可轻松操作
  • Uuencode 是一种遗留机制,除非您坚持使用 1980 年代仍然不支持 MIME 的工具,否则应避免使用它。
猜你喜欢
  • 2016-08-16
  • 1970-01-01
  • 2023-03-25
  • 2020-06-02
  • 2018-07-24
  • 1970-01-01
  • 2018-11-07
  • 2013-11-26
  • 2012-11-17
相关资源
最近更新 更多