简体   繁体   中英

Python Emails Not Sending to BCC

I am trying to make some python code that will send emails. I have been successful so far, but the BCC's are not being sent the message.

"""
June 18, 2020

@author: Carlos
"""
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail = smtplib.SMTP("smtp.gmail.com", 587)
msg = MIMEMultipart()

message = input("Your message: ")
password = input("Your password: ")
msg['From'] = input("Your email: ")
msg['To'] = input("Reciever(s): ")
msg['CC'] = ", " + input("CC(s): ")
msg['BCC'] = ", " + input("BCC(s): ")
msg['Subject'] = input("Your header: ")

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

mail.ehlo()

mail.starttls()

mail.login(msg['From'], password)

mail.sendmail(msg['From'], msg['To'] + msg['CC'] + msg['BCC'], msg.as_string())

mail.close()

print("Successfully sent email to %s:" % (msg['To']))

You probably do not want the BCC to be part of the message itself (because by definition it is not meant to be seen by the recipients), so it is probably best stored in a separate variable rather than as an element of the MIMEMultipart instance.

You also do not want to put the leading commas etc (which you have added merely to help join the strings) into the elements of msg as they will form part of the message.

If you do this:

msg['To'] = input("Receiver(s): ")
msg['CC'] = input("CC(s): ")
bcc = input("BCC(s): ")

then you can construct a list of all the recipients using the following:

all_recipients = sum([s.split(",") for s in (msg['To'], msg['CC'], bcc) if s],
                     [])

You can then send the message using:

mail.sendmail(msg['From'], ",".join(all_recipients), msg.as_string())

The above code assumes that your input strings do not include comma except as a separator. In principle a header such as "To:" could contain real name parts, and these could contain commas, eg:

"John Doe, Yoyodyne, Inc." <john.doe@yoyodyne.example.com>

If this might be used, then an alternative approach would be to make a list of whichever To/CC/BCC headers are not empty, and join those on "," , so that you do not have to split any strings:

non_empty_recipient_headers = [h for h in (msg['To'], msg['CC'], bcc) if h]

mail.sendmail(msg['From'], ",".join(non_empty_recipient_headers), msg.as_string())

I found a solution to my issue with it not sending! Here's my code if anyone is interested!

"""
June 20, 2020

@author: Carlos
"""
import smtplib
mail = smtplib.SMTP("smtp.gmail.com", 587)

sender = input("Your email: ")
password = input("Your password: ")
reciever = input("Receiver(s): ")
cc = [input("CC(s): ")]
bcc = [input("BCC(s): ")]
subject = input("Your header: ")
message_text = input("Your message: ")
message = "From: %s\r\n" % sender + "To: %s\r\n" % reciever + "CC: %s\r\n" % ",".join(cc) + "Subject: %s\r\n" % subject + "\r\n"  + message_text
to = [reciever] + cc + bcc

mail.ehlo()

mail.starttls()

mail.login(sender, password)

mail.sendmail(sender, to, message)

mail.close()

print("Successfully sent email!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM