1 | # |
---|
2 | # RTEMS Tools Project (http://www.rtems.org/) |
---|
3 | # Copyright 2013-2016 Chris Johns (chrisj@rtems.org) |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
7 | # |
---|
8 | # Redistribution and use in source and binary forms, with or without |
---|
9 | # modification, are permitted provided that the following conditions are met: |
---|
10 | # |
---|
11 | # 1. Redistributions of source code must retain the above copyright notice, |
---|
12 | # this list of conditions and the following disclaimer. |
---|
13 | # |
---|
14 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | # this list of conditions and the following disclaimer in the documentation |
---|
16 | # and/or other materials provided with the distribution. |
---|
17 | # |
---|
18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
22 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
28 | # POSSIBILITY OF SUCH DAMAGE. |
---|
29 | # |
---|
30 | |
---|
31 | # |
---|
32 | # Manage emailing results or reports. |
---|
33 | # |
---|
34 | |
---|
35 | from __future__ import print_function |
---|
36 | |
---|
37 | import os |
---|
38 | import smtplib |
---|
39 | import socket |
---|
40 | |
---|
41 | # |
---|
42 | # Support to handle use in a package and as a unit test. |
---|
43 | # If there is a better way to let us know. |
---|
44 | # |
---|
45 | try: |
---|
46 | from . import error |
---|
47 | from . import options |
---|
48 | from . import path |
---|
49 | except (ValueError, SystemError): |
---|
50 | import error |
---|
51 | import options |
---|
52 | import path |
---|
53 | |
---|
54 | def append_options(opts): |
---|
55 | opts['--mail'] = 'Send email report or results.' |
---|
56 | opts['--smtp-host'] = 'SMTP host to send via.' |
---|
57 | opts['--mail-to'] = 'Email address to send the email too.' |
---|
58 | opts['--mail-from'] = 'Email address the report is from.' |
---|
59 | |
---|
60 | class mail: |
---|
61 | def __init__(self, opts): |
---|
62 | self.opts = opts |
---|
63 | |
---|
64 | def from_address(self): |
---|
65 | |
---|
66 | def _clean(l): |
---|
67 | if '#' in l: |
---|
68 | l = l[:l.index('#')] |
---|
69 | if '\r' in l: |
---|
70 | l = l[:l.index('r')] |
---|
71 | if '\n' in l: |
---|
72 | l = l[:l.index('\n')] |
---|
73 | return l.strip() |
---|
74 | |
---|
75 | addr = self.opts.get_arg('--mail-from') |
---|
76 | if addr is not None: |
---|
77 | return addr[1] |
---|
78 | mailrc = None |
---|
79 | if 'MAILRC' in os.environ: |
---|
80 | mailrc = os.environ['MAILRC'] |
---|
81 | if mailrc is None and 'HOME' in os.environ: |
---|
82 | mailrc = path.join(os.environ['HOME'], '.mailrc') |
---|
83 | if mailrc is not None and path.exists(mailrc): |
---|
84 | # set from="Joe Blow <joe@blow.org>" |
---|
85 | try: |
---|
86 | mrc = open(mailrc, 'r') |
---|
87 | lines = mrc.readlines() |
---|
88 | mrc.close() |
---|
89 | except IOError as err: |
---|
90 | raise error.general('error reading: %s' % (mailrc)) |
---|
91 | for l in lines: |
---|
92 | l = _clean(l) |
---|
93 | if 'from' in l: |
---|
94 | fa = l[l.index('from') + len('from'):] |
---|
95 | if '=' in fa: |
---|
96 | addr = fa[fa.index('=') + 1:].replace('"', ' ').strip() |
---|
97 | if addr is not None: |
---|
98 | return addr |
---|
99 | addr = self.opts.defaults.get_value('%{_sbgit_mail}') |
---|
100 | return addr |
---|
101 | |
---|
102 | def smtp_host(self): |
---|
103 | host = self.opts.get_arg('--smtp-host') |
---|
104 | if host is not None: |
---|
105 | return host[1] |
---|
106 | host = self.opts.defaults.get_value('%{_mail_smtp_host}') |
---|
107 | if host is not None: |
---|
108 | return host |
---|
109 | return 'localhost' |
---|
110 | |
---|
111 | def send(self, to_addr, subject, body): |
---|
112 | from_addr = self.from_address() |
---|
113 | msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \ |
---|
114 | (from_addr, to_addr, subject) + body |
---|
115 | try: |
---|
116 | s = smtplib.SMTP(self.smtp_host()) |
---|
117 | s.sendmail(from_addr, [to_addr], msg) |
---|
118 | except smtplib.SMTPException as se: |
---|
119 | raise error.general('sending mail: %s' % (str(se))) |
---|
120 | except socket.error as se: |
---|
121 | raise error.general('sending mail: %s' % (str(se))) |
---|
122 | |
---|
123 | if __name__ == '__main__': |
---|
124 | import sys |
---|
125 | optargs = {} |
---|
126 | append_options(optargs) |
---|
127 | opts = options.load(sys.argv, optargs = optargs, defaults = 'defaults.mc') |
---|
128 | m = mail(opts) |
---|
129 | print('From: %s' % (m.from_address())) |
---|
130 | print('SMTP Host: %s' % (m.smtp_host())) |
---|
131 | m.send(m.from_address(), 'Test mailer.py', 'This is a test') |
---|