使用 python smtplib 发送邮件时,CC 被 postfix 忽略

CC is ignored by postfix when sending mail using python smtplib

我有以下代码用于发送带附件的电子邮件,Python 3.6。它工作正常,我 (somemail@other.com) 可以收到电子邮件,当我打开邮件时,我看到抄送收件人也在那里。但是抄送收件人还没有收到电子邮件。 当我查看 postfix 日志时,我发现邮件只发送给我,并没有尝试发送给 CC 收件人 (cc-somemail@other.com)

当我尝试从邮件服务器所在的本地托管的 roundcube 发送电子邮件时,使用相同的文本、主题、附件,我们都可以很好地接收邮件。

# Mail setup
smtp_server = 'smtp.example.com'
smtp_port = 587
smtp_username = 'admin@example.com'
smtp_password = '0643533546brE'
amount = 1234
import email, smtplib, ssl
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = "Bill"
body = f"Auto generated, bill amount is: {amount}"
sender_email = "admin@example.com"
receiver_email = "somemail@other.com"

# Multipart
msg = MIMEMultipart()
msg["From"] = smtp_username
msg["To"] = receiver_email
msg["Cc"] = "cc-somemail@other.com"
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain"))

# Attachment
filename = "Bill.pdf"

# Open PDF in binary
with open(filename, "rb") as attachment:
    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email
encoders.encode_base64(part)

# Add header as key/value pair to attachment part
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)

# Add attachment to message and convert message to string
msg.attach(part)
text = msg.as_string()

# Login to server using secure context and send email
context = ssl.create_default_context()

smtp = smtplib.SMTP(smtp_server, port=smtp_port)
smtp.ehlo()
smtp.starttls()
smtp.login(smtp_username, smtp_password)
smtp.sendmail(smtp_username, receiver_email, text)
smtp.quit()

下面是smtplib发送的邮件签名部分:

Content-Type: multipart/mixed; boundary="===============0768161488932452429=="
MIME-Version: 1.0
From: admin@example.com
To: somemail@other.com
Cc: cc-somemail@other.com
Subject: Bill
Message-Id: <4G0S5Q5bfVzZdL0@xxxxxxxx>
Date: Wed,
  9 Jun 2021 15:05:30 +0200 (CEST)

--===============0768161488932452429==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Auto generated, bill amount is: 1234
--===============0768161488932452429==
Content-Type: application/octet-stream
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename= Bill.pdf


--===============0768161488932452429==--

当我从 roundcube 尝试时,下面是签名:

MIME-Version: 1.0
Date: Wed, 09 Jun 2021 14:16:31 +0200
From: admin@example.com
To: somemail@other.com
Cc: cc-somemail@other.com
Subject: Bill
User-Agent: Roundcube Webmail
Message-ID: <742155857b037867452b2a5e31faa831@xxxxxx>
X-Sender: admin@example.com
Content-Type: multipart/mixed; boundary="=_be8adc6d944e66d68b7502ba2b42c9cd"

--=_be8adc6d944e66d68b7502ba2b42c9cd
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

Auto generated, bill amount is: 1234
--=_be8adc6d944e66d68b7502ba2b42c9cd
Content-Transfer-Encoding: base64
Content-Type: application/pdf; name=Bill.pdf
Content-Disposition: attachment; filename=Bill.pdf; size=111049


--=_be8adc6d944e66d68b7502ba2b42c9cd--

我不确定如何进一步解决此问题

Headers 可以,只需将抄送收件人作为 python 列表添加到当前收件人即可。例如:

subject = "Bill"
body = f"Auto generated, bill amount is: {amount}"
sender_email = "admin@example.com"
receiver_email = [ "somemail@other.com", "cc-somemail@other.com" ]

# Multipart
msg = MIMEMultipart()
msg["From"] = smtp_username
msg["To"] = "somemail@other.com"
msg["Cc"] = "cc-somemail@other.com"
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain"))

...

smtp.sendmail(smtp_username, receiver_email, text)