簡(jiǎn)單了解python 郵件模塊的使用方法
我們?cè)陂_發(fā)程序的時(shí)候,有時(shí)候需要開發(fā)一些自動(dòng)化的任務(wù),執(zhí)行完之后,將結(jié)果自動(dòng)的發(fā)送一份郵件,python發(fā)送郵件使用smtplib模塊,是一個(gè)標(biāo)準(zhǔn)包,直接import導(dǎo)入使用即可,代碼如下:
import smtplib
from email.mime.text import MIMEText
email_host = 'smtp.163.com' #郵箱地址
email_user = 'xxxx@163.com' # 發(fā)送者賬號(hào)
email_pwd = 'xxxx' # 發(fā)送者密碼
maillist ='511402865@qq.com'
#收件人郵箱,多個(gè)賬號(hào)的話,用逗號(hào)隔開
me = email_user
msg = MIMEText('郵件發(fā)送測(cè)試內(nèi)容') # 郵件內(nèi)容
msg['Subject'] = '郵件測(cè)試主題' # 郵件主題
msg['From'] = me # 發(fā)送者賬號(hào)
msg['To'] = maillist # 接收者賬號(hào)列表
smtp = smtplib.SMTP(email_host,port=25) # 連接郵箱,傳入郵箱地址,和端口號(hào),smtp的端口號(hào)是25
smtp.login(email_user, email_pwd) # 發(fā)送者的郵箱賬號(hào),密碼
smtp.sendmail(me, maillist, msg.as_string())
# 參數(shù)分別是發(fā)送者,接收者,第三個(gè)是把上面的發(fā)送郵件的內(nèi)容變成字符串
smtp.quit() # 發(fā)送完畢后退出smtp
print ('email send success.')
下面是發(fā)送帶附件的郵件
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart username='xxx@xx.com' email_host = 'smtp.163.com' passwd='123456' recv=['511402865@qq.com',] title='郵件標(biāo)題' content='發(fā)送郵件測(cè)試' msg = MIMEMultipart() file='a.txt' att = MIMEText(open(file,encoding='utf-8').read()) att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="%s"'%file msg.attach(att) msg.attach(MIMEText(content))#郵件正文的內(nèi)容 msg['Subject'] = title # 郵件主題 msg['From'] = username # 發(fā)送者賬號(hào) msg['To'] = recv # 接收者賬號(hào)列表 #smtp = smtplib.SMTP_SSL(eail_host,port=456)#qq郵箱 smtp = smtplib.SMTP_SSL(eail_host,port=25)#其他郵箱 smtp.login(username,passwd) smtp.sendmail(username,recv,msg.as_string()) smtp.quit()
當(dāng)然,我們可以封裝成一個(gè)函數(shù),使用的時(shí)候,直接調(diào)用函數(shù),傳入郵箱賬號(hào)密碼,收件人,發(fā)件人,標(biāo)題和內(nèi)容即可。
import smtplib
from email.mime.text import MIMEText
def send_mail(username,passwd,recv,title,content,mail_host='smtp.163.com',port=25):
'''
發(fā)送郵件函數(shù),默認(rèn)使用163smtp
:param username: 郵箱賬號(hào) xx@163.com
:param passwd: 郵箱密碼
:param recv: 郵箱接收人地址,多個(gè)賬號(hào)以逗號(hào)隔開
:param title: 郵件標(biāo)題
:param content: 郵件內(nèi)容
:param mail_host: 郵箱服務(wù)器
:param port: 端口號(hào)
:return:
'''
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),密碼
smtp.sendmail(username, recv, msg.as_string())
# 參數(shù)分別是發(fā)送者,接收者,第三個(gè)是把上面的發(fā)送郵件的內(nèi)容變成字符串
smtp.quit() # 發(fā)送完畢后退出smtp
print ('email send success.')
email_user = 'xxxx@163.com' # 發(fā)送者賬號(hào)
email_pwd = 'xxxxx' # 發(fā)送者密碼
maillist ='511402865@qq.com'
title = '測(cè)試郵件標(biāo)題'
content = '這里是郵件內(nèi)容'
send_mail(email_user,email_pwd,maillist,title,content)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 控制臺(tái)單行刷新,多行刷新實(shí)例
今天小編就為大家分享一篇python 控制臺(tái)單行刷新,多行刷新實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
django創(chuàng)建超級(jí)用戶時(shí)指定添加其它字段方式
這篇文章主要介紹了django創(chuàng)建超級(jí)用戶時(shí)指定添加其它字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
如何使用Python實(shí)現(xiàn)PPT批量轉(zhuǎn)圖片
這篇文章主要為大家詳細(xì)介紹了如何使用Python開發(fā)一個(gè)帶有圖形界面的PPT批量轉(zhuǎn)圖片工具,文中的示例代碼講解詳細(xì),有需要的小伙伴可以了解下2025-02-02
python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例
本篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法,涉及Python調(diào)用系統(tǒng)win32com組件實(shí)現(xiàn)文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-07-07
經(jīng)驗(yàn)豐富程序員才知道的15種高級(jí)Python小技巧(收藏)
本文將介紹15個(gè)簡(jiǎn)潔的Python技巧,向著簡(jiǎn)潔更高效,學(xué)習(xí)易懂出發(fā),具說只有經(jīng)驗(yàn)豐富程序員才知道的15種高級(jí)Python小技巧,喜歡的朋友快來看看吧2021-10-10

