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:<support@xxxxx.com> size=1412\r\n'
reply: b'250 Ok\r\n'
reply: retcode (250); Msg: b'Ok'
send: 'rcpt TO:<xxxxx@126.com>\r\n'
reply: b'250 Ok\r\n'
reply: retcode (250); Msg: b'Ok'
send: 'rcpt TO:<xxxxx@smail.nju.edu.cn>\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 <support@xxxxx.com>\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看到的邮件都是这样的:

python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送)的更多相关文章

  1. MimeMessageHelper代码发邮件时,通过客服端登陆到邮箱,在已发送邮件里没有已经通过代码发送的邮件

    MimeMessageHelper代码发邮件时,通过客服端登陆到邮箱,在已发送邮件里没有已经通过代码发送的邮件, 这个问题很奇怪,这样的话不能看到通过代码发送的邮件历史记录,所以只好借助秘密抄送了,抄 ...

  2. python smtp 发邮件 添加附件

    # -*- coding:utf-8 -*- # __author__ = 'justing' import os import smtplib from email.mime.multipart i ...

  3. python SMTP发邮件

    # from email.mime.text import MIMEText from email.header import Header import smtplib # sender = 'zc ...

  4. python连接数据库自动发邮件

    python连接数据库实现自动发邮件 1.运行环境 redhat6 + python3.6 + crontab + Oracle客户端 2.用到的模块  3.操作步骤 (1)安装python3.6参考 ...

  5. EDM营销算法:python自动批量发邮件

    EDM营销:全称Email Direct Marketing,即电子邮件营销.企业可以通过使用EDM软件向目标客户发送EDM邮件,建立同目标顾客的沟通渠道,向其直接传达相关信息,用来促进销售.EDM软 ...

  6. Python自定义任务发邮件提醒

    前言 在工作中,有时会有一些定期需要执行的任务或在将来某一天需要执行的任务,为避免疏漏,设计个小工具,发邮件提醒自己去处理. 方案简介 1.建立一个Excel文件,里面定义好待提醒的任务 2.建立一个 ...

  7. 解决升级PHP7.1后,发邮件时提示“fsockopen(): Peer certificate CN=`xxx.xx.com' did not match expected CN=`113.x.xx.98”

    把项目环境升级到PHP7.1后,发现在不使用SSL时可以使用IP发邮件,可设置成SSL时就只能使用hostname发送,PHP提示的错误信息大致意思是说,IP与hostname无法通过SSL验证,修改 ...

  8. Qt5.5 使用smtp发邮件的各种坑

    本人刚开始学习C++,用的是Qt5.5的IED,经过了两天的学习和查找资料,终于成功发了第一封邮件.以163邮箱为例,简单总结一下. 1.设置邮箱 这一步比较关键,要开通smtp服务,在开通的过程中会 ...

  9. php socket通过smtp发送邮件(纯文本、HTML,多收件人,多抄送,多密送)

    <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送 * @example * $mail = new MySendMail(); * ...

随机推荐

  1. NSQ消息队列

    前面的总结中提到过这个玩意,所以简单说说,win上面的测试验证 网上有比较合适的博文,我先推荐几篇 https://blog.csdn.net/a2247889821/article/details/ ...

  2. ps学习记录

    基本快捷键: ctrl + 放大 ctrl - 缩小 ctrl 空格键 放大工具 ctrl 0 适合屏幕大小 ctrl 1 显示实际大小 ctrl n 新建画布 ctrl v 移动工具 按住alt键 ...

  3. System.Net.Mail.SmtpException:不允许使用邮箱名称.

    使用SmtpClient发送邮件的时候,出现了如题错误. 解决方案: 将  SmtpClient.UseDefaultCredentials  属性设置为 true . 官方文档说明: Some SM ...

  4. element el-table 合计在横拉滚动条的下面,正确展示应该是滚动条在合计下面

    <style lang="less"> .el-table{ overflow: auto; } .el-table .el-table__body-wrapper, ...

  5. mysql-alter语句常用操作

    删除列 alter table table-name drop col-name 增加列(多列) alter table table-name add col-name col-type commen ...

  6. 学习selenium grid记录

    1.找两台Windows系统,一个是A,作为Hub:一个是B,作为Node: 2.在A.B两台电脑分别下载selenium-server-standalone-2.48.0.jar,并放到指定目录 3 ...

  7. ATM&购物商城程序

    模拟实现一个ATM + 购物商城程序 额度15000或自定义 实现购物商城,买东西加入购物车,调用信用卡接口转账 可以体现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 提供还款接 ...

  8. 西电源ubuntu12

    deb http://linux.xidian.edu.cn/mirrors/ubuntu/ precise main restricted universe multiverse #deb-src ...

  9. 【leetcode】576. Out of Boundary Paths

    题目如下: There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can mov ...

  10. 【和孩子一起学编程】 python笔记--第二天

    第六章 GUI:用户图形界面(graphical user interface) 安装easygui:打开cmd命令窗口,输入:pip install easygui 利用msgbox()函数创建一个 ...