-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsendmail.py
More file actions
executable file
·615 lines (522 loc) · 19.1 KB
/
sendmail.py
File metadata and controls
executable file
·615 lines (522 loc) · 19.1 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#!/usr/bin/env python
# cardinal_pythonlib/email_utils/sendmail.py
"""
===============================================================================
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
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
https://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.
===============================================================================
**Sends e-mails from the command line.**
"""
import argparse
import email.encoders
import email.mime.base
import email.mime.text
import email.mime.multipart
import email.header
import email.utils
import logging
import os
import re
import smtplib
import sys
from typing import List, NoReturn, Sequence, Tuple, Union
from cardinal_pythonlib.logs import get_brace_style_log_with_null_handler
log = get_brace_style_log_with_null_handler(__name__)
# =============================================================================
# Constants
# =============================================================================
CONTENT_TYPE_TEXT = "text/plain"
CONTENT_TYPE_HTML = "text/html"
ASCII = "ascii"
UTF8 = "utf8"
COMMA = ","
COMMASPACE = ", "
STANDARD_SMTP_PORT = 25
STANDARD_TLS_PORT = 587
# =============================================================================
# Make e-mail message
# =============================================================================
def make_email(
from_addr: str,
date: str = None,
sender: str = "",
reply_to: Union[str, List[str]] = "",
to: Union[str, List[str]] = "",
cc: Union[str, List[str]] = "",
bcc: Union[str, List[str]] = "",
subject: str = "",
body: str = "",
content_type: str = CONTENT_TYPE_TEXT,
charset: str = UTF8,
attachment_filenames: Sequence[str] = None,
attachment_binaries: Sequence[bytes] = None,
attachment_binary_filenames: Sequence[str] = None,
verbose: bool = False,
) -> email.mime.multipart.MIMEMultipart:
"""
Makes an e-mail message.
Arguments that can be multiple e-mail addresses are (a) a single e-mail
address as a string, or (b) a list of strings (each a single e-mail
address), or (c) a comma-separated list of multiple e-mail addresses.
Args:
from_addr:
name of the sender for the "From:" field
date:
e-mail date in RFC 2822 format, or ``None`` for "now"
sender:
name of the sender for the "Sender:" field
reply_to:
name of the sender for the "Reply-To:" field
to:
e-mail address(es) of the recipients for "To:" field
cc:
e-mail address(es) of the recipients for "Cc:" field
bcc:
e-mail address(es) of the recipients for "Bcc:" field
subject:
e-mail subject
body:
e-mail body
content_type:
MIME type for body content, default ``text/plain``
charset:
character set for body; default ``utf8``
attachment_filenames:
filenames of attachments to add
attachment_binaries:
binary objects to add as attachments
attachment_binary_filenames:
filenames corresponding to ``attachment_binaries``
verbose: be verbose?
Returns:
a :class:`email.mime.multipart.MIMEMultipart`
Raises:
:exc:`AssertionError`, :exc:`ValueError`
"""
def _csv_list_to_list(x: str) -> List[str]:
stripped = [item.strip() for item in x.split(COMMA)]
return [item for item in stripped if item]
def _assert_nocomma(x: Union[str, List[str]]) -> None:
if isinstance(x, str):
x = [x]
for _addr in x:
assert (
COMMA not in _addr
), f"Commas not allowed in e-mail addresses: {_addr!r}"
# -------------------------------------------------------------------------
# Arguments
# -------------------------------------------------------------------------
if not date:
date = email.utils.formatdate(localtime=True)
assert isinstance(from_addr, str), (
f"'From:' can only be a single address "
f"(for Python sendmail, not RFC 2822); was {from_addr!r}"
)
_assert_nocomma(from_addr)
assert isinstance(
sender, str
), f"'Sender:' can only be a single address; was {sender!r}"
_assert_nocomma(sender)
if isinstance(reply_to, str):
reply_to = [reply_to] if reply_to else [] # type: List[str]
_assert_nocomma(reply_to)
if isinstance(to, str):
to = _csv_list_to_list(to)
if isinstance(cc, str):
cc = _csv_list_to_list(cc)
if isinstance(bcc, str):
bcc = _csv_list_to_list(bcc)
assert to or cc or bcc, "No recipients (must have some of: To, Cc, Bcc)"
_assert_nocomma(to)
_assert_nocomma(cc)
_assert_nocomma(bcc)
attachment_filenames = attachment_filenames or [] # type: List[str]
assert all(
attachment_filenames
), f"Missing attachment filenames: {attachment_filenames!r}"
attachment_binaries = attachment_binaries or [] # type: List[bytes]
attachment_binary_filenames = (
attachment_binary_filenames or []
) # type: List[str]
assert len(attachment_binaries) == len(attachment_binary_filenames), (
"If you specify attachment_binaries or attachment_binary_filenames, "
"they must be iterables of the same length."
)
assert all(attachment_binary_filenames), (
f"Missing filenames for attached binaries: "
f"{attachment_binary_filenames!r}"
)
# -------------------------------------------------------------------------
# Make message
# -------------------------------------------------------------------------
msg = email.mime.multipart.MIMEMultipart()
# Headers: mandatory
msg["From"] = from_addr
msg["Date"] = date
msg["Subject"] = subject
# Headers: optional
if sender:
msg["Sender"] = sender # Single only, not a list
if reply_to:
msg["Reply-To"] = COMMASPACE.join(reply_to)
if to:
msg["To"] = COMMASPACE.join(to)
if cc:
msg["Cc"] = COMMASPACE.join(cc)
if bcc:
msg["Bcc"] = COMMASPACE.join(bcc)
# Body
if content_type == CONTENT_TYPE_TEXT:
msgbody = email.mime.text.MIMEText(body, "plain", charset)
elif content_type == CONTENT_TYPE_HTML:
msgbody = email.mime.text.MIMEText(body, "html", charset)
else:
raise ValueError("unknown content_type")
msg.attach(msgbody)
# Attachments
# noinspection PyPep8,PyBroadException
try:
if attachment_filenames:
# -----------------------------------------------------------------
# Attach things by filename
# -----------------------------------------------------------------
if verbose:
log.debug("attachment_filenames: {}", attachment_filenames)
# noinspection PyTypeChecker
for f in attachment_filenames:
part = email.mime.base.MIMEBase("application", "octet-stream")
part.set_payload(open(f, "rb").read())
email.encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
'attachment; filename="%s"' % os.path.basename(f),
)
msg.attach(part)
if attachment_binaries:
# -----------------------------------------------------------------
# Binary attachments, which have a notional filename
# -----------------------------------------------------------------
if verbose:
log.debug(
"attachment_binary_filenames: {}",
attachment_binary_filenames,
)
for i in range(len(attachment_binaries)):
blob = attachment_binaries[i]
filename = attachment_binary_filenames[i]
part = email.mime.base.MIMEBase("application", "octet-stream")
part.set_payload(blob)
email.encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
'attachment; filename="%s"' % filename,
)
msg.attach(part)
except Exception as e:
raise ValueError(f"send_email: Failed to attach files: {e}")
return msg
# =============================================================================
# Send message
# =============================================================================
def send_msg(
from_addr: str,
to_addrs: Union[str, List[str]],
host: str,
user: str,
password: str,
port: int = None,
use_tls: bool = True,
msg: email.mime.multipart.MIMEMultipart = None,
msg_string: str = None,
) -> None:
"""
Sends a pre-built e-mail message.
Args:
from_addr: e-mail address for 'From:' field
to_addrs: address or list of addresses to transmit to
host: mail server host
user: username on mail server
password: password for username on mail server
port: port to use, or ``None`` for protocol default
use_tls: use TLS, rather than plain SMTP?
msg: a :class:`email.mime.multipart.MIMEMultipart`
msg_string: alternative: specify the message as a raw string
Raises:
:exc:`RuntimeError`
See also:
- https://tools.ietf.org/html/rfc3207
"""
assert bool(msg) != bool(msg_string), "Specify either msg or msg_string"
# Connect
try:
session = smtplib.SMTP(host, port)
except OSError as e:
# https://bugs.python.org/issue2118
# Not all errors from smtplib are raised as SMTPException
# e.g. ConnectionRefusedError when creating the socket
# SMTPException is a subclass of OSError since 3.4
raise RuntimeError(
f"send_msg: Failed to connect to host {host}, port {port}: {e}"
)
try:
session.ehlo()
except smtplib.SMTPException as e:
raise RuntimeError(f"send_msg: Failed to issue EHLO: {e}")
if use_tls:
try:
session.starttls()
session.ehlo()
except smtplib.SMTPException as e:
raise RuntimeError(f"send_msg: Failed to initiate TLS: {e}")
# Log in
if user:
try:
session.login(user, password)
except smtplib.SMTPException as e:
raise RuntimeError(
f"send_msg: Failed to login as user {user}: {e}"
)
else:
log.debug("Not using SMTP AUTH; no user specified")
# For systems with... lax... security requirements
# Send
try:
session.sendmail(from_addr, to_addrs, msg.as_string())
except smtplib.SMTPException as e:
raise RuntimeError(f"send_msg: Failed to send e-mail: {e}")
# Log out
session.quit()
# =============================================================================
# Send e-mail
# =============================================================================
def send_email(
from_addr: str,
host: str,
user: str,
password: str,
port: int = None,
use_tls: bool = True,
date: str = None,
sender: str = "",
reply_to: Union[str, List[str]] = "",
to: Union[str, List[str]] = "",
cc: Union[str, List[str]] = "",
bcc: Union[str, List[str]] = "",
subject: str = "",
body: str = "",
content_type: str = CONTENT_TYPE_TEXT,
charset: str = UTF8,
attachment_filenames: Sequence[str] = None,
attachment_binaries: Sequence[bytes] = None,
attachment_binary_filenames: Sequence[str] = None,
verbose: bool = False,
) -> Tuple[bool, str]:
"""
Sends an e-mail in text/html format using SMTP via TLS.
Args:
host:
mail server host
user:
username on mail server
password:
password for username on mail server
port:
port to use, or ``None`` for protocol default
use_tls:
use TLS, rather than plain SMTP?
date:
e-mail date in RFC 2822 format, or ``None`` for "now"
from_addr:
name of the sender for the "From:" field
sender:
name of the sender for the "Sender:" field
reply_to:
name of the sender for the "Reply-To:" field
to:
e-mail address(es) of the recipients for "To:" field
cc:
e-mail address(es) of the recipients for "Cc:" field
bcc:
e-mail address(es) of the recipients for "Bcc:" field
subject:
e-mail subject
body:
e-mail body
content_type:
MIME type for body content, default ``text/plain``
charset:
character set for body; default ``utf8``
attachment_filenames:
filenames of attachments to add
attachment_binaries:
binary objects to add as attachments
attachment_binary_filenames:
filenames corresponding to ``attachment_binaries``
verbose:
be verbose?
Returns:
tuple: ``(success, error_or_success_message)``
See
- https://tools.ietf.org/html/rfc2822
- https://tools.ietf.org/html/rfc5322
- https://segfault.in/2010/12/sending-gmail-from-python/
- https://stackoverflow.com/questions/64505
- https://stackoverflow.com/questions/3362600
Re security:
- TLS supersedes SSL:
https://en.wikipedia.org/wiki/Transport_Layer_Security
- https://en.wikipedia.org/wiki/Email_encryption
- SMTP connections on ports 25 and 587 are commonly secured via TLS using
the ``STARTTLS`` command:
https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
- https://tools.ietf.org/html/rfc8314
- "STARTTLS on port 587" is one common method. Django refers to this as
"explicit TLS" (its ``E_MAIL_USE_TLS`` setting; see
https://docs.djangoproject.com/en/2.1/ref/settings/#std:setting-EMAIL_USE_TLS).
- Port 465 is also used for "implicit TLS" (3.3 in
https://tools.ietf.org/html/rfc8314). Django refers to this as "implicit
TLS" too, or SSL; see its ``EMAIL_USE_SSL`` setting at
https://docs.djangoproject.com/en/2.1/ref/settings/#email-use-ssl). We
don't support that here.
"""
if isinstance(to, str):
to = [to]
if isinstance(cc, str):
cc = [cc]
if isinstance(bcc, str):
bcc = [bcc]
# -------------------------------------------------------------------------
# Make it
# -------------------------------------------------------------------------
try:
msg = make_email(
from_addr=from_addr,
date=date,
sender=sender,
reply_to=reply_to,
to=to,
cc=cc,
bcc=bcc,
subject=subject,
body=body,
content_type=content_type,
charset=charset,
attachment_filenames=attachment_filenames,
attachment_binaries=attachment_binaries,
attachment_binary_filenames=attachment_binary_filenames,
verbose=verbose,
)
except (AssertionError, ValueError) as e:
errmsg = str(e)
log.error("{}", errmsg)
return False, errmsg
# -------------------------------------------------------------------------
# Send it
# -------------------------------------------------------------------------
to_addrs = to + cc + bcc
try:
send_msg(
msg=msg,
from_addr=from_addr,
to_addrs=to_addrs,
host=host,
user=user,
password=password,
port=port,
use_tls=use_tls,
)
except RuntimeError as e:
errmsg = str(e)
log.error("{}", e)
return False, errmsg
return True, "Success"
# =============================================================================
# Misc
# =============================================================================
_SIMPLE_EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")
def is_email_valid(email_: str) -> bool:
"""
Performs some basic checks that a string appears to be an e-mail address.
See
https://stackoverflow.com/questions/8022530/how-to-check-for-valid-email-address.
"""
# Very basic checks!
if not email_:
return False
if _SIMPLE_EMAIL_REGEX.match(email_) is None:
return False
# The other things that can get through:
# Exclude e.g. two @ symbols
if email_.count("@") != 1:
return False
# Commas are not allowed
if email_.count(",") != 0:
return False
# Can't end in a full stop:
if email_.endswith("."):
return False
return True
def get_email_domain(email_: str) -> str:
"""
Returns the domain part of an e-mail address.
"""
return email_.split("@")[1]
# =============================================================================
# Parse command line
# =============================================================================
def main() -> NoReturn:
"""
Command-line processor. See ``--help`` for details.
"""
logging.basicConfig()
log.setLevel(logging.DEBUG)
parser = argparse.ArgumentParser(
description="Send an e-mail from the command line."
)
parser.add_argument(
"sender", action="store", help="Sender's e-mail address"
)
parser.add_argument("host", action="store", help="SMTP server hostname")
parser.add_argument("user", action="store", help="SMTP username")
parser.add_argument("password", action="store", help="SMTP password")
parser.add_argument(
"recipient", action="append", help="Recipient e-mail address(es)"
)
parser.add_argument("subject", action="store", help="Message subject")
parser.add_argument("body", action="store", help="Message body")
parser.add_argument("--attach", nargs="*", help="Filename(s) to attach")
parser.add_argument(
"--tls", action="store_false", help="Use TLS connection security"
)
parser.add_argument("--verbose", action="store_true", help="Be verbose")
parser.add_argument("-h --help", action="help", help="Prints this help")
args = parser.parse_args()
(result, msg) = send_email(
from_addr=args.sender,
to=args.recipient,
subject=args.subject,
body=args.body,
host=args.host,
user=args.user,
password=args.password,
use_tls=args.tls,
attachment_filenames=args.attach,
verbose=args.verbose,
)
if result:
log.info("Success")
else:
log.info("Failure")
# log.error(msg)
sys.exit(0 if result else 1)
if __name__ == "__main__":
main()