python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送)

SMTP发送邮件的时候,并没有特殊的通信语句告诉邮件服务器 谁是主送,谁是抄送/密送,这三个角色都是以同样的方式告诉邮件服务器的,然后重点在邮件内容里。
邮件内容分为头和体两部分(就像http),头部包含了各种meta信息,其中说明了谁要用to,谁要cc,谁要bcc.
一个典型的带to和bcc的邮件发送过程debug日志如下:

send: 'ehlo E42.lan\r\n'
reply: b'250-smtp.qq.com\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-SIZE 73400320\r\n'
reply: b'250-AUTH LOGIN PLAIN\r\n'
reply: b'250-AUTH=LOGIN\r\n'
reply: b'250-MAILCOMPRESS\r\n'
reply: b'250 8BITMIME\r\n'
reply: retcode (250); Msg: b'smtp.qq.com\nPIPELINING\nSIZE 73400320\nAUTH LOGIN PLAIN\nAUTH=LOGIN\nMAILCOMPRESS\n8BITMIME'
send: 'AUTH PLAIN xxxxxxxxxxxxxxxxxxxxxxxxx\r\n'
reply: b'235 Authentication successful\r\n'
reply: retcode (235); Msg: b'Authentication successful'
send: 'mail FROM:<[email protected]> size=1412\r\n'
reply: b'250 Ok\r\n'
reply: retcode (250); Msg: b'Ok'
send: 'rcpt TO:<[email protected]>\r\n'
reply: b'250 Ok\r\n'
reply: retcode (250); Msg: b'Ok'
send: 'rcpt TO:<[email protected]>\r\n'
reply: b'250 Ok\r\n'
reply: retcode (250); Msg: b'Ok'
send: 'data\r\n'
reply: b'354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'Content-Type: multipart/alternative; boundary="===============6519358828643049548=="\r\nMIME-Version: 1.0\r\nAccept-Language: zh-CN\r\nAccept-Charset: ISO-8859-1,utf-8\r\nFrom: support <[email protected]>\r\nTo: [email protected]\r\nSubject: =?utf-8?b?5Yiw5pyf5o+Q6YaS?=\r\nBcc: [email protected]\r\n\r\n--===============6519358828643049548==\r\nContent-Type: text/html; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\n\r\nPCFET0N 这里省略一长串正文 tbD4K\r\n\r\n--===============6519358828643049548==--\r\n.\r\n'
reply: b'250 Ok: queued as \r\n'
reply: retcode (250); Msg: b'Ok: queued as'
data: (250, b'Ok: queued as')
send: 'quit\r\n'
reply: b'221 Bye\r\n'
reply: retcode (221); Msg: b'Bye'

下面是python代码:

#!/usr/bin/env python3
# coding: utf-8
#
# Created by dylanchu on 2019/7/5

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr


class MyMailer(object):
    def __init__(self, config: dict):
        self.Host = config['host']
        self.Port = config['port']
        self.Email = config['email']
        self.Password = config['password']
        self.From = config['from']
        self.using_ssl = config['using_ssl']
        self.Bcc = config['bcc']

    def send_mail(self, recipient, subject, content):
        if recipient == '' or subject == '' or content == '':
            raise Exception('recipient/subject/content should not be empty!!')

        # Create message container - the correct MIME type is multipart/alternative.
        msg = MIMEMultipart('alternative')
        msg["Accept-Language"] = "zh-CN"
        msg["Accept-Charset"] = "ISO-8859-1,utf-8"
        msg['From'] = formataddr([self.From, self.Email])
        msg['To'] = recipient
        msg['Subject'] = subject

        # msg format should be 'plain' or 'html'
        body = MIMEText(content, 'html', 'utf-8')
        msg.attach(body)
        if self.Bcc:  # 重点在这里:加上相应字段,并添加到收件人即可
            msg['Bcc'] = ','.join(self.Bcc)
            recipient = [recipient] + self.Bcc
        try:
            if self.using_ssl:
                smtp = smtplib.SMTP_SSL(self.Host, self.Port, timeout=30)
            else:
                smtp = smtplib.SMTP(self.Host, self.Port, timeout=30)
            smtp.set_debuglevel(1)
            smtp.login(self.Email, self.Password)
            smtp.sendmail(self.Email, recipient, msg.as_string())
            smtp.quit()
            print("email sent successfully")
        except Exception as e:
            print("email sent failed with error: %s" % e)

作为to和bcc看到的邮件都是这样的:

猜你喜欢

转载自www.cnblogs.com/dylanchu/p/11141238.html