3

我需要用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

任何反馈将不胜感激。


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

4

2 回答 2

3

您必须同时连接您的消息文本和 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"

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

于 2008-09-18T20:35:21.627 回答
1

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

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

  2. 您在滥用 I/O 重定向:uuencode 的输出和 here-document 都被馈送到 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

还有很多其他的可能性......但是这个仍然有你的例子中的here文档,并且很容易从我的脑海中浮出水面,并且没有涉及临时文件。

于 2008-09-18T20:33:30.757 回答