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