summaryrefslogtreecommitdiffstats
path: root/source-builder/sb/mailer.py
blob: b9ef81232f99703313f1b01865642a61f72b8783 (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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2013 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-tools'.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#
# Manage emailing results or reports.
#

from __future__ import print_function

import os
import smtplib
import socket

import error
import options
import path

def append_options(opts):
    opts['--mail'] = 'Send email report or results.'
    opts['--smtp-host'] = 'SMTP host to send via.'
    opts['--mail-to'] = 'Email address to send the email too.'
    opts['--mail-from'] = 'Email address the report is from.'

class mail:
    def __init__(self, opts):
        self.opts = opts

    def from_address(self):

        def _clean(l):
            if '#' in l:
                l = l[:l.index('#')]
            if '\r' in l:
                l = l[:l.index('r')]
            if '\n' in l:
                l = l[:l.index('\n')]
            return l.strip()

        addr = self.opts.get_arg('--mail-from')
        if addr is not None:
            return addr[1]
        mailrc = None
        if 'MAILRC' in os.environ:
            mailrc = os.environ['MAILRC']
        if mailrc is None and 'HOME' in os.environ:
            mailrc = path.join(os.environ['HOME'], '.mailrc')
        if mailrc is not None and path.exists(mailrc):
            # set from="Joe Blow <joe@blow.org>"
            try:
                mrc = open(mailrc, 'r')
                lines = mrc.readlines()
                mrc.close()
            except IOError as err:
                raise error.general('error reading: %s' % (mailrc))
            for l in lines:
                l = _clean(l)
                if 'from' in l:
                    fa = l[l.index('from') + len('from'):]
                    if '=' in fa:
                        addr = fa[fa.index('=') + 1:].replace('"', ' ').strip()
            if addr is not None:
                return addr
        addr = self.opts.defaults.get_value('%{_sbgit_mail}')
        return addr

    def smtp_host(self):
        host = self.opts.get_arg('--smtp-host')
        if host is not None:
            return host[1]
        host = self.opts.defaults.get_value('%{_mail_smtp_host}')
        if host is not None:
            return host
        return 'localhost'

    def send(self, to_addr, subject, body):
        from_addr = self.from_address()
        msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
            (from_addr, to_addr, subject) + body
        try:
            s = smtplib.SMTP(self.smtp_host())
            s.sendmail(from_addr, [to_addr], msg)
        except smtplib.SMTPException as se:
            raise error.general('sending mail: %s' % (str(se)))
        except socket.error as se:
            raise error.general('sending mail: %s' % (str(se)))

if __name__ == '__main__':
    import sys
    optargs = {}
    append_options(optargs)
    opts = options.load(sys.argv, optargs = optargs, defaults = 'defaults.mc')
    m = mail(opts)
    print('From: %s' % (m.from_address()))
    print('SMTP Host: %s' % (m.smtp_host()))
    m.send(m.from_address(), 'Test mailer.py', 'This is a test')