本篇文章主要介绍了python 发送邮件实例代码,详细的介绍了各种方式发送邮件,包括文件形式的邮件、html形式的邮件、带图片的html邮件等,有兴趣的可以了解一下。
python 发送邮件实例
文件形式的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from emailmimetext import mimetext
from emailheader import header
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtpcom’
username = ‘***’
password = ‘***’
msg = mimetext(‘你好’,’text’,’utf-8′)#中文需参数‘utf-8’,单字节字符不需要
msg[‘subject’] = header(subject, ‘utf-8’)
smtp = smtplibsmtp()
smtpconnect(‘smtpcom’)
smtplogin(username, password)
smtpsendmail(sender, receiver, msgas_string())
smtpquit()
html形式的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from emailmimetext import mimetext
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtpcom’
username = ‘***’
password = ‘***’
msg = mimetext(‘你好’,’html’,’utf-8′)
msg[‘subject’] = subject
smtp = smtplibsmtp()
smtpconnect(‘smtpcom’)
smtplogin(username, password)
smtpsendmail(sender, receiver, msgas_string())
smtpquit()
带图片的html邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from emailmimemultipart import mimemultipart
from emailmimetext import mimetext
from emailmimeimage import mimeimage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtpcom’
username = ‘***’
password = ‘***’
msgroot = mimemultipart(‘related’)
msgroot[‘subject’] = ‘test message’
msgtext = mimetext(‘some html text and an imagegood!’,’html’,’utf-8′)
msgrootattach(msgtext)
fp = open(‘h:\\python\\jpg’, ‘rb’)
msgimage = mimeimage(fpread())
fpclose()
msgimageadd_header(‘content-id’, ”)
msgrootattach(msgimage)
smtp = smtplibsmtp()
smtpconnect(‘smtpcom’)
smtplogin(username, password)
smtpsendmail(sender, receiver, msgrootas_string())
smtpquit()
带附件的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from emailmimemultipart import mimemultipart
from emailmimetext import mimetext
from emailmimeimage import mimeimage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtpcom’
username = ‘***’
password = ‘***’
msgroot = mimemultipart(‘related’)
msgroot[‘subject’] = ‘test message’
#构造附件
att = mimetext(open(‘h:\\python\\jpg’, ‘rb’)read(), ‘base64’, ‘utf-8’)
att[“content-type”] = ‘application/octet-stream’
att[“content-disposition”] = ‘attachment; filename=”jpg”‘
msgrootattach(att)
smtp = smtplibsmtp()
smtpconnect(‘smtpcom’)
smtplogin(username, password)
smtpsendmail(sender, receiver, msgrootas_string())
smtpquit()
群邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from emailmimetext import mimetext
sender = ‘***’
receiver = [‘***’,’****’,……]
subject = ‘python email test’
smtpserver = ‘smtpcom’
username = ‘***’
password = ‘***’
msg = mimetext(‘你好’,’text’,’utf-8′)
msg[‘subject’] = subject
smtp = smtplibsmtp()
smtpconnect(‘smtpcom’)
smtplogin(username, password)
smtpsendmail(sender, receiver, msgas_string())
smtpquit()
各种元素都包含的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from emailmimemultipart import mimemultipart
from emailmimetext import mimetext
from emailmimeimage import mimeimage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtpcom’
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:\n#”
html = “””\
hi!
how are you?
here is the link you wanted
“””
# 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
msgattach(part1)
msgattach(part2)
#构造附件
att = mimetext(open(‘h:\\python\\jpg’, ‘rb’)read(), ‘base64’, ‘utf-8’)
att[“content-type”] = ‘application/octet-stream’
att[“content-disposition”] = ‘attachment; filename=”jpg”‘
msgattach(att)
smtp = smtplibsmtp()
smtpconnect(‘smtpcom’)
smtplogin(username, password)
smtpsendmail(sender, receiver, msgas_string())
smtpquit()
基于ssl的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from emailmimetext import mimetext
from emailheader import header
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtpcom’
username = ‘***’
password = ‘***’
msg = mimetext(‘你好’,’text’,’utf-8′)#中文需参数‘utf-8’,单字节字符不需要
msg[‘subject’] = header(subject, ‘utf-8’)
smtp = smtplibsmtp()
smtpconnect(‘smtpcom’)
smtpehlo()
smtpstarttls()
smtpehlo()
smtpset_debuglevel(1)
smtplogin(username, password)
smtpsendmail(sender, receiver, msgas_string())
smtpquit()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持php中文网。
更多详解python 发送邮件实例代码相关文章请关注php中文网!