需要在Unix中发送带有附件的html邮件 [英] need to send html mail with attachment in unix

查看:95
本文介绍了需要在Unix中发送带有附件的html邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Attachemnt发送html邮件时遇到问题. 使用mailx -s和uuencode命令以及html邮件进行附件(纯文本) 没有使用sendmail选项的附件.

I am facing problems in sending html mail with attachemnt.I will able to send mail with attachment (plain text ) using mailx -s and uuencode command and also html mail without attachment using sendmail option.

但是,我无法发送带有附件的html邮件. 两者之一都在工作(html邮件或附件)

However I am not able to send html mail along with attachment. Either one of it is working (html mail or attachment)

以下是我尝试过的不同方法.您能帮我解决这个问题吗?

Below are the different ways I have tried. Could you please help me in resolving the same.

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

-------------------------------------

推荐答案

您的第一次尝试非常接近.您的第二次尝试似乎是向文件中写入一些内容,然后放弃该文件,然后将空输入提供给sendmail.您的第三次尝试在MIME边界上有一个严重错误,并且有一些特殊性(为什么您将相同的内容放两次?),但是基本上与第一次尝试非常相似.您的第四次尝试看起来相当合理,但是过时了,但是由于关于如何格式化内容的错误假设而可能失败(例如,如果$CONTENT不是以空行开头,则您的内容将被打邮件标头,创建非法标头并可能被拒绝).

Your first attempt is fairly close. Your second attempt seems to write something to a file which is then abandoned, and you feed empty input to sendmail. Your third attempt has a grave error in the MIME boundary and some peculiarities (why do you put the same content twice?), but is basically fairly similar to the first. Your fourth attempt looks fairly reasonable but old-fashioned, but could have failed because of false assumptions about how things need to be formatted (for example, if $CONTENT does not start with an empty line, your contents would go smack dab in the message headers, creating illegal headers and probably being rejected).

假设您的系统上有命令base64(自

Assuming you have a command base64 on your system (it is included in GNU Coreutils since 6.0, almost ten years back), just replace the uuencode call with that, and fix the various minor formatting errors. Here is a refactored script which hopefully catches most of them.

#!/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/mailx克隆具有类似的功能,但这不是标准).

However, assembling your own MIME structure gets tired really fast -- I would recommend looking for a command-line MUA with proper attachment support (mutt is popular; some modern mail/mailx clones have similar facilities, but it's not standard).

此外,这里没有ksh特有的内容,所以我更改了shebang.

Also, there is nothing ksh-specific here, so I changed the shebang.

这篇关于需要在Unix中发送带有附件的html邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆