-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.py
38 lines (33 loc) · 993 Bytes
/
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
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 logging
import json
logging.basicConfig(level=logging.INFO)
signature = '\r\n\r\n\r\nThe Talao/Altme team.\r\nhttps://talao.io/'
keys = json.load(open('keys.json'))
password = keys['smtp_password'] # used in smtp.py
def message(subject, to, messagetext):
fromaddr = "relay@talao.io"
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'))
# 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 Exception:
logging.error('sending mail')
return
s.quit()
return True