python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送)
2020-12-13 05:28
标签:图片 pie etc %s iso zh-cn word inf attach SMTP发送邮件的时候,并没有特殊的通信语句告诉邮件服务器 谁是主送,谁是抄送/密送,这三个角色都是以同样的方式告诉邮件服务器的,然后重点在邮件内容里。 下面是python代码: 作为to和bcc看到的邮件都是这样的: python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送) 标签:图片 pie etc %s iso zh-cn word inf attach 原文地址:https://www.cnblogs.com/dylanchu/p/11141238.html
邮件内容分为头和体两部分(就像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:
#!/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)
文章标题:python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送)
文章链接:http://soscw.com/essay/31089.html