Python發送郵件以及對其封裝


對Python發送郵件進行封裝

Python發送郵件分為四步

  1. 連接到smtp服務器
  2. 登陸smtp服務器
  3. 准備郵件
  4. 發送郵件

導入所需要的包

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服務器

smtp.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、添加發件人、收件人、郵件主題

# 添加發件人
msg["From"] = f_user
# 添加收件人
msg["To"] = t_user
# 添加郵件主題
msg["subject"] = subject

四、發送郵件

smtp.send_message(msg, from_addr=f_user, to_addrs=t_user)

像這樣上面這樣寫發送郵件,寫一次還好,如果說一個項目中多個地方都需要用發送郵件,那就顯得笨重了,所以呢,這個時候就需要給上面內容做一個封裝,供項目中所有用到發送郵件的地方都可以直接調用.

一、首先,創建一個配置文件conf.ini

[email]
# smtp服務地址
host = smtp.163.com
# 端口
port = 465
# 發件人
user = 163郵箱
# 授權碼
pwd = 授權碼
# 收件人
to_user = 收件人郵箱
# 郵件正文
content = 正文
# 郵件主題
subject = 主題

二、對發送郵件進行封裝

封裝了兩個方法:
  1. send_text:發送文本郵件

  2. send_file:發送文件附件郵件

  3. 以下代碼帶[]的都是要從配置文件中獲取的

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)


免責聲明!

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



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