Posted by:
努力记

努力记,通过记录,计划,让自己的努力可以触碰彩虹!

4,399

Python发送邮件

  • 2015-02-07
  • 暂无评论

下面是具体实例
注意不能用email作为文件名或者包名

import smtplib
import sys
from email.mime.text import MIMEText

email_config = {
'mail_host': 'smtp.exmail.qq.com',
'mail_port': '25',
'mail_user': 'gaojie@limei.com',
'mail_pass': '52906814',
'send_mail': 'gaojie@limei.com',
'mail_user_name': '高杰'
}

email_report_list = ['gaojie@limei.com']


class Email(object):
# 构造函数
def __init__(self):
self.mail_host = ''
self.mail_port = ''
self.mail_user = ''
self.mail_pass = ''
self.send_mail = ''
self.from_email = ''
pass

def smtp(self, email_config):
self.mail_host = email_config['mail_host']
self.mail_port = email_config['mail_port']
self.mail_user = email_config['mail_user']
self.mail_pass = email_config['mail_pass']
self.send_mail = email_config['send_mail'] if 'send_mail' in email_config else "%s@%s" % (self.mail_user,email_config['mail_postfix'])
if 'from_email' in email_config:
self.from_email = email_config['from_email']
elif 'mail_user_name' in email_config:
self.from_email = email_config['mail_user_name']+"<"+self.send_mail+">"
else:
self.from_email = self.send_mail

def send(self, title, message, to, from_email=''):
if from_email is not '':
from_email = self.from_email
msg = MIMEText(message, _charset='gbk')
msg['Subject'] = title
msg['From'] = from_email
msg['To'] = ";".join(to) if type(to) is list else to
try:
s = smtplib.SMTP()
s.connect(self.mail_host, self.mail_port)
s.login(self.mail_user, self.mail_pass)
s.sendmail(from_email, to if type(to) is list else [to], msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False


if __name__ == '__main__':
o_email = Email()
o_email.smtp(email_config)
o_email.send("DoubleClick Except", "allow send mail to gaojie@limei.com", email_report_list)

 



back up ↑

无觅相关文章插件,快速提升流量