我想发送一个HTML文件作为邮件正文,并希望将多个文本文件附加到此电子邮件。
由于html文件需要发送,sendmail必须使用(我不能使用mailx)。
如何使用sendmail发送HTML正文电子邮件和多个文本附件?
我不认为sendmail
会帮助你。 去找一个像mutt
这样的客户端,然后执行mutt -a file1 -a file2 -- recipient@do.main
。 或者去perl
。
假设您的系统中有可用的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
这是一个bash脚本,我用它来发送给人们的报告。 他们作为附件发送。 将您的HTML放在脚本的“body”变量中。 我将把变量的参数化给你。
#!/bin/bash function get_mimetype(){ file --mime-type "$1" | sed 's/.*: //' } from="me.last@company.com" to="some.one@companyBlah.com" 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