KornShell(ksh)代码发送带有mailx和uuencode的附件?

时间:2020-03-06 14:23:15  来源:igfitidea点击:

我需要用mailx添加文件,但目前还没有成功。

这是我的代码:

subject="Something happened"
to="[email protected]"
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,以避免每次都必须使用路径。

解决方案

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

  • 我们似乎假设邮件客户端将处理没有任何标头的uuencoded附件。那不会发生。
  • 我们正在滥用I / O重定向:uuencode的输出和here-document都被馈送到mailx,这是不可能的。
  • 我们正在滥用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

还有很多其他可能性...但是这个仍然有此处文档
就像示例一样,很容易,而且没有涉及临时文件。

我们必须同时合并邮件的文本和uuencoded附件:

$ subject="Something happened"
$ to="[email protected]"
$ 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"

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