电子邮件不会被触发到 CC 收件人
Emails are not getting trigerred to CC recipients
我开发了一个 python 代码来向选定的用户发送邮件。邮件正在递送至 "TO" 个收件人,但未递送至 "CC" 个收件人。
可以有 100 多个 CC 收件人,这些信息将被硬编码。
请帮我找出下面代码中的错误
conn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=Birthday_Database.accdb;')
cur = conn.cursor()
sql = ("SELECT Name,DOB,Image,Email FROM Calendar where DOB = {}".format(t2))
cur.execute(sql)
df = cur.fetchall()
if len(df) == 0:
print("There are no Birthday's for today!!!!")
sys.exit(0)
for row in df:
myVar1 = row.Name
myVar2 = row.Image
myVar3 = row.Email
# Define these once; use them twice!
strFrom = 'do.not.reply@abc.com'
strTo = myVar3
Image = myVar2
Names = myVar1
strcc = ['qwerty@abc.com','ytrewq@abc.com','poiuyt@abc.com']
strcc = ','.join(strcc)
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Happy Birthday {0}'.format(Names)
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot['Cc'] = strcc
#msgRoot['Cc'] = strcc
msgRoot.preamble = 'This is a multi-part message in MIME format.'
print(msgRoot['Cc'])
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText('<br><img src="cid:image1">', 'html')
msgAlternative.attach(msgText)
fp = open("Images\{0}".format(Image),"rb")
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('outlook.abc.com')
#smtp.login('exampleuser', 'examplepass')
smtp.sendmail(strFrom,strTo+strcc, msgRoot.as_string())
smtp.quit()
根据the docs:
Send mail. The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string.
据此,您的串联字符串将被视为单个地址。要修复它,请创建一个列表并将其用于 to_addrs 参数。
to_addrs = [strTo] + cc_list
其中 cc_list 是列表中的 strcc。
问题的原因是 sendmail
方法需要一个地址列表,而您传递的是一个字符串。文档是明确的:
...(a bare string will be treated as a list with 1 address)...
所以你很(不)幸运,CC 列表的第一个成员可以收到邮件...
但是由于您已正确填写邮件的“收件人”和“抄送”字段,您可以简单地使用 send_message
方法,该方法使用这些字段来查找收件人列表。当我们在这里时,您真的应该只连接到邮件服务器一次,然后循环发送邮件:
import smtplib
smtp.connect('outlook.abc.com')
#smtp.login('exampleuser', 'examplepass')
for row in df:
...
msgRoot.attach(msgImage)
smtp.send_message(msgRoot)
smtp.quit()
我开发了一个 python 代码来向选定的用户发送邮件。邮件正在递送至 "TO" 个收件人,但未递送至 "CC" 个收件人。
可以有 100 多个 CC 收件人,这些信息将被硬编码。
请帮我找出下面代码中的错误
conn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=Birthday_Database.accdb;')
cur = conn.cursor()
sql = ("SELECT Name,DOB,Image,Email FROM Calendar where DOB = {}".format(t2))
cur.execute(sql)
df = cur.fetchall()
if len(df) == 0:
print("There are no Birthday's for today!!!!")
sys.exit(0)
for row in df:
myVar1 = row.Name
myVar2 = row.Image
myVar3 = row.Email
# Define these once; use them twice!
strFrom = 'do.not.reply@abc.com'
strTo = myVar3
Image = myVar2
Names = myVar1
strcc = ['qwerty@abc.com','ytrewq@abc.com','poiuyt@abc.com']
strcc = ','.join(strcc)
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Happy Birthday {0}'.format(Names)
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot['Cc'] = strcc
#msgRoot['Cc'] = strcc
msgRoot.preamble = 'This is a multi-part message in MIME format.'
print(msgRoot['Cc'])
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText('<br><img src="cid:image1">', 'html')
msgAlternative.attach(msgText)
fp = open("Images\{0}".format(Image),"rb")
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('outlook.abc.com')
#smtp.login('exampleuser', 'examplepass')
smtp.sendmail(strFrom,strTo+strcc, msgRoot.as_string())
smtp.quit()
根据the docs:
Send mail. The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string.
据此,您的串联字符串将被视为单个地址。要修复它,请创建一个列表并将其用于 to_addrs 参数。
to_addrs = [strTo] + cc_list
其中 cc_list 是列表中的 strcc。
问题的原因是 sendmail
方法需要一个地址列表,而您传递的是一个字符串。文档是明确的:
...(a bare string will be treated as a list with 1 address)...
所以你很(不)幸运,CC 列表的第一个成员可以收到邮件...
但是由于您已正确填写邮件的“收件人”和“抄送”字段,您可以简单地使用 send_message
方法,该方法使用这些字段来查找收件人列表。当我们在这里时,您真的应该只连接到邮件服务器一次,然后循环发送邮件:
import smtplib
smtp.connect('outlook.abc.com')
#smtp.login('exampleuser', 'examplepass')
for row in df:
...
msgRoot.attach(msgImage)
smtp.send_message(msgRoot)
smtp.quit()