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

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

send: 'ehlo E42.lan
'
reply: b'250-smtp.qq.com
'
reply: b'250-PIPELINING
'
reply: b'250-SIZE 73400320
'
reply: b'250-AUTH LOGIN PLAIN
'
reply: b'250-AUTH=LOGIN
'
reply: b'250-MAILCOMPRESS
'
reply: b'250 8BITMIME
'
reply: retcode (250); Msg: b'smtp.qq.com
PIPELINING
SIZE 73400320
AUTH LOGIN PLAIN
AUTH=LOGIN
MAILCOMPRESS
8BITMIME'
send: 'AUTH PLAIN xxxxxxxxxxxxxxxxxxxxxxxxx
'
reply: b'235 Authentication successful
'
reply: retcode (235); Msg: b'Authentication successful'
send: 'mail FROM:<support@xxxxx.com> size=1412
'
reply: b'250 Ok
'
reply: retcode (250); Msg: b'Ok'
send: 'rcpt TO:<xxxxx@126.com>
'
reply: b'250 Ok
'
reply: retcode (250); Msg: b'Ok'
send: 'rcpt TO:<xxxxx@smail.nju.edu.cn>
'
reply: b'250 Ok
'
reply: retcode (250); Msg: b'Ok'
send: 'data
'
reply: b'354 End data with <CR><LF>.<CR><LF>
'
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=="
MIME-Version: 1.0
Accept-Language: zh-CN
Accept-Charset: ISO-8859-1,utf-8
From: support <support@xxxxx.com>
To: xxxxx@126.com
Subject: =?utf-8?b?5Yiw5pyf5o+Q6YaS?=
Bcc: xxxxx@smail.nju.edu.cn

--===============6519358828643049548==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

PCFET0N 这里省略一长串正文 tbD4K

--===============6519358828643049548==--
.
'
reply: b'250 Ok: queued as 
'
reply: retcode (250); Msg: b'Ok: queued as'
data: (250, b'Ok: queued as')
send: 'quit
'
reply: b'221 Bye
'
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):
        """ config['bcc'] should be a list """
        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']
        if not isinstance(self.Bcc, list):
            raise Exception('passed in "bcc" should be a list')

    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 and '@' in self.Bcc[0]:
            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看到的邮件都是这样的:

原文地址:https://www.cnblogs.com/dylanchu/p/11141238.html