Python-邮件客户端-以html邮件为例,适合群发

#!/usr/local/bin/python #coding=utf-8   def mail(): import smtplib import email from email.MIMEText import MIMEText from time import sleep   dir_path = '/xxx/' html_file = open(dir_path+'html.d/'+raw_input('html_file: '), 'r') subject_file = open('/xxx/subject.txt', 'r') recipient_file = open(dir_path+'recipient.d/'+raw_input('recipient_file: '), 'r') html = html_file.read()   sender = '[email protected]' subject = subject_file.readline() subject = email.Header.Header(subject, 'utf-8') msg = MIMEText(html, 'html') msg['From'] = sender msg['Subject'] = subject   smtp_server = smtplib.SMTP('smtp.xxx.xxx') #smtp_server.set_debuglevel(1) smtp_server.login('[email protected]', 'xxx')   while True: recipient = recipient_file.readline() if len(recipient) == 0: break msg['To'] = recipient smtp_server.sendmail(sender, recipient, msg.as_string()) del msg['To'] sleep(2)   html_file.close() subject_file.close() recipient_file.close()   mail()

 

 

 

当然,以上python脚本只是实现了基本的html邮件功能。在生产环境中(我是放在我的VPS cyent.org (CentOS5.5)上使用,用这做群发器,配合crontab -e 每天可以发出将近5000封邮件,主要面向QQ邮箱)——即与另一篇博文《【解决】crontab shell、python程序不执行的解决办法 》搭配使用

 

另外还有许多问题需要解决,比如字符编码的问题(包括字符集的选择与邮件编码,这种脚本发出的html内容是未经编码的,而通常的邮件是经过base64编码)、又比如smtp服务器是163,则发了没几封就被挡等 等问题。这些还需根据实际的工作环境而调整。

你可能感兴趣的:(Python)