Python 电子邮件不会发送给抄送收件人
我已经开发了一个python代码来向选定的用户发送邮件。邮件将发送给收件人,但不会发送给抄送收件人 可能有100个抄送收件人,这些信息将被硬编码 请帮助我识别下面代码中的错误Python 电子邮件不会发送给抄送收件人,python,email,mime,Python,Email,Mime,我已经开发了一个python代码来向选定的用户发送邮件。邮件将发送给收件人,但不会发送给抄送收件人 可能有100个抄送收件人,这些信息将被硬编码 请帮助我识别下面代码中的错误 conn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=Birthday_Database.accdb;') cur = conn.cursor() sql = ("SELECT Name,DOB,Image,Email FR
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()
根据:
发邮件。所需参数包括RFC 822 from地址字符串、RFC 822 to地址字符串列表、裸字符串将被视为具有1个地址的列表以及消息字符串
因此,连接的字符串将被视为单个地址。要修复它,请创建一个列表并将其用于To_addrs参数
to_addrs = [strTo] + cc_list
其中,cc_list作为列表输入。问题的原因是sendmail方法需要一个地址列表,而您只传递一个字符串。文件明确指出: …裸字符串将被视为具有1个地址的列表 所以你很不幸,抄送名单上的第一个成员可以收到邮件 但是,由于您已经正确填写了邮件的“收件人”和“抄送”字段,您可以简单地使用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()
我有一个类似的代码。唯一的区别在于sendmail调用:我使用[strTo,strc]而不是strTo+strc,只是CC列表id中第一个接收邮件的收件人,而不是其他收件人。