我需要用 mailx 附加一个文件,但目前我没有成功。

这是我的代码:

subject="Something happened"
to="somebody@somewhere.com"
body="Attachment Test"
attachment=/path/to/somefile.csv

uuencode $attachment | mailx -s "$subject" "$to" << EOF

The message is ready to be sent with the following file or link attachments:

somefile.csv

Note: To protect against computer viruses, e-mail programs may prevent
sending or receiving certain types of file attachments.  Check your
e-mail security settings to determine how attachments are handled.

EOF

任何反馈将不胜感激。


更新我添加了附件变量以避免每次都使用该路径。

有帮助吗?

解决方案

您必须连接消息文本和 uuencoded 附件:

$ subject="Something happened"
$ to="somebody@somewhere.com"
$ body="Attachment Test"
$ attachment=/path/to/somefile.csv
$
$ cat >msg.txt <<EOF
> The message is ready to be sent with the following file or link attachments:
>
> somefile.csv
>
> Note: To protect against computer viruses, e-mail programs may prevent
> sending or receiving certain types of file attachments.  Check your
> e-mail security settings to determine how attachments are handled.
>
> EOF
$ ( cat msg.txt ; uuencode $attachment somefile.csv) | mailx -s "$subject" "$to"

提供消息文本的方式有多种,这只是一个接近您原始问题的示例。如果应该重用该消息,则只需将其存储在文件中并使用该文件是有意义的。

其他提示

好吧,这是您首先遇到的几个问题。

  1. 您似乎假设邮件客户端将处理没有任何标头的 uuencoded 附件。那不会发生。

  2. 您滥用了 I/O 重定向:uuencode 的输出和此处文档都被馈送到 mailx,这是不可能发生的。

  3. 您滥用了 uuencode:如果给出一个路径,则它只是给出解码文件的名称,而不是输入文件名。给该文件两次将为解码的文件分配与读取的文件相同的名称。-m 标志强制进行 Base64 编码。但这仍然不会为 mailx 提供附件标头。

你最好得到一份 mpack 的副本,它会做你想要的事情。

如果你必须这样做,你可以这样做:

cat <<EOF | ( cat -; uuencode -m /path/to/somefile.csv /path/to/somefile.csv; ) | mailx -s "$subject" "$to" 
place your message from the here block in your example here
EOF

还有很多其他的可能性...但是,这仍然像您的示例中一样具有此处的文档,并且很容易在我的头顶上,并且不涉及临时文件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top