使用Python的smtplib发送邮件时,Gmail上的BCC列表不会被隐藏。

3
我正在编写一个Python脚本,用于向我的客户发送调查问卷邮件。我将在BCC字段中发送一封电子邮件,其中包含所有客户的电子邮件地址,这样我就不需要遍历所有电子邮件地址。当我测试向公司同事和个人电子邮件发送邮件时,一切正常,但是每当我发送到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'
3个回答

2

在msg['To']或msg['Cc']中不要提及密送邮件。只需在server.sendmail()中执行。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

from_addr = "your@mail.com"
to_addr = ["to@mail.com", "to2@mail.com"]

msg = MIMEMultipart()

msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = "SUBJECT"

body = "BODY"

msg.attach(MIMEText(body, 'plain'))

filename = "FILE.pdf"
attachment = open('/home/FILE.pdf', "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('.....', 587)
server.starttls()
server.login(from_addr, 'yourpass')
text = msg.as_string()
server.sendmail(from_addr, to_addr + [bcc@mail.com], text)
server.quit()

1
你尝试过不定义msg['BCC']字段吗?设置此字段会强制包含它。只需将密送电子邮件地址放在sendmail命令的目标地址列表中即可。请参阅this question

是的,我在链接上使用了相同的解决方案,但在我的情况下,我需要正文是HTML格式的,因此当我这样做时,电子邮件地址以及主题也会出现在电子邮件的正文中。 - greenFedoraHat

0
MAIL_FROM = default@server.com
MAIL_DL = default@server.com

def send(to, cc, bcc, subject, text, html):
    message = MIMEMultipart("alternative")
    message["Subject"] = subject
    message["From"] = MAIL_FROM
    message["To"] = to
    message["Cc"] = cc + "," + MAIL_DL

    if html is not None:
        body = MIMEText(html, "html")
    else:
        body = MIMEText(text)
    message.attach(body)

    server = smtplib.SMTP(MAIL_SERVER)
    server.set_debuglevel(1)

    server.sendmail(MAIL_DL, to.split(",") + bcc.split(","), message.as_string())
    server.quit()

    return {
        "to": to,
        "cc": cc,
        "bcc": bcc,
        "subject": subject,
        "text": text,
        "html": html,
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接