python使用smtplib模块发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python使用smtplib模块发送电子邮件相关的知识,希望对你有一定的参考价值。

模式示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
import string

HOST="smtp.gmail.com"
SUBJECT="this is a test"
TO="[email protected]"
FROM="[email protected]"
text="let us try to get python mail"
BODY=string.join((
	"From:%s" % FROM,
	"To:%s" % TO,
	"Subject:%s" % SUBJECT,
	"-------------------------------------------",
	"-------------------------------------------",
	text
	),"\r\n")
server=smtplib.SMTP()
server.connect(HOST,"25")
server.starttls()
server.login(FROM,"mypasswd")
server.sendmail(FROM,[TO],BODY)
server.quit()


示例1:带有html格式的报表邮件

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText

HOST="smtp.163.com"
FROM="[email protected]"
TO="[email protected]"
SUBJECT="数据报表详情"
msg=MIMEText("""
html 内容

"""
)

msg['Subject']=SUBJECT
msg['From']=FROM
msg['To']=TO
try:
	server=smtplib.SMTP()
	server.connect(HOST,"25")
	server.starttls()
	server.login(FROM,"password")
	server.sendmail(FROM,TO,msg.as_string())
	server.quit()
	print "邮件已发送"
except Exception,e:
	print "失败:"+str(e)


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

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

HOST="smtp.qq.com"
FROM="[email protected]"
TO="[email protected]"
SUBJECT="服务器数据报表"

def adding(src,imgid):
	fp=open(src,'rb')
	msgImage=MIMEImage(fp.read())
	fp.close()
	msgImage.add_header('Content-ID',imgid)
	return msgImage

msg=MIMEMultipart('related')
msgtext=MIMEText("""

""")
msg.attach(msgtext)
msg.attach(adding("/data/image/test.png","io"))
msg.attach(adding("/data/image/good.png","disk"))
msg.attach(adding("/data/image/try.png","cpu"))
msg.attach(adding("/data/image/mem.png","mem"))
msg['Subject']=SUBJECT
msg['From']=FROM
msg['To']=TO

try:
	server=smtplib.SMTP()
	server.connect(HOST,"25")
	server.starttls()
	server.login(FROM,"password")
	server.sendmail(FROM,TO,msg.as_string())
	server.quit()
	print "邮件已发送"
except Exception,e:
	print "失败:"+str(e)
	
--------------------------------------------------

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

HOST="smtp.qq.com"
FROM="[email protected]"
TO="[email protected]"
SUBJECT="业务月报信息"

def adding(src,imgid):
	fp=open(src,'rb')
	msgImage=MIMEImage(fp.read())
	fp.close()
	msgImage.add_header('Content-ID',imgid)
	return msgImage
	
msg=MIMEMultipart('related')
msgtext=MIMEText("<font color=red> 业务报表:<br><img src=\"cid:yuebao\"border=\"1\"></br>详见附件  </font>","html","utf-8")
msg.attach(msgtext)
msg.attach(adding("/data/image/yuebao.png","yuebao"))

attach=MIMEText(open("/data/yuebao.xlsx","rb").read(),"base64","utf-8")
attach["Content-Type"]="application/octet-stream"
attach["Content-Disposition"]="attachment;filename=\"月报.xlsx\"
#attach["Content-Disposition"]="attachment;filename=\"月报.xlsx\"".decode("utf-8").encode("gb18030")
msg.attach(attach)
msg['Subject']=SUBJECT
msg['From']=FROM
msg['To']=TO

try:
	server=smtplib.SMTP()
	server.connect(HOST,"25")
	server.starttls()
	server.login(FROM,"password")
	server.sendmail(FROM,TO,msg.as_string())
	server.quit()
	print "邮件已发送"
except Exception,e:
	print "失败:"+str(e)


注意:采用SMTP协议,那么需要确保自己的发送邮箱地址开启了SMTP服务

技术分享图片

点击开启后,生产授权码,上面示例代码中的password 就是授权码,不是自己的密码

    










以上是关于python使用smtplib模块发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

Python:使用 smtplib 模块发送电子邮件时未显示“主题”

python使用smtplib模块发送电子邮件

Python3 使用smtplib和email模块发送邮件

python使用电子邮件模块smtplib的方法(发送图片 附件)实用可行

python模块----yagmail模块smtplib模块 (电子邮件)

python模块----yagmail模块smtplib模块 (电子邮件)