Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python发送HTML丰富的电子邮件_Python_Html_Smtplib - Fatal编程技术网

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

使用python发送HTML丰富的电子邮件,python,html,smtplib,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 ema

我正在尝试发送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
从email.MIMEMultipart导入MIMEMultipart
从email.MIMEBase导入MIMEBase
从email.MIMEText导入MIMEText
从email.Utils导入COMMASPACE,formatdate
从电子邮件导入编码器
导入smtplib
类电子邮件(对象):
“”“类定义了发送附件的电子邮件的方法
"""
def u uu init u uuu(self、sendto、mailFrom、server、usrname、密码、文件、debug=False、subject=None):
self.debug=debug
self.mailto=sendto
self.mailFrom=mailFrom
self.smtpserver=server
self.EMAIL_PORT=587
self.usrname=usrname
self.password=密码
self.subject=subject
#self.send(文件)
def sendMessage(自身、msgContent、文件):
#收集信息并准备电子邮件
如果文件:
如果self.subject==“”:
#主题应包含所附文件的内容
如果len(files)3:subquadd=','.join(files[:3])+'…'
self.subject=self.systemLogin()+“从maya发送邮件”+os.path.basename(subjectadd)
打印“主题:”,self.subject
msg=self.prepareMail(self.mailFrom、self.mailto、self.subject、msgContent、文件)
#连接到服务器并发送电子邮件
服务器=smtplib.SMTP(self.smtpserver,端口=self.EMAIL\u端口)
server.ehlo()
server.starttls()#使用加密SSL模式
server.ehlo()#使starttls工作
server.login(self.usrname、self.password)
server.set_debuglevel(self.debug)
尝试:
failed=server.sendmail(From、to、msg.as_string())
除作为er的例外情况外:
印刷机
最后:
server.quit()
def PrepareEmail(自我、发件人、收件人、主题、msgHTML、附件):
msg=MIMEMultipart()
msg['From']=From
msg['To']=To
msg['Date']=formattate(localtime=True)
msg['Subject']=主语
#身体信息
附加(MIMEText(msgHTML,'html'))
msg.attach(MIMEText(“由Mini-Me从maya发送”))
如有附件:
对于附件中的phile:
#我们可以在这里检查mimetype
part=MIMEBase(“应用程序”、“八位字节流”)
部分设置有效载荷(打开(phile,“rb”).read())
编码器。编码_base64(部分)
part.add_头('Content-Disposition','attachment;filename=“%s”'%os.path.basename(phile))
附加信息(部分)
返回消息
这是我正在发送的HTML格式的文本,(由于css的原因,我删除了HTML标题部分)


---
send.py | 4+---
1个文件已更改,1个插入(+),3个删除(-)
diff——git a/send.py b/send.py
索引87126d5..abb1fd8 100644
---a/send.py
+++b/send.py
@@-49,14+49,12@@class电子邮件(对象):
server.quit()
def PrepareEmail(自我、发件人、收件人、主题、msgHTML、附件):
-msg=MIMEMultipart('alternative')
+msg=MIMEMultipart()
msg['From']=From
msg['To']=To
msg['Date']=formattate(localtime=True)
msg['Subject']=主语
-打印msgHTML
-
#身体信息
附加(MIMEText(msgHTML,'html'))
msg.attach(MIMEText(“由Mini-Me从maya发送”))
-- 
1.8.3.4(苹果Git-47)
似乎只有标记前格式有效,而不是css。。为什么会这样?Gmail不支持
块。您可以在此处看到流行邮件客户端之间的css支持比较:。

并非所有电子邮件客户端都能够呈现css,有些只是内联呈现styles@furins:那gmail呢?请看zord的答案(截至今天:不,它不支持
头部
正文
中的
样式
标记)。一般来说,电子邮件中的css很棘手,应该是最小的,或者可以在不影响邮件可读性的情况下优雅地降级。还请考虑到,您的电子邮件不应大于600px,以适合某些在线浏览器(例如,任何图像都不应大于该尺寸)。最终,像这样的工具可能会对您有所帮助。好吧,我找到了一种从python for git diff中嵌入样式内联的方法,我将在单独的线程中共享并链接到这里。
<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>