【发布时间】:2017-08-18 10:53:57
【问题描述】:
我正在编写一个 python 脚本,通过调查向我的客户发送电子邮件。我将在密件抄送字段中只发送一封包含所有客户电子邮件的电子邮件,这样我就不需要遍历所有电子邮件。当我测试向我公司的同事发送电子邮件以及发送到我的个人电子邮件时,一切正常,但是每当我发送到 gmail 帐户时,BCC 字段似乎没有被隐藏并显示所有电子邮件。我找到了这篇文章Email Bcc recipients not hidden using Python smtplib 并尝试了该解决方案,但是由于我使用的是 html 正文电子邮件,因此电子邮件显示在正文中。谁能帮我解决这个问题?
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def send_survey_mail():
template_path = 'template.html'
background_path = 'image.png'
button_path = 'image2.png'
try:
body = open(template_path, 'r')
msg = MIMEMultipart()
msg['Subject'] = 'Customer Survey'
msg['To'] = ', '.join(['myemail@domain.com.br', 'myemail2@domain.com'])
msg['From'] = 'mycompany@mycompany.com.br'
msg['Bcc'] = 'customer@domain.com'
text = MIMEText(body.read(), 'html')
msg.attach(text)
fp = open(background_path, 'rb')
img = MIMEImage(fp.read())
fp.close()
fp2 = open(button_path, 'rb')
img2 = MIMEImage(fp2.read())
fp2.close()
img.add_header('Content-ID', '<image1>')
msg.attach(img)
img2.add_header('Content-ID', '<image2>')
msg.attach(img2)
s = smtplib.SMTP('smtpserver')
s.sendmail('mycompany@mycompany.com.br',
['myemail@domain.com.br', 'myemail2@domain.com', 'customer@domain.com'],
msg.as_string())
s.quit()
except Exception as ex:
raise ex
send_survey_mail()
我从代码中删除了以下行并再次尝试。现在电子邮件没有发送到我客户的 Gmail 电子邮件。
msg['Bcc'] = 'customer@gmail.com'
【问题讨论】: