annotate MoinMoin/util/mail.py @ 380:f91064250ea7

cfg.mail_smarthost = 'server:port' support imported from: moin--main--1.5--patch-384
author Thomas Waldmann <tw@waldmann-edv.de>
date Mon, 16 Jan 2006 00:46:43 +0000
parents d1333236adba
children 50c7a0cc1791
rev   line source
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 MoinMoin - email helper functions
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
5 @copyright: 2003 by Jürgen Hermann <jh@web.de>
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
7 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
63
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
9 import os, re
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
10 from email.Header import Header
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
11 from MoinMoin import config
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
12
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
13 _transdict = {"AT": "@", "DOT": ".", "DASH": "-"}
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
14
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
15
63
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
16 def encodeAddress(address, charset):
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
17 """ Encode email address to enable non ascii names
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
18
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
19 e.g. '"Jürgen Hermann" <jh@web.de>'. According to the RFC, the name
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
20 part should be encoded, the address should not.
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
21
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
22 @param address: email address, posibly using '"name" <address>' format
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
23 @type address: unicode
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
24 @param charset: sepcifying both the charset and the encoding, e.g
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
25 quoted printble or base64.
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
26 @type charset: email.Charset.Charset instance
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
27 @rtype: string
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
28 @return: encoded address
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
29 """
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
30 composite = re.compile(r'(?P<phrase>.+)(?P<angle_addr>\<.*\>)',
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
31 re.UNICODE)
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
32 match = composite.match(address)
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
33 if match:
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
34 phrase = match.group('phrase').encode(config.charset)
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
35 phrase = str(Header(phrase, charset))
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
36 angle_addr = match.group('angle_addr').encode(config.charset)
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
37 return phrase + angle_addr
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
38 else:
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
39 return address.encode(config.charset)
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
40
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
41
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
42 def sendmail(request, to, subject, text, **kw):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
43 """ Create and send a text/plain message
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
44
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
45 Return a tuple of success or error indicator and message.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
46
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
47 @param request: the request object
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
48 @param to: recipients (list)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
49 @param subject: subject of email (unicode)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
50 @param text: email body text (unicode)
63
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
51 @keyword mail_from: override default mail_from
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
52 @type mail_from: unicode
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
53 @rtype: tuple
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
54 @return: (is_ok, Description of error or OK message)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
55 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
56 import smtplib, socket
56
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
57 from email.Message import Message
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
58 from email.Charset import Charset, QP
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
59 from email.Utils import formatdate, make_msgid
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
60
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
61 _ = request.getText
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
62 cfg = request.cfg
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
63 mail_from = kw.get('mail_from', '') or cfg.mail_from
56
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
64 subject = subject.encode(config.charset)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
65
56
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
66 # Create a text/plain body using CRLF (see RFC2822)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
67 text = text.replace(u'\n', u'\r\n')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
68 text = text.encode(config.charset)
56
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
69
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
70 # Create a message using config.charset and quoted printable
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
71 # encoding, which should be supported better by mail clients.
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
72 # TODO: check if its really works better for major mail clients
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
73 msg = Message()
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
74 charset = Charset(config.charset)
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
75 charset.header_encoding = QP
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
76 charset.body_encoding = QP
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
77 msg.set_charset(charset)
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
78 msg.set_payload(text)
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
79
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
80 # Create message headers
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
81 # Don't expose emails addreses of the other subscribers, instead we
63
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
82 # use the same mail_from, e.g. u"Jürgen Wiki <noreply@mywiki.org>"
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
83 address = encodeAddress(mail_from, charset)
62
3de67d5c26b7 experimental (tested in my setup) encoded From and To mail headers
Nir Soffer <nirs@freeshell.org>
parents: 61
diff changeset
84 msg['From'] = address
3de67d5c26b7 experimental (tested in my setup) encoded From and To mail headers
Nir Soffer <nirs@freeshell.org>
parents: 61
diff changeset
85 msg['To'] = address
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
86 msg['Date'] = formatdate()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
87 msg['Message-ID'] = make_msgid()
56
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
88 msg['Subject'] = Header(subject, charset)
aedb415dc3a4 more readable email notifications using quoted printable encoding
Nir Soffer <nirs@freeshell.org>
parents: 0
diff changeset
89
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
90 if cfg.mail_sendmail:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
91 # Set the BCC. This will be stripped later by sendmail.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
92 msg['BCC'] = ','.join(to)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
93 # Set Return-Path so that it isn't set (generally incorrectly) for us.
63
d1333236adba backward compability with 1.3 mail_from configuration, update example configuration, add tests
Nir Soffer <nirs@freeshell.org>
parents: 62
diff changeset
94 msg['Return-Path'] = address
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
95
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
96 # Send the message
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
97 if not cfg.mail_sendmail:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
98 try:
380
f91064250ea7 cfg.mail_smarthost = 'server:port' support
Thomas Waldmann <tw@waldmann-edv.de>
parents: 63
diff changeset
99 host, port = (cfg.mail_smarthost + ':25').split(':')[:2]
f91064250ea7 cfg.mail_smarthost = 'server:port' support
Thomas Waldmann <tw@waldmann-edv.de>
parents: 63
diff changeset
100 server = smtplib.SMTP(host, int(port))
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
101 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
102 #server.set_debuglevel(1)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
103 if cfg.mail_login:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
104 user, pwd = cfg.mail_login.split()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
105 try: # try to do tls
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
106 server.ehlo()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
107 if server.has_extn('starttls'):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
108 server.starttls()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
109 server.ehlo()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
110 except:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
111 pass
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
112 server.login(user, pwd)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
113 server.sendmail(mail_from, to, msg.as_string())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
114 finally:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
115 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
116 server.quit()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
117 except AttributeError:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
118 # in case the connection failed, SMTP has no "sock" attribute
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
119 pass
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
120 except smtplib.SMTPException, e:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
121 return (0, str(e))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
122 except (os.error, socket.error), e:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
123 return (0, _("Connection to mailserver '%(server)s' failed: %(reason)s") % {
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
124 'server': cfg.mail_smarthost,
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
125 'reason': str(e)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
126 })
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
127 else:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
128 try:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
129 sendmailp = os.popen(cfg.mail_sendmail, "w")
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
130 # msg contains everything we need, so this is a simple write
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
131 sendmailp.write(msg.as_string())
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
132 sendmail_status = sendmailp.close()
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
133 if sendmail_status:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
134 return (0, str(sendmail_status))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
135 except:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
136 return (0, _("Mail not sent"))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
137
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
138 return (1, _("Mail sent OK"))
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
139
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
140
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
141 def decodeSpamSafeEmail(address):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
142 """ Decode obfuscated email address to standard email address
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
143
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
144 Decode a spam-safe email address in `address` by applying the
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
145 following rules:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
146
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
147 Known all-uppercase words and their translation:
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
148 "DOT" -> "."
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
149 "AT" -> "@"
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
150 "DASH" -> "-"
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
151
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
152 Any unknown all-uppercase words simply get stripped.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
153 Use that to make it even harder for spam bots!
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
154
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
155 Blanks (spaces) simply get stripped.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
156
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
157 @param address: obfuscated email address string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
158 @rtype: string
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
159 @return: decoded email address
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
160 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
161 email = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
162
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
163 # words are separated by blanks
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
164 for word in address.split():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
165 # is it all-uppercase?
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
166 if word.isalpha() and word == word.upper():
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
167 # strip unknown CAPS words
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
168 word = _transdict.get(word, '')
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
169 email.append(word)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
170
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
171 # return concatenated parts
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
172 return ''.join(email)
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
173