mailer.py 1.12 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time


class mailman(object):
    def __init__(self):
        self.sender = 'robot@yohoops.cn'
        self.smtpObj = smtplib.SMTP('localhost')
     
    def mail(self,receivers=[],Content='Test Mail',From ='Robot',To='YOHOOPS',Title='Test Mail'):
        message = MIMEText(Content, 'plain', 'utf-8')
        message['From'] = Header(From, 'utf-8') 
        message['To'] =  Header(To, 'utf-8') 
        message['Subject'] = Header(Title, 'utf-8')
        
        retry = 5
        while retry>0:
            try:
                self.smtpObj.sendmail(self.sender, receivers, message.as_string())
                print "邮件发送成功"
                return True
            except Exception as e:
                retry-=1
                print e
                print "Error: 无法发送邮件"
                print "重新发送邮件...还剩{0}次".format(retry)
                time.sleep(10)
        return False

if __name__ == '__main__':
    demo = mailman()
    demo.mail(['tiexin.yang@yoho.cn'])