aboutsummaryrefslogtreecommitdiffstats
path: root/run/emailer.py
blob: 6d66da2d8c66f9daa9b3537e16ff2ee17f7618cc (plain) (blame)
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
import os
import pwd
import smtplib
import socket

class Emailer(object):
    def __init__(self, target):
        user = pwd.getpwuid(os.getuid())[0]
        host = socket.gethostname()

        self.sender = "%s@%s" % (user, host)
        self.target = target

        self.body = "\r\n".join(["From: %s" % self.sender,
                                 "To: %s" % target,
                                 "Subject: Test Completed!", "", "{}"])

        self.mail = smtplib.SMTP("localhost")

        # Hopefully crash if the server is not running
        self.mail.ehlo()

    def close(self):
        self.mail.quit()

    def send(self, text):
        self.mail.sendmail(self.sender, [self.target],
                           self.body.format(text))