python3實(shí)現(xiàn)帶多張圖片、附件的郵件發(fā)送
本文實(shí)例為大家分享了python3實(shí)現(xiàn)多張圖片附件郵件發(fā)送的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼,沒(méi)有注釋!
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header
class Mail(object):
def __init__(self, host, nickname, username, password, postfix):
self.host = host
self.nickname = nickname
self.username = username
self.password = password
self.postfix = postfix
def send_mail(self, to_list, subject, content, cc_list=[], encode='gbk', is_html=True, images=[]):
me = str(Header(self.nickname, encode)) + "<" + self.username + "@" + self.postfix + ">"
msg = MIMEMultipart()
msg['Subject'] = Header(subject, encode)
msg['From'] = me
msg['To'] = ','.join(to_list)
msg['Cc'] = ','.join(cc_list)
if is_html:
mail_msg = ''
for i in range(len(images)):
mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
msg.attach(MIMEText(content + mail_msg, 'html', 'utf-8'))
for i, img_name in enumerate(images):
with open(img_name, 'rb') as fp:
img_data = fp.read()
msg_image = MIMEImage(img_data)
msg_image.add_header('Content-ID', '<image%d>' % (i+1))
msg.attach(msg_image)
# 將圖片作為附件
# image = MIMEImage(img_data, _subtype='octet-stream')
# image.add_header('Content-Disposition', 'attachment', filename=images[i])
# msg.attach(image)
else:
msg_content = MIMEText(content, 'plain', encode)
msg.attach(msg_content)
try:
s = smtplib.SMTP()
# s.set_debuglevel(1)
s.connect(self.host)
s.login(self.username, self.password)
s.sendmail(me, to_list + cc_list, msg.as_string())
s.quit()
s.close()
return True
except Exception as e:
print(e)
return False
def send_mail(to_list, title, content, cc_list=[], encode='utf-8', is_html=True, images=[]):
content = '<pre>%s</pre>' % content
m = Mail('smtp.163.com', 'TV-APP TEST', 'tvapp_qa', 'ujlnluutpfespgxz', '163.com')
m.send_mail(to_list, title, content, cc_list, encode, is_html, images)
if __name__ == '__main__':
images = [
'1.png',
'2.png',
'3.png',
'4.png',
]
import time
title = 'new images %s' % time.strftime('%H:%M:%S')
content = 'this is attach images %s' % time.time()
send_mail(['x@163.com'], title, content, ['xx@163.com', 'xxx@163.com'], 'utf-8', True, images)
后記
調(diào)試發(fā)送多張圖片的時(shí)候遇到的問(wèn)題:
用for循環(huán)生成的mail_msg,不能直接attach,需要和content一起attach
mail_msg = '' for i in range(len(images)): mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1) msg.attach(MIMEText(**content** + mail_msg, 'html', 'utf-8'))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python程序設(shè)計(jì)入門(1)基本語(yǔ)法簡(jiǎn)介
Python是當(dāng)今日趨流行的一種腳本語(yǔ)言,它比Java更簡(jiǎn)單,比php更強(qiáng)大,并且還適用于做桌面應(yīng)用的開發(fā),這篇文章主要介紹了Python基本語(yǔ)法,需要的朋友可以參考下2014-06-06
手機(jī)使用python操作圖片文件(pydroid3)過(guò)程詳解
這篇文章主要介紹了手機(jī)使用python操作圖片文件(pydroid3)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python中安全地使用多進(jìn)程和多線程進(jìn)行數(shù)據(jù)共享
在并發(fā)編程中,有時(shí)多個(gè)線程或進(jìn)程需要訪問(wèn)共享的數(shù)據(jù),因此我們需要一些機(jī)制來(lái)確保數(shù)據(jù)的安全訪問(wèn),本文將從多線程和多進(jìn)程兩個(gè)角度探討Python如何安全地實(shí)現(xiàn)數(shù)據(jù)共享2024-12-12
Python3讀取UTF-8文件及統(tǒng)計(jì)文件行數(shù)的方法
這篇文章主要介紹了Python3讀取UTF-8文件及統(tǒng)計(jì)文件行數(shù)的方法,涉及Python讀取指定編碼文件的相關(guān)技巧,需要的朋友可以參考下2015-05-05
tensorflow ckpt模型和pb模型獲取節(jié)點(diǎn)名稱,及ckpt轉(zhuǎn)pb模型實(shí)例
今天小編就為大家分享一篇tensorflow ckpt模型和pb模型獲取節(jié)點(diǎn)名稱,及ckpt轉(zhuǎn)pb模型實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
PyAutoGUI圖形用戶界面自動(dòng)化的超詳細(xì)教程
PyautoGUI是一個(gè)純Python的自動(dòng)化工具,能實(shí)現(xiàn)用程序自動(dòng)控制鼠標(biāo)和鍵盤操作,下面這篇文章主要給大家介紹了關(guān)于PyAutoGUI圖形用戶界面自動(dòng)化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

