-
Notifications
You must be signed in to change notification settings - Fork 1
/
emailer.py
44 lines (39 loc) · 1.33 KB
/
emailer.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
import os
import sys
import smtplib
from termcolor import colored
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from application import User, db, Role
class Emailer:
def __init__(self):
# get email credentials
if 'EMAIL_PASSWORD' in os.environ and 'EMAIL_ADDRESS' in os.environ:
self.address = os.environ.get('EMAIL_ADDRESS')
password = os.environ.get('EMAIL_PASSWORD')
# setup mailer
self.mail_server = smtplib.SMTP("smtp.gmail.com", 587)
self.mail_server.starttls()
self.mail_server.login(self.address, password)
else:
print colored('ERROR: set environment variable EMAIL_ADDRESS AND EMAIL_PASSWORD', 'red')
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.mail_server.quit()
def email_user(self, email, name, subject, text):
msg = MIMEMultipart()
msg['From'] = self.address
msg['To'] = email
msg['Subject'] = subject
msg.attach(MIMEText(text))
self.mail_server.sendmail(self.address, email, msg.as_string())
print colored("SENT ONE EMAIL TO " + email, 'green')
"""
Usage:
if __name__ == '__main__':
with Emailer() as emailer:
emailer.email_user('[email protected]', 'Testname', 'testsubject', 'testteset')
"""