Python傳送郵件以及對其封裝
阿新 • • 發佈:2021-06-24
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構造郵件
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學習交流,免費公開課,免費資料,
免費答疑,系統學習加群: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)