[Python] Send emails to the recepients specified in Message["CC"]
from email.message import Message import smtplibdef sendEmail(subject, content):
"""
Send Email.
"""
try:
smtpServer = "smtp.cnnic.cn"
userName = "liuxiaowei@cnnic.cn"
password = "my_passwd_is_abc"
fromAddr = "liuxiaowei@cnnic.cn"
toAddrs = ["chenyong@cnnic.cn", "lab_student@cnnic.cn"]
ccAddrs = ["gengguanggang@cnnic.cn", "yanzhiwei@cnnic.cn"]
message = Message()
message["Subject"] = subject
message["From"] = fromAddr
message["To"] = ";".join(toAddrs)
#Copy to
#message["CC"] is only for display, to send the email we must specify it in the method "SMTP.sendmail".
message["CC"] = "gengguanggang@cnnic.cn;yanzhiwei@cnnic.cn"
message.set_payload(content)
message.set_charset("utf-8")
msg = message.as_string()
sm = smtplib.SMTP(smtpServer)
sm.set_debuglevel(0)
sm.ehlo()
sm.starttls()
sm.ehlo()
sm.login(userName, password)
sm.sendmail(fromAddr, toAddrs+ccAddrs, msg)
time.sleep(5)
sm.quit()
except Exception, e:
writeLog("EMAIL SENDING ERROR", "", traceback.format_exc())
else:
writeLog("EMAIL SENDING SUCCESS", "", "")
页:
[1]