发邮件

发邮件:https://www.cnblogs.com/hudieren/p/16792041.html

收邮件:https://www.cnblogs.com/hudieren/p/16792045.html

解析邮件:https://www.cnblogs.com/hudieren/p/16792096.html

删除邮件:https://www.cnblogs.com/hudieren/p/16798017.html

 

# -*- coding: utf-8 -*-
# @Author  : 107
# @File    : emaildriver.py
# @explain : 发邮件
import json
import re
import smtplib
import email
import time
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
import imaplib

import os


class SendEmail:
    def __init__(self, sender, sender_name, sender_token, attn, attn_name, subject, connect, files, server):
        """
        :param sender: 发件人邮箱账号
        :param sender_name: 发件人邮箱昵称
        :param sender_token: 发件人邮箱密码(当时申请smtp给的口令)
        :param attn: 收件人邮箱账号
        :param attn_name: 收件人邮箱昵称
        :param subject: 主题
        :param connect: 内容
        :param files: 附件,格式:{'path': '路径', 'read': 'r', 'encoding': 'utf-8', '_charset': 'utf-8', 'name': '文件名'}
        :param server: 邮件服务器地址
        """
        self.sender = sender
        self.sender_name = sender_name
        self.sender_token = sender_token
        self.attn = attn
        self.attn_name = attn_name
        self.subject = subject
        self.connect = connect
        self.files = files
        self.server = server

    def accessory(self, file):
        """
        处理单个附件
        :param file:
        :return:
        """
        path = file.get('path')
        rea = file.get('read')
        e = file.get('encoding')
        _charset = file.get('_charset')
        name = file.get('name')
        if not name:
            name = os.path.split(path)[-1]
        if e:
            with open(path, rea, encoding=e) as r:
                connect = r.read()
        else:
            with open(path, rea) as r:
                connect = r.read()
        acc = MIMEText(connect, _charset=_charset)
        acc["Content-Type"] = 'application/octet-stream'
        # 不能兼容outlook
        # acc1["Content-Disposition"] = f'attachment; filename="{name}"'
        # 可以兼容outlook
        acc.add_header("Content-Disposition", "attachment", filename=("gb18030", "", name))
        return acc

    def send_email(self):
        # connect = "这是我使用python smtplib及email模块发送的邮件"  # 正文
        msg = MIMEMultipart()
        # 括号里的对应发件人邮箱昵称、发件人邮箱账号
        msg['From'] = formataddr([self.sender_name, self.sender])
        # 括号里的对应收件人邮箱昵称、收件人邮箱账号
        msg['To'] = formataddr([self.attn_name, self.attn])
        # 邮件的主题,也可以说是标题
        msg['Subject'] = self.subject

        # 正文
        msg.attach(MIMEText(self.connect, _charset='utf-8'))
        # 附件
        for file in self.files:
            acc = self.accessory(file)
            msg.attach(acc)

        # 发件人邮箱中的SMTP服务器,端口是465
        # server = smtplib.SMTP_SSL("smtp.qq.com", 465)
        server = smtplib.SMTP_SSL(self.server, 465)

        # 括号中对应的是发件人邮箱账号、邮箱密码
        server.login(self.sender, self.sender_token)
        # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.sendmail(self.sender, [self.attn, ], msg.as_string())

        server.quit()  # 关闭连接

    def run(self):
        # self.send_email()
        try:
            self.send_email()
            return True
        except Exception as e:
            print(e.args)

 

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:发邮件 - Python技术站

(0)
上一篇 2023年4月2日
下一篇 2023年4月2日
合作推广
合作推广
分享本页
返回顶部