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

动手之前需要开启一下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()

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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值