E-mail package and its Python

Send mail package to Python

Python send messages in four steps

  1. Connect to smtp server
  2. Log smtp server
  3. Mail preparation
  4. send email

Introducing the required package

import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart

A connection to the server smtp

Method 1: Do not use ssl encryption

smtp = smtplib.SMTP(host="smtp.163.com", port=465)

Second way: use ssl encryption

smtp = smtplib.SMTP_SSL(host="smtp.163.com", port=465)

* Note: When you pass host parameters, if QQ mailbox to change 'smtp.qq.com'

Second, the landing smtp server

smtp.login(user="发件人地址", password="授权码")

Third, prepare the mail

①: sending text messages

1, ready to content

f_user = "发件人地址"
t_user = "收件人地址"
content = "邮件的正文"
subject = "邮件的主题"

2, constructed using email messages

msg = MIMEText(content, _subtype='plain', _charset="utf8")
# 添加发件人
msg["From"] = f_user
# 添加收件人
msg["To"] = t_user
# 添加邮件主题
msg["subject"] = subject
②: Send e-mail with attachments

1, ready to content

f_user = "发件人地址"
t_user = "收件人地址"
content = "邮件的正文"
subject = "邮件的主题"
# 读取要发送附件的内容
file_content = open("附件文件名", "rb").read()

2, constructed using email messages

Mail (1) construct more than one component

msg = MIMEMultipart()

(2) to multi-component added text messages

text_msg = MIMEText(content, _subtype='plain', _charset="utf8")
msg.attach(text_msg)

(3) added to the multi-component email file attachments

file_msg = MIMEApplication(file_content)
file_msg.add_header('content-disposition', 'attachment', filename='发送附件的名称(可自定义)')
msg.attach(file_msg)

3. Add the sender, recipient, message subject

# 添加发件人
msg["From"] = f_user
# 添加收件人
msg["To"] = t_user
# 添加邮件主题
msg["subject"] = subject

Fourth, send e-mail

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

Write like this above to send mail, write a good, if a project with multiple locations need to send a message, it would be cumbersome, so, um, when you need to make the contents of a package above, for projects All places used to send messages can be called directly.

I. First, create a configuration file conf.ini

[email]
# smtp服务地址
host = smtp.163.com
# 端口
port = 465
# 发件人
user = 163邮箱
# 授权码
pwd = 授权码
# 收件人
to_user = 收件人邮箱
# 邮件正文
content = 正文
# 邮件主题
subject = 主题

Second, the transmission mail package

Packaging two methods:
  1. send_text: sending text messages

  2. send_file: Send e-mail file attachments
  3. The following code with [] are acquired from the configuration 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)

Guess you like

Origin www.cnblogs.com/desireyang/p/12050426.html