admin管理员组

文章数量:1559388

2021/9/15更新:添加图片附件+添加抄送

注意:msg[‘Cc’] = ‘,’.join(cc) 这里表示在邮件的抄送栏写明抄送给谁,但是不代表会被发送。所以必须把cc加入到receiver中一起发送才可以。

# sender: 发件邮箱, string
# receiver: 收件邮箱, string
# cc: 抄送, list
# subject: 主题
# content: 正文内容(html)
# imgs: 图片附件 list
# attaches: 其他附件 list
# my_pass: 发信密码
def send_mail(sender, receiver, cc, subject, content, imgs, attaches, my_pass):
    # ret是返回值, False表示发送失败
    ret = True
    try:
        # 发送html和附件
        msg = MIMEMultipart('related')
        msg['From'] = formataddr(["", sender])
        msg['To'] = formataddr(["", receiver])
        msg['Subject'] = subject
        msg['Cc'] = ','.join(cc)
        # 填写正文内容
        msgTxt = MIMEText(content, "html", "utf-8")
        msg.attach(msgTxt)
        # 添加img
        for img in imgs:
            att = addimg(img, '<image1>')
            att["Content-Type"] = 'application/octet-stream'
            # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
            att["Content-Disposition"] = 'attachment; filename=%s' % img
            msg.attach(att)
        # 添加其他附件
        for f in attaches:
            att = MIMEApplication(open(f, 'rb').read())
            att["Content-Type"] = 'application/octet-stream'
            # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
            att["Content-Disposition"] = 'attachment; filename=%s' % f
            msg.attach(att)
        # 登录邮件服务器
        server = smtplib.SMTP_SSL('smtp.exmail.qq', 465)
        server.login(sender, my_pass)
        server.sendmail(sender, [receiver,]+cc, msg.as_string())
        server.quit()

    except Exception as e:
        print("send_mail:failed!")
        print(e)
        ret = False
    return ret
    

2020/12/14更新:腾讯企业邮不需要取得发信密码

今天PM要求用公共邮箱发信,所以。。我发现不并不需要开启安全登录+获取发信密码。

代码不用改,my_pass直接用登录密码即可。

示例代码

import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr

def send_mail(sender, receiver, subject, attaches, my_pass):
    ret = True
    try:
        msg = MIMEMultipart()
        # 邮件显示的发信人名称
        msg['From'] = formataddr(["大数据XXX", sender])
        msg['To'] = formataddr(["生产", receiver])
        # 邮件显示的主题
        msg['Subject'] = subject
        server = smtplib.SMTP_SSL('smtp.exmail.qq', 465) # 腾讯企业邮
        # 这个是普通qq邮箱的服务器
        #server = smtplib.SMTP_SSL("smtp.qq", 465)
        for f in attaches:
            att = MIMEApplication(open(f, 'rb').read())
            att["Content-Type"] = 'application/octet-stream'
            # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
            att["Content-Disposition"] = 'attachment; filename=%s' % f
            msg.attach(att)
        server.login(sender, my_pass)
        server.sendmail(sender, [receiver, ], msg.as_string())
        server.quit()
    except Exception:
        ret = False
    return ret
    
sender = 'XXX@XX'
receiver = 'XXXX@qq'
my_pass = '**********'
attaches = [准备发送的文件名列表]
send_mail(sender, receiver, "订单更新结果", attaches, my_pass)

要点在于如何获取这个my_pass,就是使用邮箱发信的密码。这个密码并不是发件人登录邮箱服务器所用的密码(其实不太确切)。

对于普通qq邮箱

设置 - 账户 -

用这个授权码当my_pass。

腾讯企业邮

我是公司业务嘛,肯定不能用个人邮箱对吧。首先开启安全登录,之后可能会被退出账户吧,扫码进来之后,获取客户端专用密码即可。客户端专用密码就是my_pass。

本文标签: 腾讯示例发送邮件脚本邮箱