python3.5 email實(shí)現(xiàn)發(fā)送郵件功能
更新時(shí)間:2018年05月22日 11:53:00 投稿:lijiao
這篇文章主要為大家詳細(xì)介紹了python3.5 email實(shí)現(xiàn)發(fā)送郵件功能,包含txt、圖片、HTML、附件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python3.5 email發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
直接套用代碼即可
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email import encoders
import smtplib
import time
def send_mail(subject):
email_host = '' # 服務(wù)器地址
sender = '' # 發(fā)件人
password = '' # 密碼,如果是授權(quán)碼就填授權(quán)碼
receiver = '' # 收件人
msg = MIMEMultipart()
msg['Subject'] = subject # 標(biāo)題
msg['From'] = '' # 發(fā)件人昵稱
msg['To'] = '' # 收件人昵稱
signature = '''
\n\t this is auto test report!
\n\t you don't need to follow
'''
# text = MIMEText(signature, 'plain') # 簽名
# msg.attach(text)
# 正文-圖片 只能通過html格式來放圖片,所以要注釋25,26行
mail_msg = '''
<p>\n\t this is auto test report!</p>
<p>\n\t you don't need to follow</p>
<p><a rel="external nofollow" >我的博客:</a></p>
<p>截圖如下:</p>
<p><img src="cid:image1"></p>
'''
msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))
# 指定圖片為當(dāng)前目錄
fp = open(r'111.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定義圖片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
# 附件-圖片
image = MIMEImage(open(r'111.jpg', 'rb').read(), _subtype=subtype)
image.add_header('Content-Disposition', 'attachment', filename='img.jpg')
msg.attach(image)
# 附件-文件
file = MIMEBase(maintype, subtype)
file.set_payload(open(r'320k.txt', 'rb').read())
file.add_header('Content-Disposition', 'attachment', filename='test.txt')
encoders.encode_base64(file)
msg.attach(file)
# 發(fā)送
smtp = smtplib.SMTP()
smtp.connect(email_host, 25)
smtp.login(sender, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print('success')
if __name_- == '__main__':
now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
subject = now + '自動(dòng)化測(cè)試報(bào)告'
send_mail(subject)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python smtplib發(fā)送多個(gè)email聯(lián)系人的實(shí)現(xiàn)
- python email smtplib模塊發(fā)送郵件代碼實(shí)例
- python中使用smtplib和email模塊發(fā)送郵件實(shí)例
- 用smtplib和email封裝python發(fā)送郵件模塊類分享
- 詳解Python發(fā)送email的三種方式
- Python使用QQ郵箱發(fā)送Email的方法實(shí)例
- Python使用email模塊對(duì)郵件進(jìn)行編碼和解碼的實(shí)例教程
- 在Python的Flask框架中驗(yàn)證注冊(cè)用戶的Email的方法
- Python發(fā)送email的3種方法
- Python實(shí)現(xiàn)發(fā)送email的幾種常用方法
- Python調(diào)用SMTP服務(wù)自動(dòng)發(fā)送Email的實(shí)現(xiàn)步驟
相關(guān)文章
python 根據(jù)pid殺死相應(yīng)進(jìn)程的方法
下面小編就為大家?guī)硪黄猵ython 根據(jù)pid殺死相應(yīng)進(jìn)程的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Python基于pygame實(shí)現(xiàn)的彈力球效果(附源碼)
這篇文章主要介紹了Python基于pygame實(shí)現(xiàn)的彈力球效果,涉及pygame圖形動(dòng)態(tài)操作的相關(guān)的技巧,并附帶了完整的源碼供讀者下載參考,需要的朋友可以參考下2015-11-11

