Based on answer to question "How do I reply to an email using the Python imaplib and include the original message?" I did try to create function to answer with multi "Cc" sending.
def send_answer(original, file_to_attach, body_text="Macros reply", cc_addrs: list = []):
# https://stackoverflow.com/questions/2182196/how-do-i-reply-to-an-email-using-the-python-imaplib-and-include-the-original-mes
import smtplib
import ssl
# Then create a reply message:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
new = MIMEMultipart("mixed")
body = MIMEMultipart("alternative")
body.attach(MIMEText(body_text, "plain"))
body.attach(MIMEText(f"<html> {body_text} </html>", "html"))
new.attach(body)
new["Message-ID"] = email.utils.make_msgid()
new["In-Reply-To"] = original["Message-ID"]
new["References"] = original["Message-ID"]
new["Subject"] = "Re: " + original["Subject"]
new["To"] = original["From"].lower()
new["From"] = user
if cc_addrs:
new['Cc'] = ",".join(cc_addrs)
# Then attach the file: file_to_attach
file_name = file_to_attach.name
part = MIMEBase('application', 'octet-stream')
with open(file_to_attach, 'rb') as attachment:
part.set_payload(attachment.read())
email.encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=file_name)
new.attach(part)
context = ssl.create_default_context()
with smtplib.SMTP('smtp.gmail.com', 587) as s: # creates SMTP session
s.ehlo() # Can be omitted
s.starttls(context=context) # start TLS for security
s.ehlo() # Can be omitted
s.login(user, password) # Authentication # "Password_of_the_sender"
s.set_debuglevel(True)
s.send_message(new)
It works in strange manner:
- answer with attachment was received by sender (it's Ok)
- no-one from "Cc" list receive the answer
- gmail shows that copies was sent
here is the headers:
send: 'mail FROM:<vasilij.kolomiets@gmail.com> size=505259\r\n'
and end the debug message:
So, the question is - HOW to send copy to many receivers in list?