Python發送郵件


 

使用email模塊和smtplib模塊,內容比較固定,配好了即可實現,代碼如下:

一、普通郵件發送
import smtplib
from email.mime.text import MIMEText
email_host = 'smtp.163.com' #郵箱地址
email_user = 'XXX@163.com' # 發送者賬號
email_pwd = 'XXX' # 發送者的密碼
maillist ='XXX@XXXX.com'
#收件人郵箱,多個賬號的話,用逗號隔開
me = email_user
msg = MIMEText('這是個python測試郵件,不用回復。') # 郵件內容
msg['Subject'] = 'python測試' # 郵件主題
msg['From'] = me # 發送者賬號
msg['To'] = maillist # 接收者賬號列表
smtp = smtplib.SMTP(email_host,port=25) # 連接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25
smtp.login(email_user, email_pwd) # 發送者的郵箱賬號,密碼
smtp.sendmail(me, maillist, msg.as_string())
# 參數分別是發送者,接收者,第三個是把上面的發送郵件的內容變成字符串
smtp.quit() # 發送完畢后退出smtp
print ('email send success.')

注意
1. 發送者密碼這里不是平時登錄郵箱的密碼,而是開啟登錄第三方郵件客戶端的授權碼。
2. 多數郵箱的smtp的端口號都是25,個別的請具體確認。


發郵件的代碼封裝成函數:

import smtplib
from email.mime.text import MIMEText


def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25):
'''
發送郵件函數,默認使用163smtp
:param username: 郵箱賬號 xx@163.com
:param passwd: 郵箱密碼
:param recv: 郵箱接收人地址,多個賬號以逗號隔開
:param title: 郵件標題
:param content: 郵件內容
:param mail_host: 郵箱服務器
:param port: 端口號
:return:
'''
msg = MIMEText(content) # 郵件內容
msg['Subject'] = title # 郵件主題
msg['From'] = username # 發送者賬號
msg['To'] = recv # 接收者賬號列表
smtp = smtplib.SMTP(mail_host, port=port) # 連接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25
smtp.login(username, passwd) # 發送者的郵箱賬號,密碼
smtp.sendmail(username, recv, msg.as_string())
# 參數分別是發送者,接收者,第三個是把上面的發送郵件的內容變成字符串
smtp.quit() # 發送完畢后退出smtp
print('email send success.')


email_user = 'xxxx@163.com' # 發送者賬號
email_pwd = 'xxxxx' # 發送者密碼
maillist = 'XXX@XXX.com'
title = '測試郵件標題'
content = '這里是郵件內容'
send_mail(email_user, email_pwd, maillist, title, content)

二、發帶附件的郵件

import smtplib
#smtplib這個模塊是管發郵件
from email.mime.text import MIMEText
#構造郵件內容
from email.mime.multipart import MIMEMultipart
#發帶附件的郵件用的
email_host = 'smtp.163.com' #郵箱服務器地址
email_user = 'XXX@163.com' # 發送者賬號
email_pwd = 'XXX'
# 發送者密碼是郵箱的授權碼,不是登錄的密碼
maillist = 'XXX@XXX.com'
#收件人郵箱,多個賬號的話,用逗號隔開
new_msg = MIMEMultipart()
#構建了一個能發附件的郵件對象
new_msg.attach(MIMEText('這是Python測試發郵件的郵件,不要回復'))
# 郵件內容
new_msg['Subject'] = 'Python測試郵件帶附件' # 郵件主題
new_msg['From'] = email_user # 發送者賬號
new_msg['To'] = maillist # 接收者賬號列表
att = MIMEText(open('like_report.txt').read())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="haha.txt"'
new_msg.attach(att)
smtp = smtplib.SMTP(email_host,port=25) # 連接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25
smtp.login(email_user, email_pwd) # 發送者的郵箱賬號,密碼
smtp.sendmail(email_user, maillist, new_msg.as_string())
# 參數分別是發送者,接收者,第三個是把上面的發送郵件的內容變成字符串
smtp.quit() # 發送完畢后退出smtp
print ('email send success.')

三、封裝發送郵件的類並驗證

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()
#發送內容的對象
if self.file:#處理附件的
att = MIMEText(open(self.file).read())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"'%self.file
msg.attach(att)
msg.attach(MIMEText(self.content))#郵件正文的內容
msg['Subject'] = self.title # 郵件主題
msg['From'] = self.username # 發送者賬號
msg['To'] = self.recv # 接收者賬號列表
self.smtp = smtplib.SMTP(self.email_host,port=self.port)
#發送郵件服務器的對象
self.smtp.login(self.username,self.passwd)
try:
self.smtp.sendmail(self.username,self.recv,msg.as_string())
except Exception as e:
print('出錯了。。',e)
else:
print('發送成功!')
def __del__(self):
self.smtp.quit()

if __name__ == '__main__':
m = SendMail(
username='XXX@163.com',passwd='XXX',recv='XXX@XXX.com',
title='新鞋的發送郵件',content='哈哈哈啊哈哈哈哈',file='like_report.txt'
)
m.send_mail()



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM