python實(shí)現(xiàn)發(fā)送郵件功能
本文實(shí)例為大家分享了python實(shí)現(xiàn)發(fā)送郵件功能的具體代碼,供大家參考,具體內(nèi)容如下
依賴(lài):
Python代碼實(shí)現(xiàn)發(fā)送郵件,使用的模塊是smtplib、MIMEText,實(shí)現(xiàn)代碼之前需要導(dǎo)入包:
import smtplib from email.mime.text import MIMEText
使用163郵件發(fā)送郵件,具體代碼實(shí)現(xiàn)如下:
import smtplib
from email.mime.text import MIMEText
'''
發(fā)送郵件函數(shù),默認(rèn)使用163smtp
:param mail_host: 郵箱服務(wù)器,16郵箱host: smtp.163.com
:param port: 端口號(hào),163郵箱的默認(rèn)端口是 25
:param username: 郵箱賬號(hào) xx@163.com
:param passwd: 郵箱密碼(不是郵箱的登錄密碼,是郵箱的授權(quán)碼)
:param recv: 郵箱接收人地址,多個(gè)賬號(hào)以逗號(hào)隔開(kāi)
:param title: 郵件標(biāo)題
:param content: 郵件內(nèi)容
:return:
'''
def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25):
msg = MIMEText(content) # 郵件內(nèi)容
msg['Subject'] = title # 郵件主題
msg['From'] = username # 發(fā)送者賬號(hào)
msg['To'] = recv # 接收者賬號(hào)列表
smtp = smtplib.SMTP(mail_host, port=port) # 連接郵箱,傳入郵箱地址,和端口號(hào),smtp的端口號(hào)是25
smtp.login(username, passwd) # 登錄發(fā)送者的郵箱賬號(hào),密碼
# 參數(shù)分別是 發(fā)送者,接收者,第三個(gè)是把上面的發(fā)送郵件的 內(nèi)容變成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 發(fā)送完畢后退出smtp
print('email send success.')
if __name__ == '__main__':
email_user = 'xxxx@163.com' # 發(fā)送者賬號(hào)
email_pwd = 'xxxxx' # 發(fā)送者密碼,授權(quán)碼
maillist = 'xxxx@qq.com'
title = '測(cè)試郵件標(biāo)題'
content = '這里是郵件內(nèi)容'
send_mail(email_user, email_pwd, maillist, title, content)
163郵箱的授權(quán)碼獲取方法如下:
1. 登錄163郵箱,點(diǎn)擊設(shè)置-POP3/SMTP/IMAP,如下:

2. 開(kāi)啟SMTP服務(wù)且可以查詢(xún)SMTP的host地址:

3. 點(diǎn)擊客戶(hù)端授權(quán)密碼,可以重置授權(quán)碼:

使用QQ郵件發(fā)送郵件,具體代碼實(shí)現(xiàn)如下:
import smtplib
from email.mime.text import MIMEText
'''
發(fā)送郵件函數(shù),默認(rèn)使用163smtp
:param mail_host: 郵箱服務(wù)器,qq郵箱host: smtp.qq.com
:param port: 端口號(hào),qq郵箱的默認(rèn)端口是: 456
:param username: 郵箱賬號(hào) xx@163.com
:param passwd: 郵箱密碼(不是郵箱的登錄密碼,是郵箱的授權(quán)碼)
:param recv: 郵箱接收人地址,多個(gè)賬號(hào)以逗號(hào)隔開(kāi)
:param title: 郵件標(biāo)題
:param content: 郵件內(nèi)容
:return:
'''
#qq郵箱發(fā)送郵件
def send_mail(username, passwd, recv, title, content, mail_host='smtp.qq.com', port=456):
msg = MIMEText(content) # 郵件內(nèi)容
msg['Subject'] = title # 郵件主題
msg['From'] = username # 發(fā)送者賬號(hào)
msg['To'] = recv # 接收者賬號(hào)列表
smtp = smtplib.SMTP_SSL(mail_host, port=port) # 連接郵箱,傳入郵箱地址,和端口號(hào),smtp的端口號(hào)是25
smtp.login(username, passwd) # 登錄發(fā)送者的郵箱賬號(hào),密碼
# 參數(shù)分別是 發(fā)送者,接收者,第三個(gè)是把上面的發(fā)送郵件的 內(nèi)容變成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 發(fā)送完畢后退出smtp
print('email send success.')
if __name__ == '__main__':
email_user = 'xxx@qq.com' # 發(fā)送者賬號(hào)
email_pwd = 'WOSHINIGE123' # 發(fā)送者密碼,授權(quán)碼
maillist = 'xxxx@qq.com'
title = '測(cè)試郵件標(biāo)題'
content = '這里是郵件內(nèi)容'
send_mail(email_user, email_pwd, maillist, title, content)
多個(gè) 收件人且?guī)Ц郊?/p>
# import smtplib
# from email.mime.text import MIMEText
# '''
# 發(fā)送郵件函數(shù),默認(rèn)使用163smtp
# :param mail_host: 郵箱服務(wù)器,qq郵箱host: smtp.163.com
# :param port: 端口號(hào),qq郵箱的默認(rèn)端口是: 25
# :param username: 郵箱賬號(hào) xx@163.com
# :param passwd: 郵箱密碼(不是郵箱的登錄密碼,是郵箱的授權(quán)碼)
# :param recv: 郵箱接收人地址,多個(gè)賬號(hào)以逗號(hào)隔開(kāi)
# :param title: 郵件標(biāo)題
# :param content: 郵件內(nèi)容
# :return:
# '''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#郵箱服務(wù)器地址
email_host = 'smtp.163.com'
#發(fā)送者
email_user = 'xxxx@163.com'
#授權(quán)碼
email_pwd = 'WOSHINIGE123'
#多個(gè)收件人,使用分號(hào)分隔
maillist ='xxxx@qq.com;aaaa@qq.com'
#對(duì)多個(gè)收件人進(jìn)行分割,以;為標(biāo)準(zhǔn),返回結(jié)果是list['xxxx@qq.com','aaaa@qq.com']
email_info = maillist.split(';')
#構(gòu)造一個(gè)發(fā)附件的郵件內(nèi)容對(duì)象
new_msg = MIMEMultipart()
#郵件正文內(nèi)容
new_msg.attach(MIMEText('test email.....'))
#郵件主題
new_msg['Subject'] = 'test email'
#郵件發(fā)送者
new_msg['From'] = email_user
#郵件接收者,多個(gè)收件人的賬號(hào)使用,連接,傳入類(lèi)型是str
new_msg['To'] = ','.join(email_info)
#打開(kāi)a.txt讀取文本內(nèi)容
att = MIMEText(open('a.txt').read())
att["Content-Type"] = 'application/octet-stream'
# 發(fā)送附件就得這么寫(xiě),固定格式,filename指定附件的名字
att["Content-Disposition"] = 'attachment; filename="haha.txt"'
new_msg.attach(att)
# 連接郵箱,傳入郵箱地址,和端口號(hào),smtp的端口號(hào)是25
smtp = smtplib.SMTP(email_host, port=25)
# 發(fā)送者的郵箱賬號(hào),密碼,先登錄
smtp.login(email_user, email_pwd)
smtp.sendmail(email_user, email_info, new_msg.as_string())
smtp.quit()
print('email send success.')
多個(gè)收件人時(shí)賬號(hào)之間使用分號(hào);分割,進(jìn)行smtp.sendmail(傳入的多個(gè)收件人類(lèi)型是list);new_msg['TO'] = recv,接收的類(lèi)型是str
使用類(lèi)實(shí)現(xiàn)郵件的發(fā)送,即可發(fā)送多人也可發(fā)送附件:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class SendMail(object):
def __init__(self, username, passwd, recv, title, content, file=None, email_host='smtp.163.com', port=25):
self.username = username
self.passwd = passwd
self.recv = recv
self.title = title
self.content = content
self.file = file
self.email_host = email_host
self.port = port
def send_mail(self):
msg = MIMEMultipart()
#發(fā)送內(nèi)容的對(duì)象
if self.file:#處理附件的
att = MIMEText(open(self.file, encoding='utf-8').read())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"'%self.file
msg.attach(att)
msg.attach(MIMEText(self.content))#郵件正文的內(nèi)容
msg['Subject'] = self.title # 郵件主題
msg['From'] = self.username # 發(fā)送者賬號(hào)
#將多個(gè)賬號(hào)'aaa.@qq.com;bbbb@163.com' 以分號(hào)分割,分割為list
self.recv = self.recv.split(';')
if isinstance(self.recv, list):
msg['To'] = ','.join(self.recv)
else:
msg['To'] = self.recv # 接收者賬號(hào)列表
if self.username.endswith('qq.com'): #如果發(fā)送者是qq郵箱
self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.port)
else:
self.smtp = smtplib.SMTP(self.email_host, port=self.port)
#發(fā)送郵件服務(wù)器的對(duì)象
self.smtp.login(self.username, self.passwd)
try:
self.smtp.sendmail(self.username, self.recv, msg.as_string())
except Exception as e:
print('出錯(cuò)了。。', e)
else:
print('發(fā)送成功!')
self.smtp.quit()
if __name__ == '__main__':
m = SendMail(
username='zzzzz@163.com', passwd='xxxxxxx',file='a.txt', recv='xxxxxx@qq.com;xxxxxx@qq.com',
title='多個(gè)收件人', content='哈哈哈啊哈哈哈哈', email_host='smtp.163.com', port=25
)
m.send_ma
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python自動(dòng)發(fā)送郵件的方法實(shí)例總結(jié)
- Python發(fā)送郵件功能示例【使用QQ郵箱】
- python發(fā)送郵件的實(shí)例代碼(支持html、圖片、附件)
- python中使用smtplib和email模塊發(fā)送郵件實(shí)例
- Python實(shí)現(xiàn)給qq郵箱發(fā)送郵件的方法
- python同時(shí)給兩個(gè)收件人發(fā)送郵件的方法
- 利用Python自動(dòng)監(jiān)控網(wǎng)站并發(fā)送郵件告警的方法
- python發(fā)送郵件示例(支持中文郵件標(biāo)題)
- python監(jiān)控網(wǎng)站運(yùn)行異常并發(fā)送郵件的方法
- python發(fā)送郵件接收郵件示例分享
- 詳解Python發(fā)送郵件實(shí)例
- Python發(fā)送郵件測(cè)試報(bào)告操作實(shí)例詳解
相關(guān)文章
Python使用文件鎖實(shí)現(xiàn)進(jìn)程間同步功能【基于fcntl模塊】
這篇文章主要介紹了Python使用文件鎖實(shí)現(xiàn)進(jìn)程間同步功能,結(jié)合實(shí)例形式分析了Python基于fcntl模塊文件鎖功能實(shí)現(xiàn)進(jìn)程間同步的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Django contrib auth authenticate函數(shù)源碼解析
這篇文章主要介紹了Django contrib auth authenticate函數(shù)源碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Python量化因子測(cè)算與繪圖超詳細(xì)流程代碼
這篇文章主要介紹了Python量化因子測(cè)算與繪圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02
OpenCV實(shí)現(xiàn)圖像平滑處理的方法匯總
這篇文章為大家詳細(xì)介紹了在圖像上面進(jìn)行了圖像均值濾波、方框?yàn)V波 、高斯濾波、中值濾波、雙邊濾波、2D卷積等具體操作的方法,需要的可以參考一下2023-02-02
python pandas實(shí)現(xiàn)excel轉(zhuǎn)為html格式的方法
今天小編就為大家分享一篇python pandas實(shí)現(xiàn)excel轉(zhuǎn)為html格式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10

