forked from intelxed/xed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmail.py
executable file
·231 lines (210 loc) · 6.79 KB
/
zmail.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
#BEGIN_LEGAL
#
#Copyright (c) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#END_LEGAL
import re
import os
import smtplib
import sys
import argparse
def msg(s):
sys.stdout.write(s + "\n")
def msgb(s, t=''):
sys.stdout.write('[{}] {}\n'.format(s, t))
def die(s):
msgb('FAILING',s)
sys.exit(1)
def warn(m):
msg("[WARNING] " + m)
############################################################################
# Stuff for sending mail or just a making an note file
def _check_muttrc():
if 'HOME' in os.environ:
home = os.environ['HOME']
muttrc = os.path.join(home,'.muttrc')
if os.path.exists(muttrc):
f = open(muttrc,"r")
lines = f.readlines()
f.close()
for line in lines:
g= re.search(r'set[ ]+from[ ]*[=][ ]*(?P<email>[A-Za-z0-9_.@]+)',
line)
if g:
sender = g.group('email')
return sender
warn("Cannot find \'from\' setting in .muttrc." +
" Please set it to your email address.")
return None
else:
warn("Cannot find " + muttrc + " file where you " +
"should set your email address.")
return None
warn("Cannot find your HOME environment variable. " +
"Cannot look for your .muttrc file.")
return None
def check_smtp_host_env_var():
servers = []
if 'SMTP_HOST' in os.environ:
servers.append(os.environ['SMTP_HOST'])
else:
servers.append('ecsmtp.hd.intel.com')
servers.append('ecsmtp.iil.intel.com')
return servers
def check_reply_to_env_var():
if 'REPLYTO' in os.environ:
return os.environ['REPLYTO']
sender = _check_muttrc()
if sender:
return sender
die("Please either set your REPLYTO environment variable " +
"to your @intel.com\n " +
"email address or have a\n\tset " +
"[email protected]\n" +
"in your $HOME/.muttrc.\n\n")
def find_sender():
return check_reply_to_env_var()
############################################################
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def _send_email(recipients_list,
sender,
subject,
body,
attachments_list = [],
cc_recipients_list = [],
verbose = 0):
"""Send the body string and any attachments to the list of
recipients. The attachments_list is a list of tuples (real_name,
attachment_name)"""
if verbose>50:
msgb('email 0.0')
all_recipients = recipients_list + cc_recipients_list
#recipients = ", ".join(all_recipients)
recipients = ", ".join(recipients_list)
cc_recipients = ", ".join(cc_recipients_list)
# Create the enclosing (outer) message
outer = MIMEMultipart()
outer['Subject'] = subject
outer['To'] = recipients
outer['Cc'] = cc_recipients
outer['From'] = sender
if verbose:
msgb("FROM", sender)
msgb("TO", recipients)
msgb("CC", cc_recipients)
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
if verbose > 50:
msgb('email 0.5')
msg = MIMEText(body, _subtype='plain')
msg.add_header('Content-Disposition', 'inline')
outer.attach(msg)
if verbose > 50:
msgb('email 1.0')
for (real_name,attachment_name) in attachments_list:
if verbose > 50:
msgb('email 2.0', real_name + " " + attachment_name )
if not os.path.exists(real_name):
die("Cannot read attachment named: " + real_name)
fp = open(real_name,'r')
msg = MIMEText(fp.read(), _subtype='plain')
fp.close()
if verbose > 50:
msgb('3.0')
msg.add_header('Content-Disposition', 'attachment',
filename=attachment_name)
outer.attach(msg)
if verbose > 50:
msgb('4.0')
# Now send or store the message
composed = outer.as_string()
s = smtplib.SMTP()
if verbose > 50:
s.set_debuglevel(1)
# try connecting to a server from the list
mail_server_list = check_smtp_host_env_var()
connected = False
for outgoing_mail_server in mail_server_list:
try:
msgb("MAIL SERVER", outgoing_mail_server)
s.connect(outgoing_mail_server)
connected = True
break
except:
continue
# last resort try the default
if not connected:
s.connect()
rdict = s.sendmail(sender, all_recipients, composed)
s.quit()
if rdict and len(rdict) > 0:
die("MAIL FAILED FOR " + str(rdict))
def mail(note,
sender,
recipients,
cc_recipients=[],
attachments=[],
subject = '',
verbosity = 0):
"""mail note to the to_recipient and the cc_recipient"""
if verbosity > 1:
msgb("SENDING EMAIL")
note = [x.rstrip() for x in note]
body = '\n'.join(note)
att = []
for attachment in attachments:
att.append( (attachment, os.path.basename(attachment)) )
try:
_send_email(recipients,
sender,
subject,
body,
att,
cc_recipients,
verbosity)
except:
die("Sending email failed")
return 0
def getargs():
parser = argparse.ArgumentParser()
parser.add_argument("-m",
dest="message",
default=[],
action="append",
help="Message to send")
parser.add_argument("-f",
dest="sender",
help="Sender")
parser.add_argument("-t",
dest="recipients",
action="append",
default=[],
help="Recipient")
parser.add_argument("-v",
dest="verbosity",
default=0,
type=int,
help="Verbosity")
args = parser.parse_args()
return args
def main():
args = getargs()
r = mail(args.message, args.sender, args.recipients,
verbosity=args.verbosity)
return r
if __name__ == '__main__':
r = main()
sys.exit(r)