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

发布于 2024-07-04 15:03:43 字数 782 浏览 9 评论 0原文

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

任何反馈都将受到高度赞赏。


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

I need to attach a file with mailx but at the moment I am not having success.

Here's my code:

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

Any feedback would be highly appreciated.


Update
I have added the attachment var to avoid having to use the path every time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-07-11 15:03:44

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

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

You have to concat both the text of your message and the uuencoded attachment:

$ 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"

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.

金橙橙 2024-07-11 15:03:44

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

  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

还有很多其他的可能性......但是这个仍然有这里的文档
就像你的例子一样,很容易就想到了,而且不涉及临时文件。

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. 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.

  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. Giving the file twice will assign the same name to the decoded file as that which was read. The -m flag forces base64 encode. But this still isn't going to provide attachment headers for mailx.

You're way better getting a copy of mpack, which will do what you want.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文