【问题标题】:How to send simple text file as attachment using HP-UX shell script?如何使用 HP-UX shell 脚本将简单文本文件作为附件发送?
【发布时间】:2011-06-08 04:49:59
【问题描述】:

我需要在 HP-UX 中使用 shell 脚本发送带有文本文件作为附件的电子邮件;我没有安装 mutt。

我正在使用以下命令,但它以电子邮件正文发送文件内容,我希望它作为附件。

mailx -s "Report" name@example.com < file.txt

【问题讨论】:

    标签: email shell unix mailx


    【解决方案1】:

    几年前我写了这个ksh函数

    # usage: email_attachment to cc subject body attachment_filename
    email_attachment() {
        to="$1"
        cc="$2"
        subject="$3"
        body="$4"
        filename="${5:-''}"
        boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
        {
            print -- "To: $to"
            print -- "Cc: $cc"
            print -- "Subject: $subject"
            print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
            print -- "Mime-Version: 1.0"
            print -- ""
            print -- "This is a multi-part message in MIME format."
            print -- ""
            print -- "--$boundary"
            print -- "Content-Type: text/plain; charset=ISO-8859-1"
            print -- ""
            print -- "$body"
            print -- ""
            if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
                print -- "--$boundary"
                print -- "Content-Transfer-Encoding: base64"
                print -- "Content-Type: application/octet-stream; name=$filename"
                print -- "Content-Disposition: attachment; filename=$filename"
                print -- ""
                print -- "$(perl -MMIME::Base64 -e 'undef $/; open $fh, shift; print MIME::Base64::encode(<$fh>); close $fh; ' $filename)"
                print -- ""
            fi
            print -- "--${boundary}--"
        } | /usr/lib/sendmail -oi -t
    }
    

    【讨论】:

    • 不,我只在电视上玩系统管理员。
    【解决方案2】:

    uuencode 是你的朋友。

    这是一个经过测试的例子:

    (uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address
    

    【讨论】:

    • 不是不行,我试过uuencode abcd.txt abcd.txt | grep mailx -s "Report" name@example.com 我收到的电子邮件在 msg 正文中包含以下内容 begin 644 abcd.txt M87-J:VIK87-J:V%S:@IA87-K:F%K ` end 而文件包含简单的文本,这也不是附件
    • @devs:“不工作”是什么意思?哪一部分失败了?
    • 数据已损坏,仍然不是附件
    • @devs:您使用哪些邮件用户代理查看您的电子邮件?邮件是否都显示已损坏?
    • @devs:你能试试其他客户端吗?例如 OWA?
    【解决方案3】:

    我遇到了同样的问题,uuencode 的输出是作为邮件正文的一部分而不是作为附件发送的(至少在使用 Outlook 2010 查看发送的邮件时)。我在这个帖子http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html找到了答案

    添加 -m 会导致 mailx 在发送电子邮件时不添加 MIME 标题行。 OP 的命令将被更改为:

    mailx -m -s "Report" name@example.com &lt; file.txt

    【讨论】:

      【解决方案4】:

      几个月前我也遇到了同样的问题。
      我需要的命令是ux2dos

      ( cat message_content_file; ux2dos file.txt | uuencode file.txt file.txt ) | mailx -m -s "subject" -r mail@sender mail@recipient
      

      希望对你有帮助!
      问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-21
        • 2022-01-15
        • 2022-01-25
        相关资源
        最近更新 更多