-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
70 lines (63 loc) · 1.9 KB
/
mail.py
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
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import secrets_parser, time
import threading
import traceback
GMAIL_PASSWORD=secrets_parser.parse("variables.txt")["GMAIL_PASSWORD"]
mail_queue=[]
queue_lock=threading.Lock()
smtp_server = "smtp.gmail.com"
smtp_port = 587
smtp_user = "[email protected]"
smtp_password = GMAIL_PASSWORD
server = None
def internal_mail(to, subject, html):
global server
msg = MIMEMultipart()
msg['From'] = "Exun Clan <[email protected]>"
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(html, 'html'))
try:
server.sendmail(smtp_user, to, msg.as_string())
return True
except Exception as e:
traceback.print_exc()
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_user, smtp_password)
return "resend"
except:
traceback.print_exc()
return False
def mail_request(to, subject, html, callback=lambda request:None):
queue_lock.acquire()
mail_queue.append({"to":to, "subject":subject, "html":html, "callback":callback})
queue_lock.release()
def mail_thread():
global mail_queue
global server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_user, smtp_password)
while True:
while len(mail_queue)==0:
time.sleep(0.1)
queue_lock.acquire()
mail=mail_queue[0]
mail_queue=mail_queue[1:]
queue_lock.release()
i=0
while True:
i+=1
if i==3:
break
response=internal_mail(mail["to"], mail["subject"], mail["html"])
if response=="resend":
continue
if response:
mail["callback"](mail)
break
threading.Thread(target=mail_thread).start()