【发布时间】:2019-01-01 22:31:21
【问题描述】:
我开发了一个 python 代码来向选定的用户发送邮件。邮件被递送到“TO”收件人,而不是“CC”收件人。
可以有 100 个抄送收件人,这些信息将被硬编码。
请帮我找出下面代码中的错误
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()
【问题讨论】:
-
我有一个类似的代码。唯一的区别在于 sendmail 调用:我使用 [strTo, strcc] 而不是 strTo+strcc
-
只有抄送列表中的第一个收件人才能收到邮件,其他人没有。