我有一个问题,我将如何使用此
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
这是我的代码:
import sys
import os
import re
from smtplib import SMTP_SSL as SMTP
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
SMTPserver = 'server.me.com'
sender = 'from@yyy.com'
destination = 'to@yyy.com'
recipient = ['rcp1@aaa.com', 'rcp2@aaa.com', 'rcp3@aaa.com']
USERNAME = 'user'
PASSWORD = 'pass'
text_subtype = 'plain'
content='This is a test for mail sending'
subject='Test'
try:
msg = MIMEMultipart()
msg = MIMEText(content, text_subtype)
msg['From'] = sender
msg['To'] = destination
msg['Cc'] = ', '.join(recipient)
msg['Subject']= subject
conn = SMTP(SMTPserver)
conn.login(USERNAME, PASSWORD)
try:
**conn.sendmail(sender, destination, msg.as_string())**
except Exception, exc:
sys.exit( "connection failed; %s" % str(exc) )
finally:
conn.close()
except Exception, exc:
sys.exit( "mail failed; %s" % str(exc) )
这是正确的语法还是代码?我想要的是我还想将邮件发送到收件人列表,但我应该把它放在代码中?
答案 0 :(得分:1)
非常好。您是否真的尝试过使用您的代码(另请参阅注释)?
您唯一需要注意的是conn.sendmail()中的destination
需要是一个列表,所以[destination]
。然后,您可以轻松地将其替换为recipient
,因为那已经是一个列表。
这里有一些很好的示例:http://docs.python.org/library/email-examples.html#email-examples,您的代码与这些示例中的代码几乎相同。
当您运行代码并获得无法解决的实际错误时,欢迎您回到此处并提出新问题。