python: how to send mail with TO, CC and BCC?
Email headers don't matter to the smtp server. Just add CC and BCC recipients to toaddrs
when sending emails. For CC, add them to the CC header.
toaddr = 'buffy@sunnydale.k12.ca.us'
cc = ['alexander@sunydale.k12.ca.us','willow@sunnydale.k12.ca.us']
bcc = ['chairman@slayerscouncil.uk']
fromaddr = 'giles@sunnydale.k12.ca.us'
message_subject = "disturbance in sector 7"
message_text = "Three are dead in an attack in the sewers below sector 7."
message = "From: %s\r\n" % fromaddr
+ "To: %s\r\n" % toaddr
+ "CC: %s\r\n" % ",".join(cc)
+ "Subject: %s\r\n" % message_subject
+ "\r\n"
+ message_text
toaddrs = [toaddr] + cc + bcc
server = smtplib.SMTP('smtp.sunnydale.k12.ca.us')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, message)
server.quit()
Python Gmail BCC and CC
you can try this
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
toaddr = ['foo@bar.us','jhon@doe.it']
cc = ['aaa@bb.com','cc@dd.com']
bcc = ['hello@world.uk']
subject = 'Email from Python Code'
fromaddr = 'your@email.com'
message = "\n !! Hello... !!"
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Cc'] = ', '.join(cc)
msg['Bcc'] = ', '.join(bcc)
# Create the body of the message (an HTML version).
text = """Hi this is the body
"""
# Record the MIME types of both parts - text/plain and text/html.
body = MIMEText(text, 'plain')
# Attach parts into message container.
msg.attach(body)
# Send the message via local SMTP server.
s = smtplib.SMTP('server.com', 587)
s.set_debuglevel(1)
s.ehlo()
s.starttls()
s.login(fromaddr, PASSWORD)
s.sendmail(fromaddr, toaddr, msg.as_string())
s.quit()
Python Emails Not Sending to BCC
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!")
Mails not being sent to people in CC
I think that you will need to put the CCADDR with the TOADDR when sending the mail:
s.sendmail(FROMADDR, TOADDR+CCADDR, msg.as_string())
You're correctly adding the addresses to your message, but you will need the cc addresses on the envelope too.
From the docs:
Note The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents.
Using Python to send to multiple BCC recipients in Outlook
olBCC
is 3.
Secondly, Recipients
collection is 1 based, not 0.
Thirdly, Recipients.Add
returns a Recipient
object. Your code ignores the result of the call:
for recipient in emails:
mail.Recipients.Add(recipient).Type = 3
Related Topics
Convert Timedelta64[Ns] Column to Seconds in Python Pandas Dataframe
Extension Method for Python Built-In Types
Builtin Function Not Working with Spyder
Why Do Attribute References Act Like This with Python Inheritance
Efficient Way to Add Spaces Between Characters in a String
How to Make an Image with a Transparent Backround in Pygame
Using Cprofile Results with Kcachegrind
How to Redirect the Output of Print to a Txt File
String Formatting: Columns in Line
Typeerror: List Indices Must Be Integers or Slices, Not Str
Subsampling Every Nth Entry in a Numpy Array
Find and Replace Values in Xml Using Python
Numpy Np.Apply_Along_Axis Function Speed Up
Getting the Index of a Row in a Pandas Apply Function
How to Edit a Seaborn Legend Title and Labels for Figure-Level Functions