python发邮件实例_python 发送邮件实例
文本形式發(fā)郵件
#!/usr/bin/env?python3
#coding:?utf-8
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.headerimportHeader
sender?=?'***'
receiver?=?'***'
subject?=?'python?email?test'
smtpserver?=?'smtp.163.com'
username?=?'***'
password?=?'***'
msg?=?MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8’,單字節(jié)字符不需要
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形式發(fā)郵件
#!/usr/bin/env?python3
#coding:?utf-8
importsmtplib
fromemail.mime.textimportMIMEText
sender?=?'***'
receiver?=?'***'
subject?=?'python?email?test'
smtpserver?=?'smtp.163.com'
username?=?'***'
password?=?'***'
msg?=?MIMEText('
你好
','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形式發(fā)郵件
#!/usr/bin/env?python3
#coding:?utf-8
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.p_w_picpathimportMIMEImage
sender?=?'***'
receiver?=?'***'
subject?=?'python?email?test'
smtpserver?=?'smtp.163.com'
username?=?'***'
password?=?'***'
msgRoot?=?MIMEMultipart('related')
msgRoot['Subject']?='test?message'
msgText?=?MIMEText('Some?HTML?text?and?an?p_w_picpath.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','')
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
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.p_w_picpathimportMIMEImage
sender?=?'***'
receiver?=?'***'
subject?=?'python?email?test'
smtpserver?=?'smtp.163.com'
username?=?'***'
password?=?'***'
msgRoot?=?MIMEMultipart('related')
msgRoot['Subject']?='test?message'
#構(gòu)造附件
att?=?MIMEText(open('h:\\python\\1.jpg','rb').read(),'base64','utf-8')
att["Content-Type"]?='application/octet-stream'
att["Content-Disposition"]?='p_w_upload;?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()
總結(jié)
以上是生活随笔為你收集整理的python发邮件实例_python 发送邮件实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 程序猿bug修改名句
- 下一篇: 同一个页面提交多个form表单方法(详细