如何回复电子邮件并创建“抄送”地址列表?

问题描述

基于 answer 的问题“我如何使用 Python imaplib 回复电子邮件并包含原始消息?”我确实尝试创建函数来回答多个“抄送” "发送。

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)

它以奇怪的方式工作:

  1. 发件人收到了带附件的答复(没关系)
  2. “抄送”列表中没有人收到答复
  3. gmail 显示已发送副本

    gmail says

这里是标题

send: 'mail FROM:<vasilij.kolomiets@gmail.com> size=505259\r\n'

enter image description here

并结束调试消息:

enter image description here

那么,问题是 - 如何将副本发送给列表中的多个接收者?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)