【问题标题】:KornShell (ksh) code to send attachments with mailx and uuencode?使用 mailx 和 uuencode 发送附件的 KornShell (ksh) 代码?
【发布时间】:2010-09-10 21:54:30
【问题描述】:

我需要用 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 以避免每次都使用路径。

【问题讨论】:

    标签: shell unix scripting solaris ksh


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复!那么 msg.txt 是由 EOF 创建然后在最后一行重用的?
    • 我的错!我已经间隔了猫 >msg.txt
    【解决方案2】:

    嗯,这是你遇到的前几个问题。

    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文档 就像在你的例子中一样,我很容易想到,并且没有涉及临时文件。

    【讨论】:

    • 非常感谢!是的,我看到有 mutt 可以做简单的附件,但由于我不是那个盒子的根,我必须处理任何 mailx hack。
    • 我遇到以下错误:uuencode: 非法选项 -- m 用法:uuencode [infile] remotefile
    • 在提供 base64 编码之前,您拥有 uuencode 的旧版本...您仍然可以获得 mpack 并编译它以在您的帐户下本地使用...这就是我可能会做的。
    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2020-06-02
    • 2023-03-19
    • 2015-08-03
    • 2018-12-22
    • 2013-03-12
    相关资源
    最近更新 更多