Mercurial > p > roundup > code
annotate roundup/mailer.py @ 7752:b2dbab2b34bc
fix(refactor): multiple fixups using ruff linter; more testing.
Converting to using the ruff linter and its rulesets. Fixed a number
of issues.
admin.py:
sort imports
use immutable tuples as default value markers for parameters where a
None value is valid.
reduced some loops to list comprehensions for performance
used ternary to simplify some if statements
named some variables to make them less magic
(e.g. _default_savepoint_setting = 1000)
fixed some tests for argument counts < 2 becomes != 2 so 3 is an
error.
moved exception handlers outside of loops for performance where
exception handler will abort loop anyway.
renamed variables called 'id' or 'dir' as they shadow builtin
commands.
fix translations of form _("string %s" % value) -> _("string %s") %
value so translation will be looked up with the key before
substitution.
end dicts, tuples with a trailing comma to reduce missing comma
errors if modified
simplified sorted(list(self.setting.keys())) to
sorted(self.setting.keys()) as sorted consumes whole list.
in if conditions put compared variable on left and threshold condition
on right. (no yoda conditions)
multiple noqa: suppression
removed unneeded noqa as lint rulesets are a bit different
do_get - refactor output printing logic: Use fast return if not
special formatting is requested; use isinstance with a tuple
rather than two isinstance calls; cleaned up flow and removed
comments on algorithm as it can be easily read from the code.
do_filter, do_find - refactor output printing logic. Reduce
duplicate code.
do_find - renamed variable 'value' that was set inside a loop. The
loop index variable was also named 'value'.
do_pragma - added hint to use list subcommand if setting was not
found. Replaced condition 'type(x) is bool' with 'isinstance(x,
bool)' for various types.
test_admin.py
added testing for do_list
better test coverage for do_get includes: -S and -d for multilinks,
error case for -d with non-link.
better testing for do_find including all output modes
better testing for do_filter including all output modes
fixed expected output for do_pragma that now includes hint to use
pragma list if setting not found.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Fri, 01 Mar 2024 14:53:18 -0500 |
| parents | 3cd43c34c095 |
| children | d7e79f8eb943 |
| rev | line source |
|---|---|
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1981
diff
changeset
|
1 """Sending Roundup-specific mail over SMTP. |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1981
diff
changeset
|
2 """ |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1981
diff
changeset
|
3 __docformat__ = 'restructuredtext' |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
4 |
| 6963 | 5 import logging |
| 6 import os | |
| 7 import smtplib | |
| 8 import socket | |
| 9 import sys | |
| 10 import time | |
| 11 import traceback | |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
12 |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
13 from email import charset |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
14 from email.charset import Charset |
|
4967
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
15 from email.header import Header |
|
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
16 from email.mime.base import MIMEBase |
|
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
17 from email.mime.multipart import MIMEMultipart |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
18 from email.mime.nonmultipart import MIMENonMultipart |
| 6963 | 19 from email.mime.text import MIMEText |
| 20 from email.utils import formatdate, specialsre, escapesre | |
|
3464
75b4c2c32cf3
fix Date: header generation to be LOCALE-agnostic [SF#1352624]
Richard Jones <richard@users.sourceforge.net>
parents:
2626
diff
changeset
|
21 |
| 6963 | 22 from roundup.anypy import email_ # noqa: F401 defines functions |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
23 from roundup.anypy.strings import b2s, s2u |
| 6963 | 24 from roundup import __version__ |
| 25 from roundup.date import get_timezone, Date | |
|
4979
f1a2bd1dea77
issue2550877: Writing headers with the email module will use continuation_ws = ' ' now for python 2.5 and 2.6 when importing anypy.email_.
Bernhard Reiter <bernhard@intevation.de>
parents:
4967
diff
changeset
|
26 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
27 try: |
| 6963 | 28 import gpg, gpg.core # noqa: E401 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
29 except ImportError: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
30 gpg = None |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
31 |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
32 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
33 class MessageSendError(RuntimeError): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
34 pass |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
35 |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
36 |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
37 def nice_sender_header(name, address, charset): |
|
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
38 # construct an address header so it's as human-readable as possible |
|
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
39 # even in the presence of a non-ASCII name part |
|
4340
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
40 if not name: |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
41 return address |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
42 try: |
|
5834
b3905faf58ea
Revert that last attempt to fix. Breaks things worse. Reopened
John Rouillard <rouilj@ieee.org>
parents:
5833
diff
changeset
|
43 encname = b2s(name.encode('ASCII')) |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
44 except UnicodeEncodeError: |
|
4340
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
45 # use Header to encode correctly. |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
46 encname = Header(name, charset=charset).encode() |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
47 |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
48 # the important bits of formataddr() |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
49 if specialsre.search(encname): |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
50 encname = '"%s"' % escapesre.sub(r'\\\g<0>', encname) |
|
4340
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
51 |
| 4341 | 52 # now format the header as a string - don't return a Header as anonymous |
| 53 # headers play poorly with Messages (eg. won't get wrapped properly) | |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
54 return '%s <%s>' % (encname, address) |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
55 |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
56 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
57 class Mailer: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
58 """Roundup-specific mail sending.""" |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
59 def __init__(self, config): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
60 self.config = config |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
61 self.logger = logging.getLogger('roundup.mailer') |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
62 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
63 # set to indicate to roundup not to actually _send_ email |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
64 # this var must contain a file to write the mail to |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
65 self.debug = os.environ.get('SENDMAILDEBUG', '') \ |
|
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
66 or config["MAIL_DEBUG"] |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
67 |
|
3912
82f462d9ad16
use local timezone for mail date header
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3878
diff
changeset
|
68 # set timezone so that things like formatdate(localtime=True) |
|
82f462d9ad16
use local timezone for mail date header
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3878
diff
changeset
|
69 # use the configured timezone |
|
3944
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
70 # apparently tzset doesn't exist in python under Windows, my bad. |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
71 # my pathetic attempts at googling a Windows-solution failed |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
72 # so if you're on Windows your mail won't use your configured |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
73 # timezone. |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
74 if hasattr(time, 'tzset'): |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
75 os.environ['TZ'] = get_timezone(self.config.TIMEZONE).tzname(None) |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
76 time.tzset() |
|
3912
82f462d9ad16
use local timezone for mail date header
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3878
diff
changeset
|
77 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
78 def set_message_attributes(self, message, to, subject, author=None): |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
79 ''' Add attributes to a standard output message |
|
2059
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
80 "to" - recipients list |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
81 "subject" - Subject |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
82 "author" - (name, address) tuple or None for admin email |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
83 |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
84 Subject and author are encoded using the EMAIL_CHARSET from the |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
85 config (default UTF-8). |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
86 ''' |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
87 # encode header values if they need to be |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
88 charset = getattr(self.config, 'EMAIL_CHARSET', 'utf-8') |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5379
diff
changeset
|
89 tracker_name = s2u(self.config.TRACKER_NAME) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
90 if not author: |
|
4313
7a6c5c117879
Fix handling of non-ascii in realname in the nosy mailer...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4287
diff
changeset
|
91 author = (tracker_name, self.config.ADMIN_EMAIL) |
|
7a6c5c117879
Fix handling of non-ascii in realname in the nosy mailer...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4287
diff
changeset
|
92 name = author[0] |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
93 else: |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5379
diff
changeset
|
94 name = s2u(author[0]) |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
95 author = nice_sender_header(name, author[1], charset) |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
96 try: |
|
5473
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
97 subject.encode('ascii') |
|
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
98 message['Subject'] = subject |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
99 except UnicodeError: |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
100 message['Subject'] = Header(subject, charset) |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
101 message['To'] = ', '.join(to) |
|
4313
7a6c5c117879
Fix handling of non-ascii in realname in the nosy mailer...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4287
diff
changeset
|
102 message['From'] = author |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
103 message['Date'] = formatdate(localtime=True) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
104 |
|
3975
48457385bf61
Send a Precedence header in email so autoresponders don't
Richard Jones <richard@users.sourceforge.net>
parents:
3944
diff
changeset
|
105 # add a Precedence header so autoresponders ignore us |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
106 message['Precedence'] = 'bulk' |
|
3975
48457385bf61
Send a Precedence header in email so autoresponders don't
Richard Jones <richard@users.sourceforge.net>
parents:
3944
diff
changeset
|
107 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
108 # Add a unique Roundup header to help filtering |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
109 try: |
|
5473
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
110 tracker_name.encode('ascii') |
|
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
111 message['X-Roundup-Name'] = tracker_name |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
112 except UnicodeError: |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
113 message['X-Roundup-Name'] = Header(tracker_name, charset) |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
114 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
115 # and another one to avoid loops |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
116 message['X-Roundup-Loop'] = 'hello' |
|
1981
ce6806a2a72d
add roundup version to mailer headers
Richard Jones <richard@users.sourceforge.net>
parents:
1830
diff
changeset
|
117 # finally, an aid to debugging problems |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
118 message['X-Roundup-Version'] = __version__ |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
119 |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
120 def get_text_message(self, _charset='utf-8', _subtype='plain'): |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
121 message = MIMENonMultipart('text', _subtype) |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
122 cs = Charset(_charset) |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
123 if cs.body_encoding == charset.BASE64: |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
124 cs.body_encoding = charset.QP |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
125 message.set_charset(cs) |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
126 del message['Content-Transfer-Encoding'] |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
127 return message |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
128 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
129 def get_standard_message(self, multipart=False): |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
130 '''Form a standard email message from Roundup. |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
131 Returns a Message object. |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
132 ''' |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
133 if multipart: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
134 message = MIMEMultipart() |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
135 else: |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
136 message = self.get_text_message(getattr(self.config, |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
137 'EMAIL_CHARSET', 'utf-8')) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
138 |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
139 return message |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
140 |
|
1803
ee33ce4987f5
Let standard_message accept a different author.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1799
diff
changeset
|
141 def standard_message(self, to, subject, content, author=None): |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
142 """Send a standard message. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
143 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
144 Arguments: |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
145 - to: a list of addresses usable by email.utils.parseaddr(). |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
146 - subject: the subject as a string. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
147 - content: the body of the message as a string. |
|
2059
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
148 - author: the sender as a (name, address) tuple |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
149 |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
150 All strings are assumed to be UTF-8 encoded. |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
151 """ |
|
5828
bd245858c823
Fix ascii decoding error
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5494
diff
changeset
|
152 charset = getattr(self.config, 'EMAIL_CHARSET', 'utf-8') |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
153 message = self.get_standard_message() |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
154 self.set_message_attributes(message, to, subject, author) |
|
5828
bd245858c823
Fix ascii decoding error
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5494
diff
changeset
|
155 message.set_payload(s2u(content), charset=charset) |
| 4214 | 156 self.smtp_send(to, message.as_string()) |
|
1981
ce6806a2a72d
add roundup version to mailer headers
Richard Jones <richard@users.sourceforge.net>
parents:
1830
diff
changeset
|
157 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
158 def bounce_message(self, bounced_message, to, error, |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
159 subject='Failed issue tracker submission', crypt=False): |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
160 """Bounce a message, attaching the failed submission. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
161 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
162 Arguments: |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
163 - bounced_message: an mailgw.RoundupMessage object. |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
164 - to: a list of addresses usable by email.utils.parseaddr(). Might be |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
165 extended or overridden according to the config |
|
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
166 ERROR_MESSAGES_TO setting. |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
167 - error: the reason of failure as a string. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
168 - subject: the subject as a string. |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
169 - crypt: require encryption with pgp for user -- applies only to |
|
4659
eabe86afc6ee
issue2550756: Fix `oder' typo in mailer.Mailer.bounce_message docstring, thanks W. Trevor King
John Kristensen <john@jerrykan.com>
parents:
4541
diff
changeset
|
170 mail sent back to the user, not the dispatcher or admin. |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
171 |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
172 """ |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
173 crypt_to = None |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
174 if crypt: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
175 crypt_to = to |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
176 to = None |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
177 # see whether we should send to the dispatcher or not |
|
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
178 dispatcher_email = getattr(self.config, "DISPATCHER_EMAIL", |
| 6963 | 179 self.config.ADMIN_EMAIL) |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
180 error_messages_to = getattr(self.config, "ERROR_MESSAGES_TO", "user") |
|
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
181 if error_messages_to == "dispatcher": |
|
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
182 to = [dispatcher_email] |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
183 crypt = False |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
184 crypt_to = None |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
185 elif error_messages_to == "both": |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
186 if crypt: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
187 to = [dispatcher_email] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
188 else: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
189 to.append(dispatcher_email) |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
190 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
191 message = self.get_standard_message(multipart=True) |
|
2124
6deda7ff3b2a
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2123
diff
changeset
|
192 |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
193 # add the error text |
|
4215
57dfcc824acc
fix problem with bounce-message if incoming mail has insufficient privilege...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4214
diff
changeset
|
194 part = MIMEText('\n'.join(error)) |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
195 message.attach(part) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
196 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
197 # attach the original message to the returned message |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
198 part = MIMEText(bounced_message.flatten()) |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
199 message.attach(part) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
200 |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
201 self.logger.debug("bounce_message: to=%s, crypt_to=%s", to, crypt_to) |
|
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
202 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
203 if to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
204 # send |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
205 self.set_message_attributes(message, to, subject) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
206 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
207 self.smtp_send(to, message.as_string()) |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
208 except MessageSendError as e: |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
209 # squash mail sending errors when bouncing mail |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
210 # TODO this *could* be better, as we could notify admin of the |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
211 # problem (even though the vast majority of bounce errors are |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
212 # because of spam) |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
213 self.logger.debug("MessageSendError: %s", str(e)) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
214 pass |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
215 if crypt_to: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
216 plain = gpg.core.Data(message.as_string()) |
|
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
217 cipher = gpg.core.Data() |
|
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
218 ctx = gpg.core.Context() |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
219 ctx.set_armor(1) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
220 keys = [] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
221 adrs = [] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
222 for adr in crypt_to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
223 ctx.op_keylist_start(adr, 0) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
224 # only first key per email |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
225 k = ctx.op_keylist_next() |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
226 if k is not None: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
227 adrs.append(adr) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
228 keys.append(k) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
229 ctx.op_keylist_end() |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
230 if not adrs: |
|
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
231 self.logger.debug("bounce_message: no keys found for %s", |
|
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
232 crypt_to) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
233 crypt_to = adrs |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
234 if crypt_to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
235 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
236 ctx.op_encrypt(keys, 1, plain, cipher) |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
237 cipher.seek(0, 0) |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
238 message = MIMEMultipart('encrypted', boundary=None, |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
239 _subparts=None, |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
240 protocol="application/pgp-encrypted") |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
241 part = MIMEBase('application', 'pgp-encrypted') |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
242 part.set_payload("Version: 1\r\n") |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
243 message.attach(part) |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
244 part = MIMEBase('application', 'octet-stream') |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
245 part.set_payload(cipher.read()) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
246 message.attach(part) |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
247 except gpg.GPGMEError: |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
248 self.logger.debug("bounce_message: Cannot encrypt to %s", |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
249 str(crypt_to)) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
250 crypt_to = None |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
251 if crypt_to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
252 self.set_message_attributes(message, crypt_to, subject) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
253 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
254 self.smtp_send(crypt_to, message.as_string()) |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
255 except MessageSendError as e: |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
256 # ignore on error, see above. |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
257 self.logger.debug("MessageSendError: %s", str(e)) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
258 pass |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
259 |
|
3548
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
260 def exception_message(self): |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
261 '''Send a message to the admins with information about the latest |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
262 traceback. |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
263 ''' |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
264 subject = '%s: %s' % (self.config.TRACKER_NAME, sys.exc_info()[1]) |
|
3548
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
265 to = [self.config.ADMIN_EMAIL] |
|
3569
3954fbcefae5
fix incompatibility with python2.3 [SF#1432602]
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3548
diff
changeset
|
266 content = '\n'.join(traceback.format_exception(*sys.exc_info())) |
|
3548
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
267 self.standard_message(to, subject, content) |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
268 |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
269 def smtp_send(self, to, message, sender=None): |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
270 """Send a message over SMTP, using roundup's config. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
271 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
272 Arguments: |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
273 - to: a list of addresses usable by rfc822.parseaddr(). |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
274 - message: a StringIO instance with a full message. |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
275 - sender: if not 'None', the email address to use as the |
|
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
276 envelope sender. If 'None', the admin email is used. |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
277 """ |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
278 |
|
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
279 if not sender: |
|
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
280 sender = self.config.ADMIN_EMAIL |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
281 if self.debug: |
|
4536
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
282 # don't send - just write to a file, use unix from line so |
|
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
283 # that resulting file can be openened in a mailer |
|
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
284 fmt = '%a %b %m %H:%M:%S %Y' |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
285 unixfrm = 'From %s %s' % (sender, Date('.').pretty(fmt)) |
|
6491
087cae2fbcea
Handle more ResourceWarning issues.
John Rouillard <rouilj@ieee.org>
parents:
6020
diff
changeset
|
286 debug_fh = open(self.debug, 'a') |
|
087cae2fbcea
Handle more ResourceWarning issues.
John Rouillard <rouilj@ieee.org>
parents:
6020
diff
changeset
|
287 debug_fh.write('%s\nFROM: %s\nTO: %s\n%s\n\n' % |
| 6963 | 288 (unixfrm, sender, |
| 289 ', '.join(to), message)) | |
|
6491
087cae2fbcea
Handle more ResourceWarning issues.
John Rouillard <rouilj@ieee.org>
parents:
6020
diff
changeset
|
290 debug_fh.close() |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
291 else: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
292 # now try to send the message |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
293 try: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
294 # send the message as admin so bounces are sent there |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
295 # instead of to roundup |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
296 smtp = SMTPConnection(self.config) |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
297 smtp.sendmail(sender, to, message) |
|
5248
198b6e810c67
Use Python-3-compatible 'as' syntax for except statements
Eric S. Raymond <esr@thyrsus.com>
parents:
5241
diff
changeset
|
298 except socket.error as value: |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
299 raise MessageSendError("Error: couldn't send email: " |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
300 "mailhost %s" % value) |
|
5379
17edccfe1755
Python 3 preparation: "except" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5248
diff
changeset
|
301 except smtplib.SMTPException as msg: |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
302 raise MessageSendError("Error: couldn't send email: %s" % msg) |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
303 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
304 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
305 class SMTPConnection(smtplib.SMTP): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
306 ''' Open an SMTP connection to the mailhost specified in the config |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
307 ''' |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
308 def __init__(self, config): |
|
3878
6d14a3b4e295
allow admin to specify port and local hostname for SMTP connections
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3667
diff
changeset
|
309 smtplib.SMTP.__init__(self, config.MAILHOST, port=config['MAIL_PORT'], |
|
6d14a3b4e295
allow admin to specify port and local hostname for SMTP connections
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3667
diff
changeset
|
310 local_hostname=config['MAIL_LOCAL_HOSTNAME']) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
311 |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
312 # start the TLS if requested |
|
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
313 if config["MAIL_TLS"]: |
|
4116
391fa482f995
fix TLS handling with some SMTP servers (issues 2484879 and 1912923)
Richard Jones <richard@users.sourceforge.net>
parents:
4114
diff
changeset
|
314 self.ehlo() |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
315 self.starttls(config["MAIL_TLS_KEYFILE"], |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
316 config["MAIL_TLS_CERTFILE"]) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
317 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
318 # ok, now do we also need to log in? |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
319 mailuser = config["MAIL_USERNAME"] |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
320 if mailuser: |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
321 self.login(mailuser, config["MAIL_PASSWORD"]) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
322 |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
323 # vim: set et sts=4 sw=4 : |
