gpt4 book ai didi

Python sendmail - 将 header 添加到 MimeMultipart 以避免隐藏所有收件人

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:00 25 4
gpt4 key购买 nike

我有一个代码可以发送一封使用 HTML 进行模板化的电子邮件。所有收件人都会收到邮件,但都是密件抄送。

def sendMail(to, cc, bcc, template, bodyParams, subjectParams):
global connection
if not testCurrentConnection(connection):
connect(currentConnectionName)
msg = MIMEMultipart('alternative')
subject, fromEmail, body = templateController.readTemplate(template)
header = 'To: ' + to + '\n' + 'Cc: ' + cc + 'From: ' + fromEmail + '\n' + 'Subject: ' + subject + '\n'
msg.attach(MIMEText(header, 'text'))
if subjectParams:
msg['Subject'] = templateController.replaceTextWithParams(subject, subjectParams)
else:
msg['Subject'] = subject
if bodyParams:
msg['Body'] = templateController.replaceTextWithParams(body, bodyParams)
else:
msg['Body'] = body

msg['From'] = fromEmail
msg['To'] = to
msg['Cc'] = cc
# no need to specify bcc here

msg.attach(MIMEText(msg['Body'], 'html'))
connection.sendmail(msg['From'], [msg['To'], msg['Cc'], bcc], msg.as_string())
del msg

我这样调用该函数:

smtpController.sendMail("myMail@gmail.com", "ccMail@gmail.com", "", "email.html", None, None)

(最后两个变量实际上是一个带有键值映射的字典,用于填充 HTML,但如果没有它们,问题也会重现)

我读到我需要在消息中添加 header 以防止出现这种情况,但由于某种原因,添加 header 不会改变任何内容(上述代码中的第 7-8 行)。
我错过了什么?

最佳答案

好吧,我不知道它有何不同,但我通过移动 msg['To'] = tomsg['Cc'] = cc< 来修复它 向上,位于 SubjectBody 之前。我完全删除了标题。

def sendMail(to, cc, bcc, template, bodyParams, subjectParams):
global connection
if not testCurrentConnection(connection):
connect(currentConnectionName)
subject, fromEmail, body = templateController.readTemplate(template)
msg = MIMEMultipart('alternative')
msg['From'] = fromEmail
msg['To'] = to
msg['Cc'] = cc
if subjectParams:
msg['Subject'] = templateController.replaceTextWithParams(subject, subjectParams)
else:
msg['Subject'] = subject
if bodyParams:
msg['Body'] = templateController.replaceTextWithParams(body, bodyParams)
else:
msg['Body'] = body

msg.attach(MIMEText(msg['Body'], 'html'))
connection.sendmail(msg['From'], to.split(',') + cc.split(',') + bcc.split(','), msg.as_string())
del msg

关于Python sendmail - 将 header 添加到 MimeMultipart 以避免隐藏所有收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53652830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com