使用python smtplib在gmail上未隐藏密件抄送
我正在编写一个python脚本,向我的客户发送带有调查的电子邮件。我只会在密件抄送字段中发送一封包含所有客户电子邮件的电子邮件,这样我就不需要循环浏览所有电子邮件。当我测试发送电子邮件到公司的邮箱以及发送个人电子邮件时,一切正常,但每当我发送到gmail帐户时,密件抄送字段似乎不会隐藏并显示所有电子邮件。我找到了这篇文章,也尝试了这个解决方案,但由于我使用的是html正文电子邮件,电子邮件显示在正文中。有人能帮我吗使用python smtplib在gmail上未隐藏密件抄送,python,email,smtplib,bcc,Python,Email,Smtplib,Bcc,我正在编写一个python脚本,向我的客户发送带有调查的电子邮件。我只会在密件抄送字段中发送一封包含所有客户电子邮件的电子邮件,这样我就不需要循环浏览所有电子邮件。当我测试发送电子邮件到公司的邮箱以及发送个人电子邮件时,一切正常,但每当我发送到gmail帐户时,密件抄送字段似乎不会隐藏并显示所有电子邮件。我找到了这篇文章,也尝试了这个解决方案,但由于我使用的是html正文电子邮件,电子邮件显示在正文中。有人能帮我吗 import smtplib from email.mime.multipart
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()
您是否尝试不定义msg['BCC']字段?设置该字段将强制将其包括在内。密件抄送电子邮件地址在sendmail命令的目标地址列表中就足够了。看一看不要在msg['To']或msg['Cc']中提及密件抄送邮件。仅在服务器.sendmail()中执行此操作。
是的,我在链接上做了同样的解决方案,但在我的例子中,我需要主体是html,所以当我这样做时,电子邮件地址以及主题都会出现在电子邮件主体上。
msg['Bcc'] = 'customer@gmail.com'
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()