如何使用 HP-UX shell 脚本将简单的文本文件作为附件发送?

发布于 2024-11-14 09:39:15 字数 300 浏览 2 评论 0原文

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

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

mailx -s "Report" [email protected] < file.txt

I need to send an email with a text file as attachment using shell script in HP-UX; I don't have mutt installed.

I am using following command but it sends the file content in body of email, I want it as an attachment.

mailx -s "Report" [email protected] < file.txt

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

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

发布评论

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

评论(4

左秋 2024-11-21 09:39:15

几年前我写了这个 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
}

I wrote this ksh function a few years ago

# 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
}
戏蝶舞 2024-11-21 09:39:15

uuencode 是你的朋友。

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

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

uuencode is your friend.

Here is a tested example:

(uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address
柠北森屋 2024-11-21 09:39:15

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

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

mailx -m -s "Report" [电子邮件受保护] <文件.txt

I was having the same problem where the output of uuencode was being sent as part of the message body rather than as an attached file (at least when using Outlook 2010 to view the sent mail). I found the answer in this thread http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html

Adding -m causes mailx to not add MIME header lines when sending email. The OP's command would be altered to look like:

mailx -m -s "Report" [email protected] < file.txt

ら栖息 2024-11-21 09:39:15

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

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

我希望它能有所帮助!
问候

I also encountered the same problem few months ago.
The command I needed was ux2dos

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

I hope it can help !
Regards

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