Python发送邮件以及对其封装
生活随笔
收集整理的這篇文章主要介紹了
Python发送邮件以及对其封装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python發送郵件分為四步
- 連接到smtp服務器
- 登陸smtp服務器
- 準備郵件
- 發送郵件
導入所需要的包
import smtplib from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart一、連接到smtp服務器
方式一:不使用ssl加密
smtp = smtplib.SMTP(host="smtp.163.com", port=25)方式二:使用ssl加密
smtp = smtplib.SMTP_SSL(host="smtp.163.com", port=465)注意:傳host參數時,如果是QQ郵箱就改成’smtp.qq.com’
二、登陸smtp服務器
mtp.login(user="發件人地址", password="授權碼")三、準備郵件
①:發送文本郵件
1、準備內容
f_user = "發件人地址" t_user = "收件人地址" content = "郵件的正文" subject = "郵件的主題"2、使用email構造郵件
''' Python學習交流,免費公開課,免費資料, 免費答疑,系統學習加QQ群:579817333 ''' msg = MIMEText(content, _subtype='plain', _charset="utf8") # 添加發件人 msg["From"] = f_user # 添加收件人 msg["To"] = t_user # 添加郵件主題 msg["subject"] = subject②:發送帶附件的郵件
1、準備內容
f_user = "發件人地址" t_user = "收件人地址" content = "郵件的正文" subject = "郵件的主題" # 讀取要發送附件的內容 file_content = open("附件文件名", "rb").read()2、使用email構造郵件
(1)構造一封多組件的郵件
msg = MIMEMultipart()(2)往多組件郵件中加入文本內容
text_msg = MIMEText(content, _subtype='plain', _charset="utf8") msg.attach(text_msg)(3)往多組件郵件中加入文件附件
file_msg = MIMEApplication(file_content) file_msg.add_header('content-disposition', 'attachment', filename='發送附件的名稱(可自定義)') msg.attach(file_msg)3、添加發件人、收件人、郵件主題
''' Python學習交流,免費公開課,免費資料, 免費答疑,系統學習加QQ群:579817333 ''' # 添加發件人 msg["From"] = f_user # 添加收件人 msg["To"] = t_user # 添加郵件主題 msg["subject"] = subject四、發送郵件
smtp.send_message(msg, from_addr=f_user, to_addrs=t_user)像這樣上面這樣寫發送郵件,寫一次還好,如果說一個項目中多個地方都需要用發送郵件,那就顯得笨重了,所以呢,這個時候就需要給上面內容做一個封裝,供項目中所有用到發送郵件的地方都可以直接調用.
1.首先,創建一個配置文件conf.ini
[email] # smtp服務地址 host = smtp.163.com # 端口 port = 465 # 發件人 user = 163郵箱 # 授權碼 pwd = 授權碼 # 收件人 to_user = 收件人郵箱 # 郵件正文 content = 正文 # 郵件主題 subject = 主題2.對發送郵件進行封裝
封裝了兩個方法:
-
send_text:發送文本郵件
-
send_file:發送文件附件郵件
以下代碼帶[]的都是要從配置文件中獲取的
class SendEMail(object):"""封裝發送郵件類"""def __init__(self):# 第一步:連接到smtp服務器self.smtp_s = smtplib.SMTP_SSL(host=[host],port=[port])# 第二步:登陸smtp服務器self.smtp_s.login(user=[user],password=[pwd])def send_text(self, to_user, content, subject):"""發送文本郵件:param to_user: 對方郵箱:param content: 郵件正文:param subject: 郵件主題:return:"""# 第三步:準備郵件# 使用email構造郵件msg = MIMEText(content, _subtype='plain', _charset="utf8")# 添加發件人msg["From"] = [user]# 添加收件人msg["To"] = to_user# 添加郵件主題msg["subject"] = subject# 第四步:發送郵件self.smtp_s.send_message(msg, from_addr=[user], to_addrs=to_user)def send_file(self, to_user, content, subject, reports_path, file_name):"""發送測試報告郵件:param to_user: 對方郵箱:param content: 郵件正文:param subject: 郵件主題:param reports_path: 測試報告路徑:param file_name: 發送時測試報告名稱"""# 讀取報告文件中的內容file_content = open(reports_path, "rb").read()# 2.使用email構造郵件# (1)構造一封多組件的郵件msg = MIMEMultipart()# (2)往多組件郵件中加入文本內容text_msg = MIMEText(content, _subtype='plain', _charset="utf8")msg.attach(text_msg)# (3)往多組件郵件中加入文件附件file_msg = MIMEApplication(file_content)file_msg.add_header('content-disposition', 'attachment', filename=file_name)msg.attach(file_msg)# 添加發件人msg["From"] = [user]# 添加收件人msg["To"] = to_user# 添加郵件主題msg["subject"] = subject# 第四步:發送郵件self.smtp_s.send_message(msg, from_addr=[user], to_addrs=to_user)總結
以上是生活随笔為你收集整理的Python发送邮件以及对其封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python基础教程:list中的app
- 下一篇: Python字符串截取值