This repository was archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_mail.py
More file actions
137 lines (118 loc) · 4.13 KB
/
send_mail.py
File metadata and controls
137 lines (118 loc) · 4.13 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
"""
"""
# @Time: 2017/6/23 1:43
# @Author:still_night@163.com
# @File: send_mail.py
from __future__ import unicode_literals
import const
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from log import logging
import time
def login():
server=None
retry=5
while retry>0:
retry-=1
try:
server = smtplib.SMTP_SSL(const.SMTPHOST)
server.login(const.SMTPUSERNAME, const.SMTPPASSWORD)
# minfo['server']=server
except smtplib.SMTPException:
# if minfo['server'] :
# minfo['server'].close()
logging.error("smtp登录失败 ,正在重试")
time.sleep(3)
else:
break
else:
raise Exception("SMTP 登录失败")
return server
def send(address,subject,content,From=const.SMTPFULLADDRESS):
s=login()
# server=minfo['server']
message=MIMEMultipart('alternative')
message.set_charset('utf8')
message['FROM']=From
message['Subject']=Header(subject.encode('utf-8'),'UTF-8').encode()
_attach = MIMEText(content.encode('utf-8'), _charset='utf-8')
message.attach(_attach)
retry=5
while retry>0:
retry-=1
try:
s.sendmail(From,[address,],message.as_string())
except (smtplib.SMTPServerDisconnected,smtplib.SMTPConnectError,
smtplib.SMTPAuthenticationError,smtplib.SMTPHeloError,smtplib.SMTPException),e :
logging.warn("邮件失败,正在重试: %s %s %s"%(address,content,e))
login()
else:
break
s.close()
# server.sendmail(From,[address,],message)
def send_check_fail(adress,stu_id,password):
subject=u"[医大一键评教] 密码错误 %s"% stu_id
content=u"""
你好:
我是医大一键评教机器人,
您的教务系统用户名密码有错误.
学号: %s
密码:%s
请您尝试登陆新版正方教务管理平台( http://202.118.40.67/jwglxt/xtgl/dl_loginForward.html )
然后回复正确的账号密码给我
我的邮箱: yijianpingjiao@foxmail.com
感谢支持!
"""% (stu_id,password)
send(adress,subject,content)
def send_check_ok(adress,stu_id,password):
subject = u"[医大一键评教]订单已接收,正在评价 %s " % stu_id
content = u"""
你好:
我是医大一键评教机器人,我们已经收到您的订单,正在马不停蹄帮您刷评价.
请不要在最近登录教务系统后台.
评价结束后将会有邮件通知您.
学号: %s
密码:%s
请您登录教务系统后台确认一下已经评价成功!!
如果有任何问题可以通过邮箱联系我.
我的邮箱是: yjpj_service@sina.com
欢迎将此服务分享给您的朋友圈和空间.
感谢您的支持!
""" % (stu_id, password)
send(adress, subject, content)
def send_comment_ok(adress,stu_id,password):
subject = u"[医大一键评教] 一键评教已成功 学号:%s" % stu_id
content = u"""
你好:
医大一键评教,已经成功帮您完成了教务系统的评价.
有问题可以给我回复
Email: yijianpingjiao@foxmail.com
感谢支持,欢迎分享给朋友!
"""
send(adress, subject, content)
if __name__ == '__main__':
# send_check_fail("still_night@163.com","1","2")
# send_check_ok("still_night@163.com", "1", "2")
send_comment_ok("531903884@qq.com", "1", "2")
# minfo['server'].close()
# FROM = 'yjpj_service@sina.com'
# TO = ["still_night@163.com"] # must be a list
# SUBJECT = "Hello!"
# TEXT = "This message was sent with Python's smtplib."
# # Prepare actual message
# message = """\
# From: %s
# To: %s
# Subject: %s
# %s
# """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
#
# # Send the mail
#
# login()
# server=minfo['server']
# server.sendmail(FROM, TO, message)
# server.quit()