Python带附件邮件发送的简易实现

Python带附件邮件发送的简易实现,第1张

动手之前需要开启一下SMTP服务,以qq邮箱为例,关于java版的可以查看https://blog.csdn.net/atwdy/article/details/119269369

下面是python代码:

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


class MailHelper:
    __mail_server = None
    __msg = None
    __sender_address = None
    __receiver_address = None

    def __init__(self, server_type):
        self.__mail_server = smtplib.SMTP(server_type)      # 初始化邮箱服务器
        self.__msg = MIMEMultipart()                        # 初始化邮件内容主体对象

    def mail_sender_register(self, sender_address, password):
        self.__sender_address = sender_address
        self.__mail_server.login(sender_address, password)  # 发送人邮箱 授权码

    def msg_sender_name(self, sender_name="默认发件人名称"):
        self.__msg["From"] = sender_name + "<" + self.__sender_address + ">"

    def mail_receiver_address(self, receiver_address):
        self.__receiver_address = receiver_address.split(",")

    def msg_title(self, title):
        self.__msg["Subject"] = title

    def msg_content(self, content):
        self.__msg.attach(MIMEText(content, "plain", "utf-8"))

    def msg_attach(self, file_paths):
        file_paths = file_paths.split(",")
        for file_path in file_paths:
            file_name = file_path.split("\")[-1]
            file = MIMEApplication(open(file_path, 'rb').read())
            file.add_header('Content-Disposition', 'attachment', filename=file_name)    # 设置附件信息
            self.__msg.attach(file)

    def send(self):
        self.__mail_server.sendmail(self.__sender_address, self.__receiver_address, self.__msg.as_string())
        self.__mail_server.quit()


if __name__ == '__main__':
    try:
        mail = MailHelper("smtp.qq.com")                    # 邮箱服务器类型
        mail.mail_sender_register("xxx@qq.com", "16位授权码")  # 发件人邮箱 授权码
        mail.mail_receiver_address("xxx@qq.com")      # 收件人账号,为多个时英文逗号隔开
        mail.msg_sender_name("大大")                        # 发件人姓名
        mail.msg_title("测试邮件标题")                      # 邮件标题
        mail.msg_content("测试邮件内容")                    # 邮件内容
        # 邮件附件,传入附件路径,路径多个时英文逗号隔离
        mail.msg_attach(r"D:\tmp\Hadoop命令 *** 作.java,D:\tmp\Hadoop知识点总结.txt,E:\仓库\图片\证件照.jpg")
        mail.send()
    except Exception as e:
        print(e)

上面是将代码进行了一个简单封装之后的结果,下面这种结构化的代码和上面等价,更容易快速理解:

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

send_from = "xxx@qq.com"
password = "16位授权码"
send_to = "xxx@qq.com"

mail_server = smtplib.SMTP("smtp.qq.com")      # 创建邮箱服务器
mail_server.login(send_from, password)
msg = MIMEMultipart()                          # 创建邮件体
msg["From"] = "发送人名称" + "<" + send_from + ">"
msg["Subject"] = "邮件标题"
msg.attach(MIMEText(r"邮件内容", "plain", "utf-8"))
attach_file = MIMEApplication(open("附件路径", 'rb').read())    # 创建附件
attach_file.add_header('Content-Disposition', 'attachment', filename="附件名")
msg.attach(attach_file)
mail_server.sendmail(send_from, send_to, msg.as_string())
mail_server.quit()

授权码在某些邮箱中可以替换成邮箱密码,这取决于邮箱类型。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/718548.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存