2017-03-25 51 views
0

我工作的Python腳本來發送電子郵件到我的客戶提供的一項調查顯示BCC不隱藏在Gmail中。我只會發送一封電子郵件,其中包含我所有客戶在BCC字段中的電子郵件,這樣我就不需要遍歷所有電子郵件。一切工作正常,當我測試發送電子郵件到我公司的coleagues還當我發到我的個人電子郵件,但每當我發送到Gmail帳戶時,BCC字段顯示不被隱藏和顯示所有的電子郵件。我發現這個職位Email Bcc recipients not hidden using Python smtplib和嘗試,解決方案,以及,但我使用HTML電子郵件的身體,被證明身體內的電子郵件。任何人都可以幫我解決這個問題嗎?使用Python的smtplib

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(['[email protected]', '[email protected]']) 
    msg['From'] = '[email protected]' 
    msg['Bcc'] = '[email protected]' 

    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('[email protected]', 
       ['[email protected]', '[email protected]', '[email protected]'], 
       msg.as_string()) 
    s.quit() 
except Exception as ex: 
    raise ex 

send_survey_mail() 

我從代碼中刪除了下面一行,然後再次嘗試。現在該電子郵件不會發送到我的客戶的Gmail電子郵件。

msg['Bcc'] = '[email protected]' 

回答

0

您是否嘗試不定義msg ['BCC']字段?設置該字段會強制將其包含在內。密件抄送電子郵件地址應該在sendmail命令的目標地址列表中。看看this post

+0

是的,我這樣做,鏈接相同的解決方案,但對我來說,我需要身體是HTML,所以當我這樣做的方式,電子郵件地址以及該主題出現在電子郵件的正文上。 – greenFedoraHat

0

只是不提BCC郵件味精[「要」]或味精[「抄送」]。做到這一點只在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 = "[email protected]" 
to_addr = ["[email protected]", "[email protected]"] 

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 + [[email protected]], text) 
server.quit()