邮件未发送给 CC 中的人

2022-01-17 00:00:00 python smtp smtplib

问题描述

我有以下使用 python 发送邮件的脚本

I have the following script for sending mails using python

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os

FROMADDR = "myaddr@server.com"
PASSWORD = 'foo'

TOADDR   = ['toaddr1@server.com', 'toaddr2@server.com']
CCADDR   = ['ccaddr1@server.com', 'ccaddr2@server.com']

# Create message container - the correct MIME type is multipart/alternative.
msg            = MIMEMultipart('alternative')
msg['Subject'] = 'Test'
msg['From']    = FROMADDR
msg['To']      = ', '.join(TOADDR)
msg['Cc']      = ', '.join(CCADDR)

# 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()

当我使用脚本时,我看到邮件同时发送到 toaddr1toadd2但是 ccaddr1ccaddr2 根本没有收到邮件.

When I use the script, I see that the mail gets delivered to both toaddr1 and toadd2 However ccaddr1 and ccaddr2 does not receive the mail at all.

有趣的是,当我检查 toaddr1toadd2 收到的邮件时,它表明ccaddr1ccaddr2 存在于 CC 中.

Interestingly, when I check the mails received by toaddr1 and toadd2, it shows that ccaddr1 and ccaddr2 are present in CC.

脚本有错误吗?最初我认为这可能是我的邮件服务器的问题.我用 Gmail 试了一下,看到了同样的结果.也就是说,无论是我当前邮件服务器中的帐户,还是我在 CC 中的 Gmail 帐户,收件人都不会收到邮件,即使收件人"字段中的人正确接收邮件并且具有中提到的正确地址抄送字段

Is there any error in the script? Initially I thought that this might be an issue with my mail server. I tried it with Gmail and saw the same result. That is, no matter whether its an account in my current mail server or my Gmail account in the CC, the recipient will not receive the mail, even though the people in the 'To' field receive it properly and have the correct addresses mentioned in the CC field


解决方案

我觉得发邮件的时候需要把CCADDR和TOADDR放在一起:

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.

来自 docs:

注意 from_addr 和 to_addrs 参数用于构造传输代理使用的消息信封.

Note The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents.

相关文章