简体   繁体   English

KornShell(ksh)代码用mailx和uuencode发送附件?

[英]KornShell (ksh) code to send attachments with mailx and uuencode?

I need to attach a file with mailx but at the moment I am not having success. 我需要附上一个mailx文件,但目前我没有成功。

Here's my code: 这是我的代码:

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

Any feedback would be highly appreciated. 任何反馈都将受到高度赞赏。


Update I have added the attachment var to avoid having to use the path every time. 更新我已添加附件var以避免每次都必须使用该路径。

You have to concat both the text of your message and the uuencoded attachment: 您必须连接邮件的文本和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"

There are different ways to provide the message text, this is just an example that is close to your original question. 有不同的方法来提供消息文本,这只是一个接近原始问题的示例。 If the message should be reused it makes sense to just store it in a file and use this file. 如果消息应该重用,那么将它存储在文件中并使用该文件是有意义的。

Well, here are the first few problems you've got. 好吧,这是你遇到的前几个问题。

  1. You appear to be assuming that a mail client is going to handle uuencoded attachment without any headers. 您似乎假设邮件客户端将处理没有任何标头的uuencoded附件。 That won't happen. 这不会发生。

  2. You're misusing I/O redirection: uuencode's output and the here-document are both being fed to mailx, which can't happen. 您正在滥用I / O重定向:uuencode的输出和here-document都被送到mailx,这是不可能发生的。

  3. You're misusing uuencode: if one path is given, it's just a name to give the decoded file, not an input file name. 你在滥用uuencode:如果给出一个路径,它只是一个给出解码文件的名称,而不是输入文件名。 Giving the file twice will assign the same name to the decoded file as that which was read. 给文件两次将为解码文件指定与读取的文件相同的名称。 The -m flag forces base64 encode. -m标志强制base64编码。 But this still isn't going to provide attachment headers for mailx. 但是这仍然不会为mailx提供附件头。

You're way better getting a copy of mpack, which will do what you want. 你最好获得一份mpack,这将是你想要的。

If you must do it, you could do something like this: 如果你必须这样做,你可以做这样的事情:

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

There are lots of other possibilities... but this one still has the here document as in your example and was easy off the top of my head, and there's no temp file involved. 还有很多其他的可能性......但是这个仍然有你的例子中的here文档,并且很容易脱离我的脑海,并且没有涉及临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM