使用 Python3 脚本给多个人同时发送多个 excel 附件

Posted liubin0505star

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 Python3 脚本给多个人同时发送多个 excel 附件相关的知识,希望对你有一定的参考价值。

#-*- coding:utf-8 -*-
import smtplib
from email.utils import COMMASPACE, formatdate
from email.message import EmailMessage

def send_mail(send_from, send_to, subject, content, files=[], email_server="smtp.exmail.qq.com"):
    msg = EmailMessage()
    msg[\'From\'] = send_from
    msg[\'To\'] = COMMASPACE.join(send_to)
    msg[\'Date\'] = formatdate(localtime=True)
    msg[\'Subject\'] = subject
    msg.set_content(content)

    # 循环读取 excel 文件列表并添加到附件中
    for file in files:
        with open(file, \'rb\') as f:
            file_data = f.read()
        msg.add_attachment(file_data, maintype="application", subtype="xlsx", filename="{}".format(os.path.basename(file)))

    # 这里使用的腾讯企业邮箱做测试
    server = smtplib.SMTP_SSL(email_server, 465)
    server.login("liubin@0505star.com", "passwd")
    server.sendmail(send_from, send_to, msg.as_string())
    server.quit()

以上是关于使用 Python3 脚本给多个人同时发送多个 excel 附件的主要内容,如果未能解决你的问题,请参考以下文章