为了测试目的,我需要在数百个电子邮件框中填充各种消息,并打算为此使用smtplib。但是除其他外,我不仅需要能够向特定邮箱发送消息,而且还必须向它们发送CC和BCC消息。看起来smtplib在发送电子邮件时不支持CC- ing和BCC-ing。
寻找有关如何执行CC或BCC从python脚本发送消息的建议。
(而且-不,我不是在创建脚本向测试环境之外的任何人发送垃圾邮件。)
电子邮件标头与smtp服务器无关。发送电子邮件时,只需将抄送和密件抄送收件人添加到toaddrs。对于CC,将它们添加到CC标头中。
toaddr = 'buffy@sunnydale.k12.ca.us' cc = ['alexander@sunydale.k12.ca.us','willow@sunnydale.k12.ca.us'] bcc = ['chairman@slayerscouncil.uk'] fromaddr = 'giles@sunnydale.k12.ca.us' message_subject = "disturbance in sector 7" message_text = "Three are dead in an attack in the sewers below sector 7." message = "From: %s\r\n" % fromaddr + "To: %s\r\n" % toaddr + "CC: %s\r\n" % ",".join(cc) + "Subject: %s\r\n" % message_subject + "\r\n" + message_text toaddrs = [toaddr] + cc + bcc server = smtplib.SMTP('smtp.sunnydale.k12.ca.us') server.set_debuglevel(1) server.sendmail(fromaddr, toaddrs, message) server.quit()