Mercurial > p > roundup > code
annotate roundup/mailer.py @ 6614:e9dc8e526dd0
Remove fix for old mysql bug; try to improve postgresql perf
Supposedly mysql bug that causes
test_mysql.mysqlDBTest.testFilteringSpecialChars to fail was fixed in
8.0.1 in 2017: https://bugs.mysql.com/bug.php?id=74901
Also disable fsync for postgres as we prefer speed to durability of
data when running test suite. Need to find correct path for
postgrsql.conf, so ls command added as well.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 12 Feb 2022 00:50:02 -0500 |
| parents | 087cae2fbcea |
| children | 3cd43c34c095 |
| 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 |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
5 import time, os, socket, smtplib, sys, traceback, logging |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
6 |
|
1981
ce6806a2a72d
add roundup version to mailer headers
Richard Jones <richard@users.sourceforge.net>
parents:
1830
diff
changeset
|
7 from roundup import __version__ |
|
4536
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
8 from roundup.date import get_timezone, Date |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
9 |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
10 from email import charset |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
11 from email.utils import formatdate, specialsre, escapesre |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
12 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
|
13 from email.header import Header |
|
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
14 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
|
15 from email.mime.text import MIMEText |
|
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
16 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
|
17 from email.mime.nonmultipart import MIMENonMultipart |
|
3464
75b4c2c32cf3
fix Date: header generation to be LOCALE-agnostic [SF#1352624]
Richard Jones <richard@users.sourceforge.net>
parents:
2626
diff
changeset
|
18 |
|
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
|
19 from roundup.anypy import email_ |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
20 from roundup.anypy.strings import b2s, s2u |
|
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
|
21 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
22 try: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
23 import gpg, gpg.core |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
24 except ImportError: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
25 gpg = None |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
26 |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
27 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
28 class MessageSendError(RuntimeError): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
29 pass |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
30 |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
31 |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
32 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
|
33 # 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
|
34 # 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
|
35 if not name: |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
36 return address |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
37 try: |
|
5834
b3905faf58ea
Revert that last attempt to fix. Breaks things worse. Reopened
John Rouillard <rouilj@ieee.org>
parents:
5833
diff
changeset
|
38 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
|
39 except UnicodeEncodeError: |
|
4340
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
40 # use Header to encode correctly. |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
41 encname = Header(name, charset=charset).encode() |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
42 |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
43 # the important bits of formataddr() |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
44 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
|
45 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
|
46 |
| 4341 | 47 # now format the header as a string - don't return a Header as anonymous |
| 48 # 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
|
49 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
|
50 |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
51 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
52 class Mailer: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
53 """Roundup-specific mail sending.""" |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
54 def __init__(self, config): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
55 self.config = config |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
56 self.logger = logging.getLogger('roundup.mailer') |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
57 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
58 # 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
|
59 # 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
|
60 self.debug = os.environ.get('SENDMAILDEBUG', '') \ |
|
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
61 or config["MAIL_DEBUG"] |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
62 |
|
3912
82f462d9ad16
use local timezone for mail date header
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3878
diff
changeset
|
63 # 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
|
64 # 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
|
65 # 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
|
66 # 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
|
67 # 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
|
68 # timezone. |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
69 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
|
70 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
|
71 time.tzset() |
|
3912
82f462d9ad16
use local timezone for mail date header
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3878
diff
changeset
|
72 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
73 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
|
74 ''' Add attributes to a standard output message |
|
2059
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
75 "to" - recipients list |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
76 "subject" - Subject |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
77 "author" - (name, address) tuple or None for admin email |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
78 |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
79 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
|
80 config (default UTF-8). |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
81 ''' |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
82 # encode header values if they need to be |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
83 charset = getattr(self.config, 'EMAIL_CHARSET', 'utf-8') |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5379
diff
changeset
|
84 tracker_name = s2u(self.config.TRACKER_NAME) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
85 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
|
86 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
|
87 name = author[0] |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
88 else: |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5379
diff
changeset
|
89 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
|
90 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
|
91 try: |
|
5473
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
92 subject.encode('ascii') |
|
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
93 message['Subject'] = subject |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
94 except UnicodeError: |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
95 message['Subject'] = Header(subject, charset) |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
96 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
|
97 message['From'] = author |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
98 message['Date'] = formatdate(localtime=True) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
99 |
|
3975
48457385bf61
Send a Precedence header in email so autoresponders don't
Richard Jones <richard@users.sourceforge.net>
parents:
3944
diff
changeset
|
100 # 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
|
101 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
|
102 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
103 # 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
|
104 try: |
|
5473
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
105 tracker_name.encode('ascii') |
|
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
106 message['X-Roundup-Name'] = tracker_name |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
107 except UnicodeError: |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
108 message['X-Roundup-Name'] = Header(tracker_name, charset) |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
109 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
110 # and another one to avoid loops |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
111 message['X-Roundup-Loop'] = 'hello' |
|
1981
ce6806a2a72d
add roundup version to mailer headers
Richard Jones <richard@users.sourceforge.net>
parents:
1830
diff
changeset
|
112 # finally, an aid to debugging problems |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
113 message['X-Roundup-Version'] = __version__ |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
114 |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 return message |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
123 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
124 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
|
125 '''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
|
126 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
|
127 ''' |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
128 if multipart: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
129 message = MIMEMultipart() |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
130 else: |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
131 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
|
132 '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
|
133 |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
134 return message |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
135 |
|
1803
ee33ce4987f5
Let standard_message accept a different author.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1799
diff
changeset
|
136 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
|
137 """Send a standard message. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
138 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
139 Arguments: |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
140 - 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
|
141 - subject: the subject as a string. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
142 - content: the body of the message as a string. |
|
2059
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
143 - 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
|
144 |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
145 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
|
146 """ |
|
5828
bd245858c823
Fix ascii decoding error
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5494
diff
changeset
|
147 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
|
148 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
|
149 self.set_message_attributes(message, to, subject, author) |
|
5828
bd245858c823
Fix ascii decoding error
Ralf Schlatterbeck <rsc@runtux.com>
parents:
5494
diff
changeset
|
150 message.set_payload(s2u(content), charset=charset) |
| 4214 | 151 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
|
152 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
153 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
|
154 subject='Failed issue tracker submission', crypt=False): |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
155 """Bounce a message, attaching the failed submission. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
156 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
157 Arguments: |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
158 - 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
|
159 - 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
|
160 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
|
161 ERROR_MESSAGES_TO setting. |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
162 - error: the reason of failure as a string. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
163 - 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
|
164 - 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
|
165 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
|
166 |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
167 """ |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
168 crypt_to = None |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
169 if crypt: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
170 crypt_to = to |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
171 to = None |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
172 # 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
|
173 dispatcher_email = getattr(self.config, "DISPATCHER_EMAIL", |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
174 getattr(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
|
175 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
|
176 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
|
177 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
|
178 crypt = False |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
179 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
|
180 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
|
181 if crypt: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
182 to = [dispatcher_email] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
183 else: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
184 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
|
185 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
186 message = self.get_standard_message(multipart=True) |
|
2124
6deda7ff3b2a
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2123
diff
changeset
|
187 |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
188 # 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
|
189 part = MIMEText('\n'.join(error)) |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
190 message.attach(part) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
191 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
192 # 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
|
193 part = MIMEText(bounced_message.flatten()) |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
194 message.attach(part) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
195 |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
196 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
|
197 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
198 if to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
199 # send |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
200 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
|
201 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
202 self.smtp_send(to, message.as_string()) |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
203 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
|
204 # 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
|
205 # 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
|
206 # 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
|
207 # because of spam) |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
208 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
|
209 pass |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
210 if crypt_to: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
211 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
|
212 cipher = gpg.core.Data() |
|
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
213 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
|
214 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
|
215 keys = [] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
216 adrs = [] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
217 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
|
218 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
|
219 # 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
|
220 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
|
221 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
|
222 adrs.append(adr) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
223 keys.append(k) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
224 ctx.op_keylist_end() |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
225 if not adrs: |
|
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
226 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
|
227 crypt_to) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
228 crypt_to = adrs |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
229 if crypt_to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
230 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
231 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
|
232 cipher.seek(0, 0) |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
233 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
|
234 _subparts=None, |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
235 protocol="application/pgp-encrypted") |
|
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
236 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
|
237 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
|
238 message.attach(part) |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
239 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
|
240 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
|
241 message.attach(part) |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
242 except gpg.GPGMEError: |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
243 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
|
244 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
|
245 crypt_to = None |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
246 if crypt_to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
247 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
|
248 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
249 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
|
250 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
|
251 # ignore on error, see above. |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
252 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
|
253 pass |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
254 |
|
3548
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
255 def exception_message(self): |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
256 '''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
|
257 traceback. |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
258 ''' |
|
6020
aa26a260e81c
flake8 fixes: fix typoed variable; remove unused imports; format fixes
John Rouillard <rouilj@ieee.org>
parents:
5834
diff
changeset
|
259 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
|
260 to = [self.config.ADMIN_EMAIL] |
|
3569
3954fbcefae5
fix incompatibility with python2.3 [SF#1432602]
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3548
diff
changeset
|
261 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
|
262 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
|
263 |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
264 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
|
265 """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
|
266 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
267 Arguments: |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
268 - 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
|
269 - 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
|
270 - 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
|
271 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
|
272 """ |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
273 |
|
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
274 if not sender: |
|
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
275 sender = self.config.ADMIN_EMAIL |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
276 if self.debug: |
|
4536
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
277 # 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
|
278 # 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
|
279 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
|
280 unixfrm = 'From %s %s' % (sender, Date('.').pretty(fmt)) |
|
6491
087cae2fbcea
Handle more ResourceWarning issues.
John Rouillard <rouilj@ieee.org>
parents:
6020
diff
changeset
|
281 debug_fh = open(self.debug, 'a') |
|
087cae2fbcea
Handle more ResourceWarning issues.
John Rouillard <rouilj@ieee.org>
parents:
6020
diff
changeset
|
282 debug_fh.write('%s\nFROM: %s\nTO: %s\n%s\n\n' % |
|
4536
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
283 (unixfrm, sender, |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
284 ', '.join(to), message)) |
|
6491
087cae2fbcea
Handle more ResourceWarning issues.
John Rouillard <rouilj@ieee.org>
parents:
6020
diff
changeset
|
285 debug_fh.close() |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
286 else: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
287 # now try to send the message |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
288 try: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
289 # 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
|
290 # instead of to roundup |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
291 smtp = SMTPConnection(self.config) |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
292 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
|
293 except socket.error as value: |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
294 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
|
295 "mailhost %s" % value) |
|
5379
17edccfe1755
Python 3 preparation: "except" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5248
diff
changeset
|
296 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
|
297 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
|
298 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
299 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
300 class SMTPConnection(smtplib.SMTP): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
301 ''' 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
|
302 ''' |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
303 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
|
304 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
|
305 local_hostname=config['MAIL_LOCAL_HOSTNAME']) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
306 |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
307 # start the TLS if requested |
|
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
308 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
|
309 self.ehlo() |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
310 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
|
311 config["MAIL_TLS_CERTFILE"]) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
312 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
313 # 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
|
314 mailuser = config["MAIL_USERNAME"] |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
315 if mailuser: |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
316 self.login(mailuser, config["MAIL_PASSWORD"]) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
317 |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
318 # vim: set et sts=4 sw=4 : |
