mailer.py
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/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'])