3

I am working on a python script to send email to my customer with a survey. I will send only one email with all my customers's emails in the BCC field so that I do not need to loop through all the emails. Everything works fine when I tested sending emails to my company's coleagues and also when I sent to my personal email, but whenever I send to a gmail account, the BCC field appears to not be hidden and show all the emails. I found this post Email Bcc recipients not hidden using Python smtplib and tried that solution as well, but as I am using a html body email, the emails were shown inside the body. Can anyone help me on this one?

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()

I removed the following line from the code and tried again. Now the email is not sent to my customer's Gmail email.

msg['Bcc'] = 'customer@gmail.com'
Community
  • 1
  • 1
greenFedoraHat
  • 97
  • 2
  • 10

3 Answers3

2

Just do not mention bcc mails in msg['To'] or msg['Cc']. Do it only in 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()
sheesh
  • 51
  • 1
  • 6
1

Did you try not to define the msg['BCC'] field? Setting this field forces it to be included. It is sufficient that the BCC email address is in the sendmail command's destination address list. Take a look at this question.

jcoppens
  • 5,306
  • 6
  • 27
  • 47
  • Yes, I did that same solution on the link, but in my case I need the body to be html, so when I do that way, the email addresses as well the subject appear on the body of the email. – greenFedoraHat Mar 26 '17 at 02:39
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,
    }
seunggabi
  • 1,699
  • 12
  • 12