python 邮件抄送是什么意思_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: size=1412\r\n'

reply: b'250 Ok\r\n'

reply: retcode (250); Msg: b'Ok'

send: 'rcpt TO:\r\n'

reply: b'250 Ok\r\n'

reply: retcode (250); Msg: b'Ok'

send: 'rcpt TO:\r\n'

reply: b'250 Ok\r\n'

reply: retcode (250); Msg: b'Ok'

send: 'data\r\n'

reply: b'354 End data with .\r\n'

reply: retcode (354); Msg: b'End data with .'

data: (354, b'End data with .')

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 \r\nTo: xxxxx@126.com\r\nSubject: =?utf-8?b?5Yiw5pyf5o+Q6YaS?=\r\nBcc: xxxxx@smail.nju.edu.cn\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):

""" 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看到的邮件都是这样的:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值