-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.py
93 lines (74 loc) · 2.51 KB
/
message.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.utils import formataddr
from email.mime.text import MIMEText
import codecs
import logging
import json
logging.basicConfig(level=logging.INFO)
signature = '\r\n\r\n\r\nThe Altme team.\r\nhttps://altme.io/'
# dict of HTML templates with commented formating needed
HTML_templates = {'code_auth_en': 'templates/code_auth_en.html', # code
'code_auth_fr': 'templates/code_auth_fr.html', # code
}
def messageHTML(subject, to, HTML_key, format_dict):
password = json.load(open("keys.json", "r"))["smtp_password"]
fromaddr = "[email protected]"
toaddr = [to]
msg = MIMEMultipart()
msg['From'] = formataddr((str(Header('Altme', 'utf-8')), fromaddr))
msg['To'] = ", ".join(toaddr)
msg['Subject'] = subject
# string to store the body of the mail
if HTML_key not in HTML_templates:
logging.error('wrong HTML_key')
return False
template = HTML_templates[HTML_key]
try:
html = str(codecs.open(template, 'r', 'utf-8').read()
).format(**format_dict)
except Exception as e:
logging.error('Upload email template : %s', str(e))
return False
msg.attach(MIMEText(html, 'html', 'utf-8'))
# p = MIMEBase('application', 'octet-stream')
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(fromaddr, password)
text = msg.as_string()
# sending the mail
try:
s.sendmail(msg['from'], msg["To"].split(","), text)
logging.info('email sent')
s.quit()
return True
except:
logging.error('sending mail')
s.quit()
return False
def message(subject, to, messagetext):
password = json.load(open("keys.json", "r"))["smtp_password"]
fromaddr = "[email protected]"
toaddr = [to]
msg = MIMEMultipart()
msg['From'] = formataddr((str(Header('Altme', 'utf-8')), fromaddr))
msg['To'] = ", ".join(toaddr)
msg['Subject'] = subject
body = messagetext + signature
msg.attach(MIMEText(body, 'plain'))
# p = MIMEBase('application', 'octet-stream')
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(fromaddr, password)
text = msg.as_string()
# sending the mail
try:
s.sendmail(msg['from'], msg["To"].split(","), text)
except:
logging.error('sending mail')
return False
s.quit()
return True