Mercurial > p > roundup > code
annotate roundup/mailer.py @ 5695:3e1b66c4e1e2
Update docs. Correct errors reported by setup.py build_docs. Add rest
interface and link to rest doc to features page. Add link to xmlrpc
doc to features page. Add rest doc to index. Update rest doc,
hopefully clarify confusing use of parameters in patch action
section. Fix code examples in "Adding new rest endpoints" section. Fix
example adding import of exception.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 07 Apr 2019 20:17:52 -0400 |
| parents | b7fa56ced601 |
| children | bd245858c823 |
| 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 |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
5 import time, quopri, os, socket, smtplib, re, sys, traceback, email, 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 |
|
4967
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
11 from email.utils import formatdate, formataddr, 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.message import Message |
|
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
14 from email.header import Header |
|
760ffc0eae5b
Change email package module names to conform v4 (issue2550875)
John Kristensen <john@jerrykan.com>
parents:
4659
diff
changeset
|
15 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
|
16 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
|
17 from email.mime.multipart import MIMEMultipart |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
18 from email.mime.nonmultipart import MIMENonMultipart |
|
3464
75b4c2c32cf3
fix Date: header generation to be LOCALE-agnostic [SF#1352624]
Richard Jones <richard@users.sourceforge.net>
parents:
2626
diff
changeset
|
19 |
|
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
|
20 from roundup.anypy import email_ |
|
5473
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
21 from roundup.anypy.strings import b2s, s2b, 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
|
22 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
23 try: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
24 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
|
25 except ImportError: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
26 gpg = None |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
27 |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
28 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
29 class MessageSendError(RuntimeError): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
30 pass |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
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: |
|
5458
bc2e682e0305
fixed encoding issues in mailer
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
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): |
|
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
45 encname = '"%s"'%escapesre.sub(r'\\\g<0>', encname) |
|
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) | |
|
4340
ac3f80e39d7a
handle quoting/escaping specials after encoding;
Richard Jones <richard@users.sourceforge.net>
parents:
4338
diff
changeset
|
49 return '%s <%s>'%(encname, address) |
|
4338
94ee533613ac
Attempt to generate more human-readable addresses in email
Richard Jones <richard@users.sourceforge.net>
parents:
4313
diff
changeset
|
50 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
51 class Mailer: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
52 """Roundup-specific mail sending.""" |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
53 def __init__(self, config): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
54 self.config = config |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
55 self.logger = logging.getLogger('roundup.mailer') |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
56 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
57 # 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
|
58 # 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
|
59 self.debug = os.environ.get('SENDMAILDEBUG', '') \ |
|
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
60 or config["MAIL_DEBUG"] |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
61 |
|
3912
82f462d9ad16
use local timezone for mail date header
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3878
diff
changeset
|
62 # 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
|
63 # 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
|
64 # 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
|
65 # 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
|
66 # 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
|
67 # timezone. |
|
7c886f83c2ab
Don't try to call time.tzset if it doesn't exist
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3912
diff
changeset
|
68 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
|
69 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
|
70 time.tzset() |
|
3912
82f462d9ad16
use local timezone for mail date header
Justus Pendleton <jpend@users.sourceforge.net>
parents:
3878
diff
changeset
|
71 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
72 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
|
73 ''' Add attributes to a standard output message |
|
2059
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
74 "to" - recipients list |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
75 "subject" - Subject |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
76 "author" - (name, address) tuple or None for admin email |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
77 |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
78 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
|
79 config (default UTF-8). |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
80 ''' |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
81 # encode header values if they need to be |
|
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
82 charset = getattr(self.config, 'EMAIL_CHARSET', 'utf-8') |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5379
diff
changeset
|
83 tracker_name = s2u(self.config.TRACKER_NAME) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
84 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
|
85 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
|
86 name = author[0] |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
87 else: |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5379
diff
changeset
|
88 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
|
89 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
|
90 try: |
|
5473
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
91 subject.encode('ascii') |
|
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
92 message['Subject'] = subject |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
93 except UnicodeError: |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
94 message['Subject'] = Header(subject, charset) |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
95 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
|
96 message['From'] = author |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
97 message['Date'] = formatdate(localtime=True) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
98 |
|
3975
48457385bf61
Send a Precedence header in email so autoresponders don't
Richard Jones <richard@users.sourceforge.net>
parents:
3944
diff
changeset
|
99 # 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
|
100 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
|
101 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
102 # 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
|
103 try: |
|
5473
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
104 tracker_name.encode('ascii') |
|
3afda04c96a1
mailer string encoding fixes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5458
diff
changeset
|
105 message['X-Roundup-Name'] = tracker_name |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
106 except UnicodeError: |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
107 message['X-Roundup-Name'] = Header(tracker_name, charset) |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
108 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
109 # and another one to avoid loops |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
110 message['X-Roundup-Loop'] = 'hello' |
|
1981
ce6806a2a72d
add roundup version to mailer headers
Richard Jones <richard@users.sourceforge.net>
parents:
1830
diff
changeset
|
111 # finally, an aid to debugging problems |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
112 message['X-Roundup-Version'] = __version__ |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
113 |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 return message |
|
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
122 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
123 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
|
124 '''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
|
125 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
|
126 ''' |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
127 if multipart: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
128 message = MIMEMultipart() |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
129 else: |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
130 message = self.get_text_message(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
|
131 |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
132 return message |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
133 |
|
1803
ee33ce4987f5
Let standard_message accept a different author.
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1799
diff
changeset
|
134 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
|
135 """Send a standard message. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
136 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
137 Arguments: |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
138 - 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
|
139 - subject: the subject as a string. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
140 - content: the body of the message as a string. |
|
2059
48600089c73d
email charset fixes
Richard Jones <richard@users.sourceforge.net>
parents:
2034
diff
changeset
|
141 - 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
|
142 |
|
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
143 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
|
144 """ |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
145 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
|
146 self.set_message_attributes(message, to, subject, author) |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
147 message.set_payload(s2u(content)) |
| 4214 | 148 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
|
149 |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
150 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
|
151 subject='Failed issue tracker submission', crypt=False): |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
152 """Bounce a message, attaching the failed submission. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
153 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
154 Arguments: |
|
5493
725266c03eab
updated mailgw to no longer use mimetools based on jerrykan's patch
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5473
diff
changeset
|
155 - 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
|
156 - 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
|
157 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
|
158 ERROR_MESSAGES_TO setting. |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
159 - error: the reason of failure as a string. |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
160 - 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
|
161 - 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
|
162 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
|
163 |
|
1830
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
164 """ |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
165 crypt_to = None |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
166 if crypt: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
167 crypt_to = to |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
168 to = None |
|
2123
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
169 # 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
|
170 dispatcher_email = getattr(self.config, "DISPATCHER_EMAIL", |
|
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
171 getattr(self.config, "ADMIN_EMAIL")) |
|
95da13c1bdbd
have bounce_message do the error_messages_to heavy-lifting
Richard Jones <richard@users.sourceforge.net>
parents:
2059
diff
changeset
|
172 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
|
173 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
|
174 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
|
175 crypt = False |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
176 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
|
177 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
|
178 if crypt: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
179 to = [dispatcher_email] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
180 else: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
181 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
|
182 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
183 message = self.get_standard_message(multipart=True) |
|
2124
6deda7ff3b2a
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2123
diff
changeset
|
184 |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
185 # 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
|
186 part = MIMEText('\n'.join(error)) |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
187 message.attach(part) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
188 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
189 # 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
|
190 part = MIMEText(bounced_message.flatten()) |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
191 message.attach(part) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
192 |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
193 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
|
194 |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
195 if to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
196 # send |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
197 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
|
198 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
199 self.smtp_send(to, message.as_string()) |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
200 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
|
201 # 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
|
202 # 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
|
203 # 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
|
204 # because of spam) |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
205 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
|
206 pass |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
207 if crypt_to: |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
208 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
|
209 cipher = gpg.core.Data() |
|
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
210 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
|
211 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
|
212 keys = [] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
213 adrs = [] |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
214 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
|
215 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
|
216 # 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
|
217 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
|
218 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
|
219 adrs.append(adr) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
220 keys.append(k) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
221 ctx.op_keylist_end() |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
222 if not adrs: |
|
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
223 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
|
224 crypt_to) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
225 crypt_to = adrs |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
226 if crypt_to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
227 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
228 ctx.op_encrypt(keys, 1, plain, cipher) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
229 cipher.seek(0,0) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
230 message=MIMEMultipart('encrypted', boundary=None, |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
231 _subparts=None, protocol="application/pgp-encrypted") |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
232 part=MIMEBase('application', 'pgp-encrypted') |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
233 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
|
234 message.attach(part) |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
235 part=MIMEBase('application', 'octet-stream') |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
236 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
|
237 message.attach(part) |
|
5494
b7fa56ced601
use gpg module instead of pyme module for PGP encryption
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5493
diff
changeset
|
238 except gpg.GPGMEError: |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
239 self.logger.debug("bounce_message: Cannot encrypt to %s", |
|
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
240 str(crypto_to)) |
|
4541
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
241 crypt_to = None |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
242 if crypt_to: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
243 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
|
244 try: |
|
62239a524beb
PGP support is again working (pyme API has changed significantly)...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4536
diff
changeset
|
245 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
|
246 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
|
247 # ignore on error, see above. |
|
5241
092c4522b3bf
Mailer: Improves diagnostic messages for DEBUG.
Bernhard Reiter <bernhard@intevation.de>
parents:
4979
diff
changeset
|
248 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
|
249 pass |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
250 |
|
3548
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
251 def exception_message(self): |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
252 '''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
|
253 traceback. |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
254 ''' |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
255 subject = '%s: %s'%(self.config.TRACKER_NAME, sys.exc_info()[1]) |
|
61d48244e7a8
login may now be for a single session
Richard Jones <richard@users.sourceforge.net>
parents:
3532
diff
changeset
|
256 to = [self.config.ADMIN_EMAIL] |
|
3569
3954fbcefae5
fix incompatibility with python2.3 [SF#1432602]
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3548
diff
changeset
|
257 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
|
258 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
|
259 |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
260 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
|
261 """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
|
262 |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
263 Arguments: |
|
4ac11e7fa11a
Fix mailer bug [SF#817470]...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1803
diff
changeset
|
264 - 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
|
265 - 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
|
266 - 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
|
267 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
|
268 """ |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
269 |
|
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
270 if not sender: |
|
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
271 sender = self.config.ADMIN_EMAIL |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
272 if self.debug: |
|
4536
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
273 # 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
|
274 # 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
|
275 fmt = '%a %b %m %H:%M:%S %Y' |
|
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
276 unixfrm = 'From %s %s' % (sender, Date ('.').pretty (fmt)) |
|
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
277 open(self.debug, 'a').write('%s\nFROM: %s\nTO: %s\n%s\n\n' % |
|
f2e6b303aa8a
When debugging mail (debug = <filename> setting in [mail] section...
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4341
diff
changeset
|
278 (unixfrm, sender, |
|
4092
4b0ddce43d08
migrate from MimeWriter to email
Richard Jones <richard@users.sourceforge.net>
parents:
3975
diff
changeset
|
279 ', '.join(to), message)) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
280 else: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
281 # now try to send the message |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
282 try: |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
283 # 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
|
284 # instead of to roundup |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
285 smtp = SMTPConnection(self.config) |
|
4287
630a20c51345
Allow non-admin email addresses as envelope sender.
Stefan Seefeld <stefan@seefeld.name>
parents:
4236
diff
changeset
|
286 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
|
287 except socket.error as value: |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
288 raise MessageSendError("Error: couldn't send email: " |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
289 "mailhost %s"%value) |
|
5379
17edccfe1755
Python 3 preparation: "except" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5248
diff
changeset
|
290 except smtplib.SMTPException as msg: |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
291 raise MessageSendError("Error: couldn't send email: %s"%msg) |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
292 |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
293 class SMTPConnection(smtplib.SMTP): |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
294 ''' 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
|
295 ''' |
|
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
296 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
|
297 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
|
298 local_hostname=config['MAIL_LOCAL_HOSTNAME']) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
299 |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
300 # start the TLS if requested |
|
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
301 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
|
302 self.ehlo() |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
303 self.starttls(config["MAIL_TLS_KEYFILE"], |
|
3572
a052781093d7
fix typo in SMTP TLS option name: "MAIL_TLS_CERFILE" [SF#1435452]
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3569
diff
changeset
|
304 config["MAIL_TLS_CERTFILE"]) |
|
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 # 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
|
307 mailuser = config["MAIL_USERNAME"] |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
308 if mailuser: |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
309 self.login(mailuser, config["MAIL_PASSWORD"]) |
|
1799
071ea6fc803f
Extracted duplicated mail-sending code...
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
diff
changeset
|
310 |
|
2626
e49e6c7b14fb
fix incompatibilities with new configuration;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2124
diff
changeset
|
311 # vim: set et sts=4 sw=4 : |
