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:<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(密送)的更多相关文章
- MimeMessageHelper代码发邮件时,通过客服端登陆到邮箱,在已发送邮件里没有已经通过代码发送的邮件
MimeMessageHelper代码发邮件时,通过客服端登陆到邮箱,在已发送邮件里没有已经通过代码发送的邮件, 这个问题很奇怪,这样的话不能看到通过代码发送的邮件历史记录,所以只好借助秘密抄送了,抄 ...
- python smtp 发邮件 添加附件
# -*- coding:utf-8 -*- # __author__ = 'justing' import os import smtplib from email.mime.multipart i ...
- python SMTP发邮件
# from email.mime.text import MIMEText from email.header import Header import smtplib # sender = 'zc ...
- python连接数据库自动发邮件
python连接数据库实现自动发邮件 1.运行环境 redhat6 + python3.6 + crontab + Oracle客户端 2.用到的模块 3.操作步骤 (1)安装python3.6参考 ...
- EDM营销算法:python自动批量发邮件
EDM营销:全称Email Direct Marketing,即电子邮件营销.企业可以通过使用EDM软件向目标客户发送EDM邮件,建立同目标顾客的沟通渠道,向其直接传达相关信息,用来促进销售.EDM软 ...
- Python自定义任务发邮件提醒
前言 在工作中,有时会有一些定期需要执行的任务或在将来某一天需要执行的任务,为避免疏漏,设计个小工具,发邮件提醒自己去处理. 方案简介 1.建立一个Excel文件,里面定义好待提醒的任务 2.建立一个 ...
- 解决升级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验证,修改 ...
- Qt5.5 使用smtp发邮件的各种坑
本人刚开始学习C++,用的是Qt5.5的IED,经过了两天的学习和查找资料,终于成功发了第一封邮件.以163邮箱为例,简单总结一下. 1.设置邮箱 这一步比较关键,要开通smtp服务,在开通的过程中会 ...
- php socket通过smtp发送邮件(纯文本、HTML,多收件人,多抄送,多密送)
<?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送 * @example * $mail = new MySendMail(); * ...
随机推荐
- vue证明题三,vue项目的包结构和配置
用vue-cli创建的项目带有自动配置好的包结构,包结构都是固定的. 关于详细的解释,网上多得是,只说下最重要的内容 1.vue项目包结构和端口号配置 这里笔者下了个HBuilderX来写代码. 2. ...
- go中浮点型用法总结
示例 // 浮点型的用法 package main import ( "fmt" "unsafe" ) func main() { // 如果浮点数声明时未指定 ...
- elasticsearch Mapping 定义索引
Mapping is the process of defining how a document should be mapped to the Search Engine, including i ...
- shell变量及相关命令
- 2019HDU多校训练第三场 Planting Trees 暴力 + 单调队列优化
题意:有一个n * n的网格,每个网格中间有一颗树,你知道每棵树的高,你可以选择一个矩形区域把里面的树都围起来,但是矩形区域里面任意两棵树的高度差的绝对值不超过m,问这个矩形的最大面积是多少? 思路: ...
- HWDB手写汉字识别 - CNN
MARK Caffe 的 googleNet近似模型,识别HWDB汉字200类 准确率96.3
- docker 运行jenkins及vue项目与springboot项目(一.安装docker)
docker 运行jenkins及vue项目与springboot项目: 一.安装docker 二.docker运行jenkins为自动打包运行做准备 三.jenkins的使用及自动打包vue项目 四 ...
- 【leetcode】923. 3Sum With Multiplicity
题目如下: Given an integer array A, and an integer target, return the number of tuples i, j, k such tha ...
- jdbcTemplate查询结果为对象list
RowMapper<WmsExpensesSettleEntity> rowMapper1=new BeanPropertyRowMapper<WmsExpensesSettleEn ...
- Python--模块之re
re模块 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C ...