aboutsummaryrefslogtreecommitdiffstats
path: root/bin/common/srtool_email.py
blob: 103c8be0c8e9998f96bd2e2de3dddf991d5a680d (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python3
#
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Security Response Tool Implementation
#
# Copyright (C) 2017       Wind River Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#
# Theory of operation
#
#  * Send an email via Python's smtplib
#  * Support encryption and passwords

import os
import sys
import argparse
import smtplib
from email.mime.text import MIMEText

# Setup:
toaddrs = ''
fromaddr = ''
smtpserver = ''
smtpencryption = ''
srt_user = ''
srt_passwd = ''
subject = ''
msg = ''
verbose = False
test = False

#################################
# Send the email
#

def prompt(prompt):
    return input(prompt).strip()

def enter_message():
    global msg

    print("Enter message, end with ^D (Unix) or ^Z (Windows):")
    msg = ''
    while True:
        try:
            line = input()
        except EOFError:
            break
        if not line:
            break
        msg = msg + line

    print("Message length is", len(msg))

#################################
# Send the email
#

def send_email():
    global msg

    # Add the headers at the start!
    msg = MIMEText(msg)
    msg['Subject'] = subject
    msg['From'] = fromaddr
    msg['To'] = ', '.join(toaddrs)

    if test:
        print("From:%s" % fromaddr)
        print("To:%s" % toaddrs)
        print("Subject:%s" % subject)
        if 'tls' == smtpencryption:
            print("TLS:yes")
        if srt_user:
            print("User:%s" % srt_user)
        print("Message:")
        print("----" % msg)
        print("%s" % msg.as_string())
        print("----" % msg)
    else:
        server = smtplib.SMTP(smtpserver)
        if verbose:
            server.set_debuglevel(1)
        if 'tls' == smtpencryption:
            server.starttls()
        if srt_user:
            server.login(srt_user, srt_passwd)
        server.sendmail(fromaddr, toaddrs, msg.as_string())
        server.quit()

#################################
# main loop
#

def main(argv):
    global toaddrs
    global fromaddr
    global smtpserver
    global smtpencryption
    global srt_user
    global srt_passwd
    global subject
    global msg
    global verbose
    global test

    parser = argparse.ArgumentParser(description='srtool_email.py: SRTool email handler')
    parser.add_argument('--from', dest='smtpfrom', help='From email address')
    parser.add_argument('--to', dest='smtpto', help='To email address')
    parser.add_argument('--subject', '-s', dest='subject', help='Subject for email address')
    parser.add_argument('--server', dest='smtpserver', help='SMTP server address')
    parser.add_argument('--user', dest='user', help='User name for Jira access')
    parser.add_argument('--passwd', dest='passwd', help='User password for Jira access')
    parser.add_argument('--tls', '-t', action='store_true', dest='tls', help='Use TLS encryption')
    parser.add_argument('--message', '-m', dest='message', help='Message to send')
    parser.add_argument('--file', '-f', dest='file', help='File to send')
    parser.add_argument('--prompt', '-p', action='store_true', dest='prompt', help='Directly enter a message')
    parser.add_argument('--verbose', '-v', action='store_true', dest='verbose', help='Verbose output from SMTP connection')
    parser.add_argument('--test', action='store_true', dest='test', help='Test the email setup')
    args = parser.parse_args()

    # Resolve the arguments
    if args.smtpfrom:
        fromaddr = args.smtpfrom
    else:
        print("ERROR: missing 'from' address")
        exit(1)
    if args.smtpto:
        toaddrs = args.smtpto
        toaddrs = toaddrs.split(',')
    else:
        print("ERROR: missing 'to' address")
        exit(1)
    if args.subject:
        subject = args.subject
    else:
        print("ERROR: missing 'subject'")
        exit(1)
    if args.smtpserver:
        smtpserver = args.smtpserver
    else:
        smtpserver = os.environ.get('SRT_SMTP')
    if not smtpserver:
        smtpserver = 'localhost'
    verbose = args.verbose
    test = args.test

    # Authorization
    if args.user:
        srt_user = args.user
    else:
        srt_user = os.environ.get('SRT_USER')
    if args.passwd:
        srt_passwd = args.passwd
    else:
        srt_passwd = os.environ.get('SRT_PASSWD')

    # Encryption
    if args.tls:
        smtpencryption = 'tls'
    else:
        smtpencryption = ''

    # Set up the message
    if args.message:
        msg = args.message
    elif args.file:
        fp = open(args.file, 'rb')
        # Create a text/plain message
        msg = fp.read()
        fp.close()
    elif args.prompt:
        enter_message()
    else:
        print("ERROR: missing message source")
        exit(1)

    # Send the email
    send_email()


if __name__ == '__main__':
    script_pathname=os.path.dirname(sys.argv[0])
    main(sys.argv[1:])