Search code examples
pythonemailsmtpsmtplibbcc

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']))

Solution

  • 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!")