如何使用 sendmail 发送带有多个文本附件的 HTML 正文电子邮件

发布于 2024-10-24 22:42:35 字数 148 浏览 5 评论 0 原文

我想发送一个 HTML 文件作为邮件正文,并希望将多个文本文件附加到此电子邮件中。

由于需要发送html文件,因此必须使用sendmail(我无法使用mailx来做到这一点)。

如何使用 sendmail 发送 HTML 正文电子邮件和多个文本附件?

I want to send a HTML file as message body and want to attach multiple text files to this email message.

Since html file needs to be sent, sendmail has to be used ( I could not do it using mailx ).

How do you send HTML body email and multiple text attachments using sendmail?

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

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

发布评论

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

评论(3

旧夏天 2024-10-31 22:42:35

假设您的系统中有可用的 uunecode,您可以发送带有多个附件的电子邮件,如下所示:

#!/bin/bash

...
...
...
BOUNDARY="=== This is the boundary between parts of the message. ==="

{
   echo  "From: $MAILFROM"
   echo  "To: $MAILTO"
   echo  "Subject:" $SUBJECT
   echo  "MIME-Version: 1.0"
   echo  "Content-Type: MULTIPART/MIXED; "
   echo  "    BOUNDARY="\"$BOUNDARY\"
   echo
   echo  "        This message is in MIME format.  But if you can see this,"
   echo  "        you aren't using a MIME aware mail program.  You shouldn't "
   echo  "        have too many problems because this message is entirely in"
   echo  "        ASCII and is designed to be somewhat readable with old "
   echo  "        mail software."
   echo
   echo  "--${BOUNDARY}"
   echo  "Content-Type: TEXT/PLAIN; charset=US-ASCII"
   echo
   echo  "This email comes with multiple attachments."
   echo
   echo
   echo  "--${BOUNDARY}"
   echo  "Content-Type: application/zip; charset=US-ASCII; name="${ZIPFILE}
   echo  "Content-Disposition: attachment;   filename="`basename ${ZIPFILE}`
   echo
   uuencode $ZIPFILE $ZIPFILE
   echo
   echo  "--${BOUNDARY}--"
   echo  "Content-Type: application/pdf; charset=US-ASCII; name="${PDFFILE}
   echo  "Content-Disposition: attachment;   filename="`basename ${PDFFILE}`
   echo  
   uuencode $PDFFILE $PDFFILE
   echo
   echo  "--${BOUNDARY}--"
} | /usr/lib/sendmail -t

Assuming you have uunecode available in your system you can send email with multiple attachments like this:

#!/bin/bash

...
...
...
BOUNDARY="=== This is the boundary between parts of the message. ==="

{
   echo  "From: $MAILFROM"
   echo  "To: $MAILTO"
   echo  "Subject:" $SUBJECT
   echo  "MIME-Version: 1.0"
   echo  "Content-Type: MULTIPART/MIXED; "
   echo  "    BOUNDARY="\"$BOUNDARY\"
   echo
   echo  "        This message is in MIME format.  But if you can see this,"
   echo  "        you aren't using a MIME aware mail program.  You shouldn't "
   echo  "        have too many problems because this message is entirely in"
   echo  "        ASCII and is designed to be somewhat readable with old "
   echo  "        mail software."
   echo
   echo  "--${BOUNDARY}"
   echo  "Content-Type: TEXT/PLAIN; charset=US-ASCII"
   echo
   echo  "This email comes with multiple attachments."
   echo
   echo
   echo  "--${BOUNDARY}"
   echo  "Content-Type: application/zip; charset=US-ASCII; name="${ZIPFILE}
   echo  "Content-Disposition: attachment;   filename="`basename ${ZIPFILE}`
   echo
   uuencode $ZIPFILE $ZIPFILE
   echo
   echo  "--${BOUNDARY}--"
   echo  "Content-Type: application/pdf; charset=US-ASCII; name="${PDFFILE}
   echo  "Content-Disposition: attachment;   filename="`basename ${PDFFILE}`
   echo  
   uuencode $PDFFILE $PDFFILE
   echo
   echo  "--${BOUNDARY}--"
} | /usr/lib/sendmail -t
终陌 2024-10-31 22:42:35

我认为 sendmail 不会帮助您解决这个问题。寻找像 mutt 这样的客户端,然后执行 mutt -a file1 -a file2 -- [电子邮件受保护]。或者使用 perl

I don't think sendmail is going to help you with that. Go for a client like mutt, and do e.g. mutt -a file1 -a file2 -- [email protected]. Or go for perl.

愁杀 2024-10-31 22:42:35

这是我用来向人们发送生成的报告的 bash 脚本。它们作为附件发送。将 HTML 放入脚本的“body”变量中。我将把变量的参数化留给你。

#!/bin/bash

function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}

from="[email protected]"
to="[email protected]"
subject="Your Report my Lord"
boundary="=== Boundary ==="
body="The reports are attached to this email"
declare -a attachments
attachments=( "fileOne.out" "fileTwo.out" "fileThree.out" "file-et-cetera.out")

# Build headers
{

printf '%s\n' "From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"

--${boundary}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

$body
"

for file in "${attachments[@]}"; do

      [ ! -f "$file" ] && echo "Attachment $file not found, omitting file" >&2 && continue

        mimetype=$(get_mimetype "$file")

  printf '%s\n' "--${boundary}
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
  "

  base64 "$file"
  echo
done

# print last boundary with closing --
printf '%s\n' "--${boundary}--"

} | sendmail -t -oi

Here is a bash script I use to send reports I generate to people. They are sent as attachments. Place your HTML in the "body" variable of the script. I will leave the parametrization of the variables up to you.

#!/bin/bash

function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}

from="[email protected]"
to="[email protected]"
subject="Your Report my Lord"
boundary="=== Boundary ==="
body="The reports are attached to this email"
declare -a attachments
attachments=( "fileOne.out" "fileTwo.out" "fileThree.out" "file-et-cetera.out")

# Build headers
{

printf '%s\n' "From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"

--${boundary}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

$body
"

for file in "${attachments[@]}"; do

      [ ! -f "$file" ] && echo "Attachment $file not found, omitting file" >&2 && continue

        mimetype=$(get_mimetype "$file")

  printf '%s\n' "--${boundary}
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
  "

  base64 "$file"
  echo
done

# print last boundary with closing --
printf '%s\n' "--${boundary}--"

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