-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailer.py
53 lines (45 loc) · 1.55 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
45
46
47
48
49
50
51
52
53
import smtplib
import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class Emailer:
def __init__(self, smtp_info, outgoing_addr, outgoing_pass):
"""Sends emails easily.
Args:
smtp_info: A tuple with SMTP host and port.
outgoing_addr: Outgoing email address.
outgoing_pass: Outgoing email password.
"""
if not outgoing_addr:
raise ValueError("Outgoing email address is required.")
if not outgoing_pass:
raise ValueError("Outgoing email password is required.")
self._server = None
self._smtp_info = smtp_info
self._addr = outgoing_addr
self._pass = outgoing_pass
self._last_status_email = datetime.datetime.now()
def send(self, to, subject, body):
"""Send a message.
Args:
to: Recipient email address.
subject: String of subject.
body: Body of the email.
"""
# Create the email.
msg = MIMEMultipart()
msg['From'] = self._addr
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
# send message
self._connect()
self._server.starttls()
self._server.login(self._addr, self._pass)
self._server.sendmail(self._addr, to, msg.as_string())
self._disconnect()
def _connect(self):
self._server = smtplib.SMTP(*self._smtp_info)
def _disconnect(self):
self._server.quit()
self._server = None