python发送邮件的实例代码(支持html、图片、附件)
第一段代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import email
import mimetypes
from email.mimemultipart import mimemultipart
from email.mimetext import mimetext
from email.mimeimage import mimeimage
import smtplib
def sendemail(authinfo, fromadd, toadd, subject, plaintext, htmltext):
strfrom = fromadd
strto = ', '.join(toadd)
server = authinfo.get('server')
user = authinfo.get('user')
passwd = authinfo.get('password')
if not (server and user and passwd) :
print 'incomplete login info, exit now'
return
# 设定root信息
msgroot = mimemultipart('related')
msgroot['subject'] = subject
msgroot['from'] = strfrom
msgroot['to'] = strto
msgroot.preamble = 'this is a multi-part message in mime format.'
# encapsulate the plain and html versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgalternative = mimemultipart('alternative')
msgroot.attach(msgalternative)
#设定纯文本信息
msgtext = mimetext(plaintext, 'plain', 'utf-8')
msgalternative.attach(msgtext)
#设定html信息
msgtext = mimetext(htmltext, 'html', 'utf-8')
msgalternative.attach(msgtext)
#设定内置图片信息
fp = open('test.jpg', 'rb')
msgimage = mimeimage(fp.read())
fp.close()
msgimage.add_header('content-id', '<image1>')
msgroot.attach(msgimage)
#发送邮件
smtp = smtplib.smtp()
#设定调试级别,依情况而定
smtp.set_debuglevel(1)
smtp.connect(server)
smtp.login(user, passwd)
smtp.sendmail(strfrom, strto, msgroot.as_string())
smtp.quit()
return
if __name__ == '__main__' :
authinfo = {}
authinfo['server'] = 'smtp.somehost.com'
authinfo['user'] = 'username'
authinfo['password'] = 'password'
fromadd = 'username@somehost.com'
toadd = ['someone@somehost.com', 'other@somehost.com']
subject = '邮件主题'
plaintext = '这里是普通文本'
htmltext = '<b>html文本</b>'
sendemail(authinfo, fromadd, toadd, subject, plaintext, htmltext)
文件形式的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext
from email.header import header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = mimetext('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['subject'] = header(subject, 'utf-8')
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
html形式的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = mimetext('<html><h1>你好</h1></html>','html','utf-8')
msg['subject'] = subject
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
带图片的html邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.image import mimeimage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msgroot = mimemultipart('related')
msgroot['subject'] = 'test message'
msgtext = mimetext('<b>some <i>html</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')
msgroot.attach(msgtext)
fp = open('h:\\python\\1.jpg', 'rb')
msgimage = mimeimage(fp.read())
fp.close()
msgimage.add_header('content-id', '<image1>')
msgroot.attach(msgimage)
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgroot.as_string())
smtp.quit()
带附件的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.image import mimeimage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msgroot = mimemultipart('related')
msgroot['subject'] = 'test message'
#构造附件
att = mimetext(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["content-type"] = 'application/octet-stream'
att["content-disposition"] = 'attachment; filename="1.jpg"'
msgroot.attach(att)
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgroot.as_string())
smtp.quit()
群邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext
sender = '***'
receiver = ['***','****',……]
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = mimetext('你好','plain','utf-8')
msg['subject'] = subject
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
各种元素都包含的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.image import mimeimage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
# create message container - the correct mime type is multipart/alternative.
msg = mimemultipart('alternative')
msg['subject'] = "link"
# create the body of the message (a plain-text and an html version).
text = "hi!\nhow are you?\nhere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>hi!<br>
how are you?<br>
here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
# record the mime types of both parts - text/plain and text/html.
part1 = mimetext(text, 'plain')
part2 = mimetext(html, 'html')
# attach parts into message container.
# according to rfc 2046, the last part of a multipart message, in this case
# the html message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att = mimetext(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["content-type"] = 'application/octet-stream'
att["content-disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
基于ssl的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import mimetext
from email.header import header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = mimetext('你好','plain','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['subject'] = header(subject, 'utf-8')
smtp = smtplib.smtp()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
上一篇: 数字城管智能管控平台介绍
下一篇: 业务用户的数据自主分析