使用python发送HTML丰富的电子邮件

时间:2014-01-25 07:53:53

标签: python html smtplib

我正在尝试发送HTML丰富的电子邮件,到目前为止代码正在运行,但是当我签入我发送到的邮箱时,我在html邮件内容中的颜色格式没有显示。

到目前为止,这是代码:

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import smtplib

class EMail(object):
        """ Class defines method to send email of attachment
        """
        def __init__(self, sendto, mailFrom, server, usrname, password, files, debug=False, subjt=None):
                self.debug = debug
                self.mailto = sendto
                self.mailFrom = mailFrom
                self.smtpserver = server
                self.EMAIL_PORT = 587
                self.usrname = usrname
                self.password = password
                self.subject = subjt

                # self.send(files)

        def sendMessage(self, msgContent, files):

                #collect info and prepare email
                if files:
                    if self.subject == "":
                        #Subject should contains of file attached
                        if len(files) <=3: subjAdd = ','.join(files)
                        if len(files) > 3: subjAdd = ','.join(files[:3]) + '...'
                        self.subject= self.systemLogin() +" sent mail from maya "+ os.path.basename(subjAdd)
                print "subject: ", self.subject
                msg = self.prepareMail(self.mailFrom, self.mailto, self.subject, msgContent, files)

                # connect to server and send email
                server=smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
                server.ehlo()
                server.starttls()#use encrypted SSL mode
                server.ehlo() # to make starttls work
                server.login(self.usrname, self.password)
                server.set_debuglevel(self.debug)
                try:
                        failed = server.sendmail(From, to, msg.as_string())
                except Exception as er:
                        print er
                finally:
                        server.quit()

        def prepareMail(self, From, to, subject, msgHTML, attachments):
                msg = MIMEMultipart()
                msg['From'] = From
                msg['To'] = to
                msg['Date'] = formatdate(localtime=True)
                msg['Subject'] = subject

                #The Body message
                msg.attach(MIMEText(msgHTML, 'html'))
                msg.attach(MIMEText("Sent from maya by Mini Me"))
                if attachments:
                    for phile in attachments:
                            #we could check for MIMETypes here
                            part = MIMEBase('application',"octet-stream")
                            part.set_payload(open(phile, "rb").read())
                            Encoders.encode_base64(part)
                            part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(phile))
                            msg.attach(part)
                return msg

这是我发送的HTML格式的文本,(由于css,我删除了HTML头部分很长)

<body class="body_foreground body_background" style="font-size: normal;" >
<pre>

---
 send.py | 4 <span class="ansi32">+</span><span class="ansi31">---</span>
 1 file changed, 1 insertion(+), 3 deletions(-)

<span class="ansi1">diff --git a/send.py b/send.py</span>
<span class="ansi1">index 87126d5..abb1fd8 100644</span>
<span class="ansi1">--- a/send.py</span>
<span class="ansi1">+++ b/send.py</span>
<span class="ansi36">@@ -49,14 +49,12 @@</span> class EMail(object):
                        server.quit()

        def prepareMail(self, From, to, subject, msgHTML, attachments):
<span class="ansi31">-              msg = MIMEMultipart('alternative')</span>
<span class="ansi32">+</span>               <span class="ansi32">msg = MIMEMultipart()</span>
                msg['From'] = From
                msg['To'] = to
                msg['Date'] = formatdate(localtime=True)
                msg['Subject'] = subject

<span class="ansi31">-              print msgHTML</span>
<span class="ansi31">-</span>
                #The Body message
                msg.attach(MIMEText(msgHTML, 'html'))
                msg.attach(MIMEText("Sent from maya by Mini Me"))
-- 
1.8.3.4 (Apple Git-47)


</pre>
</body>

</html>

似乎只有预标签格式化工作不是css ..为什么会这样?

1 个答案:

答案 0 :(得分:3)

Gmail不支持 <style>阻止。您可以在此处查看热门邮件客户端之间的CSS支持比较:Guide to CSS support in email