我在使用attachemnt发送html邮件时遇到问题。我可以发送邮件 附件(纯文本)使用mailx -s和uuencode命令以及html邮件 没有附件使用sendmail选项。
但是我无法发送html邮件和附件。 其中任何一个正在工作(html邮件或附件)
以下是我尝试过的不同方法。你能帮我解决一下吗?
1) Failed because of illegal option base64
#!/usr/bin/ksh
export MAILTO="abc@abc.com"
export SUBJECT="Mail Subject"
export BODY="card_summary_mail.html"
export ATTACH="query5_result.csv"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode --base64 $ATTACH $(basename $ATTACH)
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail $MAILTO
2)
cib-sokay2{u384283}323:cat test_html2.sh
{
uuencode query5_result.csv query5_result.csv > attachment.txt
cat mail.html attachment.txt > attachment2.html
} | /usr/lib/sendmail -t abc@abc.com
-----------------------------------------------
3)
cib-sokay2{u384283}324:cat test_html3.sh
export MAILTO="abc@abc.com"
export CONTENT="mail.html"
export CONTENT_F="attachment.txt"
export SUBJECT="TEST EMAIL: TESTING HTML"
BOUNDARY='=== This is the boundary between parts of the message. ==='
{
print - "From: Someone <$MAILFROM>"
print - "To: Someone <${MAILTO}>"
print - 'Subject:' $SUBJECT
print - 'MIME-Version: 1.0'
print - 'Content-Type: MULTIPART/MIXED; '
print - ' BOUNDARY='\"$BOUNDARY\"
print -
print - ' This message is in MIME format. But if you can see this,'
print - " you aren't using a MIME aware mail program. You shouldn't "
print - ' have too many problems because this message is entirely in'
print - ' ASCII and is designed to be somewhat readable with old '
print - ' mail software.'
print -
print - "--${BOUNDARY}"
print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII'
print -
cat $CONTENT
print -
print -
print - "--${BOUNDARY}"
print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name='${CONTENT}
print - 'Content-Disposition: attachment; filename='${CONTENT_F}
print -
cat ${CONTENT}
print -
print - "--${BOUNDARY}--"
} | /usr/lib/sendmail ${MAILTO}
------------------------------------------------------------
cib-sokay2{u384283}326:cat test_html4.sh
#!/usr/bin/ksh
export MAILTO="abc@abc.com"
export CONTENT="mail.html"
export SUBJECT="subject of email"
(
echo "Subject: $SUBJECT"
# This appears in the mail body
cat $CONTENT
# The next line creates the attachment with a suitable extension to read
# with Windows notepad
unix2dos "attachment.txt" | uuencode myattach.txt
echo "."
) | /usr/lib/sendmail $MAILTO
-------------------------------------
答案 0 :(得分:0)
你的第一次尝试相当接近。您的第二次尝试似乎是将某些内容写入文件然后放弃,并将空输入提供给sendmail
。你的第三次尝试在MIME边界有一个严重的错误和一些特点(为什么你把相同的内容两次?),但基本上与第一次非常相似。你的第四次尝试看起来相当合理但过时,但由于对事物需要格式化的错误假设(例如,如果$CONTENT
不是以空行开头,你的内容会变得轻而易举,可能会失败)在邮件标题中,创建非法标题并可能被拒绝)。
假设您的系统上有一个命令base64
(自6.0, almost ten years back以来它包含在GNU Coreutils中),只需将uuencode
调用替换为该命令,并修复各种次要格式错误。这是一个重构的脚本,希望能够捕获大部分脚本。
#!/bin/sh
# No need to export anything, we are not passing these to child processes
MAILTO="abc@abc.com"
SUBJECT="Mail Subject"
BODY="card_summary_mail.html"
ATTACH="query5_result.csv"
( cat <<____HERE
To: $MAILTO
Subject: $SUBJECT
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"
... You could put a MIME preamble here but nobody ever reads it ...
---q1w2e3r4t5
Content-Type: text/html
Content-Disposition: inline
X-Notice: you need an empty line before the body here:
____HERE
cat "$BODY"
# also notice empty line at beginning of next snippet
cat <<____THERE
---q1w2e3r4t5
X-Notice: just "application" is incompletely specified
Content-Type: application/octet-stream; name="$(basename "$ATTACH")"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$(basename "$ATTACH")"
X-Notice: another empty line
____THERE
base64 "$ATTACH"
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail "$MAILTO"
但是,组装自己的MIME结构会非常快 - 我建议寻找具有适当附件支持的命令行MUA(mutt
很受欢迎;一些现代mail
/ {{1克隆有类似的设施,但它不是标准的。)
此外,这里没有mailx
特定的内容,所以我改变了shebang。
答案 1 :(得分:0)
Your first attempt is almost correct. Just replace --base64 by -m as shown below :
#!/usr/bin/ksh
export MAILTO="abc@abc.com"
export SUBJECT="Mail Subject"
export BODY="card_summary_mail.html"
export ATTACH="query5_result.csv"
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode -m $ATTACH $(basename $ATTACH)
echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail $MAILTO