Show More
@@ -1,190 +1,206 | |||
|
1 | 1 | # mail.py - mail sending bits for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | from i18n import _ |
|
9 | 9 | import util, encoding |
|
10 | 10 | import os, smtplib, socket, quopri |
|
11 | 11 | import email.Header, email.MIMEText, email.Utils |
|
12 | 12 | |
|
13 | 13 | def _smtp(ui): |
|
14 | 14 | '''build an smtp connection and return a function to send mail''' |
|
15 | 15 | local_hostname = ui.config('smtp', 'local_hostname') |
|
16 | 16 | s = smtplib.SMTP(local_hostname=local_hostname) |
|
17 | 17 | mailhost = ui.config('smtp', 'host') |
|
18 | 18 | if not mailhost: |
|
19 | 19 | raise util.Abort(_('no [smtp]host in hgrc - cannot send mail')) |
|
20 | 20 | mailport = int(ui.config('smtp', 'port', 25)) |
|
21 | 21 | ui.note(_('sending mail: smtp host %s, port %s\n') % |
|
22 | 22 | (mailhost, mailport)) |
|
23 | 23 | s.connect(host=mailhost, port=mailport) |
|
24 | 24 | if ui.configbool('smtp', 'tls'): |
|
25 | 25 | if not hasattr(socket, 'ssl'): |
|
26 | 26 | raise util.Abort(_("can't use TLS: Python SSL support " |
|
27 | 27 | "not installed")) |
|
28 | 28 | ui.note(_('(using tls)\n')) |
|
29 | 29 | s.ehlo() |
|
30 | 30 | s.starttls() |
|
31 | 31 | s.ehlo() |
|
32 | 32 | username = ui.config('smtp', 'username') |
|
33 | 33 | password = ui.config('smtp', 'password') |
|
34 | 34 | if username and not password: |
|
35 | 35 | password = ui.getpass() |
|
36 | 36 | if username and password: |
|
37 | 37 | ui.note(_('(authenticating to mail server as %s)\n') % |
|
38 | 38 | (username)) |
|
39 | 39 | try: |
|
40 | 40 | s.login(username, password) |
|
41 | 41 | except smtplib.SMTPException, inst: |
|
42 | 42 | raise util.Abort(inst) |
|
43 | 43 | |
|
44 | 44 | def send(sender, recipients, msg): |
|
45 | 45 | try: |
|
46 | 46 | return s.sendmail(sender, recipients, msg) |
|
47 | 47 | except smtplib.SMTPRecipientsRefused, inst: |
|
48 | 48 | recipients = [r[1] for r in inst.recipients.values()] |
|
49 | 49 | raise util.Abort('\n' + '\n'.join(recipients)) |
|
50 | 50 | except smtplib.SMTPException, inst: |
|
51 | 51 | raise util.Abort(inst) |
|
52 | 52 | |
|
53 | 53 | return send |
|
54 | 54 | |
|
55 | 55 | def _sendmail(ui, sender, recipients, msg): |
|
56 | 56 | '''send mail using sendmail.''' |
|
57 | 57 | program = ui.config('email', 'method') |
|
58 | 58 | cmdline = '%s -f %s %s' % (program, util.email(sender), |
|
59 | 59 | ' '.join(map(util.email, recipients))) |
|
60 | 60 | ui.note(_('sending mail: %s\n') % cmdline) |
|
61 | 61 | fp = util.popen(cmdline, 'w') |
|
62 | 62 | fp.write(msg) |
|
63 | 63 | ret = fp.close() |
|
64 | 64 | if ret: |
|
65 | 65 | raise util.Abort('%s %s' % ( |
|
66 | 66 | os.path.basename(program.split(None, 1)[0]), |
|
67 | 67 | util.explain_exit(ret)[0])) |
|
68 | 68 | |
|
69 | 69 | def connect(ui): |
|
70 | 70 | '''make a mail connection. return a function to send mail. |
|
71 | 71 | call as sendmail(sender, list-of-recipients, msg).''' |
|
72 | 72 | if ui.config('email', 'method', 'smtp') == 'smtp': |
|
73 | 73 | return _smtp(ui) |
|
74 | 74 | return lambda s, r, m: _sendmail(ui, s, r, m) |
|
75 | 75 | |
|
76 | 76 | def sendmail(ui, sender, recipients, msg): |
|
77 | 77 | send = connect(ui) |
|
78 | 78 | return send(sender, recipients, msg) |
|
79 | 79 | |
|
80 | 80 | def validateconfig(ui): |
|
81 | 81 | '''determine if we have enough config data to try sending email.''' |
|
82 | 82 | method = ui.config('email', 'method', 'smtp') |
|
83 | 83 | if method == 'smtp': |
|
84 | 84 | if not ui.config('smtp', 'host'): |
|
85 | 85 | raise util.Abort(_('smtp specified as email transport, ' |
|
86 | 86 | 'but no smtp host configured')) |
|
87 | 87 | else: |
|
88 | 88 | if not util.find_exe(method): |
|
89 | 89 | raise util.Abort(_('%r specified as email transport, ' |
|
90 | 90 | 'but not in PATH') % method) |
|
91 | 91 | |
|
92 | 92 | def mimetextpatch(s, subtype='plain', display=False): |
|
93 | 93 | '''If patch in utf-8 transfer-encode it.''' |
|
94 | 94 | |
|
95 | 95 | enc = None |
|
96 | 96 | for line in s.splitlines(): |
|
97 | 97 | if len(line) > 950: |
|
98 | 98 | s = quopri.encodestring(s) |
|
99 | 99 | enc = "quoted-printable" |
|
100 | 100 | break |
|
101 | 101 | |
|
102 | 102 | cs = 'us-ascii' |
|
103 | 103 | if not display: |
|
104 | 104 | try: |
|
105 | 105 | s.decode('us-ascii') |
|
106 | 106 | except UnicodeDecodeError: |
|
107 | 107 | try: |
|
108 | 108 | s.decode('utf-8') |
|
109 | 109 | cs = 'utf-8' |
|
110 | 110 | except UnicodeDecodeError: |
|
111 | 111 | # We'll go with us-ascii as a fallback. |
|
112 | 112 | pass |
|
113 | 113 | |
|
114 | 114 | msg = email.MIMEText.MIMEText(s, subtype, cs) |
|
115 | 115 | if enc: |
|
116 | 116 | del msg['Content-Transfer-Encoding'] |
|
117 | 117 | msg['Content-Transfer-Encoding'] = enc |
|
118 | 118 | return msg |
|
119 | 119 | |
|
120 | 120 | def _charsets(ui): |
|
121 | 121 | '''Obtains charsets to send mail parts not containing patches.''' |
|
122 | 122 | charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')] |
|
123 | 123 | fallbacks = [encoding.fallbackencoding.lower(), |
|
124 | 124 | encoding.encoding.lower(), 'utf-8'] |
|
125 | 125 | for cs in fallbacks: # find unique charsets while keeping order |
|
126 | 126 | if cs not in charsets: |
|
127 | 127 | charsets.append(cs) |
|
128 | 128 | return [cs for cs in charsets if not cs.endswith('ascii')] |
|
129 | 129 | |
|
130 | 130 | def _encode(ui, s, charsets): |
|
131 | 131 | '''Returns (converted) string, charset tuple. |
|
132 | 132 | Finds out best charset by cycling through sendcharsets in descending |
|
133 | 133 | order. Tries both encoding and fallbackencoding for input. Only as |
|
134 | 134 | last resort send as is in fake ascii. |
|
135 | 135 | Caveat: Do not use for mail parts containing patches!''' |
|
136 | 136 | try: |
|
137 | 137 | s.decode('ascii') |
|
138 | 138 | except UnicodeDecodeError: |
|
139 | 139 | sendcharsets = charsets or _charsets(ui) |
|
140 | 140 | for ics in (encoding.encoding, encoding.fallbackencoding): |
|
141 | 141 | try: |
|
142 | 142 | u = s.decode(ics) |
|
143 | 143 | except UnicodeDecodeError: |
|
144 | 144 | continue |
|
145 | 145 | for ocs in sendcharsets: |
|
146 | 146 | try: |
|
147 | 147 | return u.encode(ocs), ocs |
|
148 | 148 | except UnicodeEncodeError: |
|
149 | 149 | pass |
|
150 | 150 | except LookupError: |
|
151 | 151 | ui.warn(_('ignoring invalid sendcharset: %s\n') % ocs) |
|
152 | 152 | # if ascii, or all conversion attempts fail, send (broken) ascii |
|
153 | 153 | return s, 'us-ascii' |
|
154 | 154 | |
|
155 | 155 | def headencode(ui, s, charsets=None, display=False): |
|
156 | 156 | '''Returns RFC-2047 compliant header from given string.''' |
|
157 | 157 | if not display: |
|
158 | 158 | # split into words? |
|
159 | 159 | s, cs = _encode(ui, s, charsets) |
|
160 | 160 | return str(email.Header.Header(s, cs)) |
|
161 | 161 | return s |
|
162 | 162 | |
|
163 |
def addressencode(ui, addr |
|
|
164 | '''Turns address into RFC-2047 compliant header.''' | |
|
165 | if display or not address: | |
|
166 | return address or '' | |
|
167 | name, addr = email.Utils.parseaddr(address) | |
|
163 | def _addressencode(ui, name, addr, charsets=None): | |
|
168 | 164 | name = headencode(ui, name, charsets) |
|
169 | 165 | try: |
|
170 | 166 | acc, dom = addr.split('@') |
|
171 | 167 | acc = acc.encode('ascii') |
|
172 | 168 | dom = dom.decode(encoding.encoding).encode('idna') |
|
173 | 169 | addr = '%s@%s' % (acc, dom) |
|
174 | 170 | except UnicodeDecodeError: |
|
175 | 171 | raise util.Abort(_('invalid email address: %s') % addr) |
|
176 | 172 | except ValueError: |
|
177 | 173 | try: |
|
178 | 174 | # too strict? |
|
179 | 175 | addr = addr.encode('ascii') |
|
180 | 176 | except UnicodeDecodeError: |
|
181 | 177 | raise util.Abort(_('invalid local address: %s') % addr) |
|
182 | 178 | return email.Utils.formataddr((name, addr)) |
|
183 | 179 | |
|
180 | def addressencode(ui, address, charsets=None, display=False): | |
|
181 | '''Turns address into RFC-2047 compliant header.''' | |
|
182 | if display or not address: | |
|
183 | return address or '' | |
|
184 | name, addr = email.Utils.parseaddr(address) | |
|
185 | return _addressencode(ui, name, addr, charsets) | |
|
186 | ||
|
187 | def addrlistencode(ui, addrs, charsets=None, display=False): | |
|
188 | '''Turns a list of addresses into a list of RFC-2047 compliant headers. | |
|
189 | A single element of input list may contain multiple addresses, but output | |
|
190 | always has one address per item''' | |
|
191 | if display: | |
|
192 | return [a.strip() for a in addrs if a.strip()] | |
|
193 | ||
|
194 | result = [] | |
|
195 | for name, addr in email.Utils.getaddresses(addrs): | |
|
196 | if name or addr: | |
|
197 | result.append(_addressencode(ui, name, addr, charsets)) | |
|
198 | return result | |
|
199 | ||
|
184 | 200 | def mimeencode(ui, s, charsets=None, display=False): |
|
185 | 201 | '''creates mime text object, encodes it if needed, and sets |
|
186 | 202 | charset and transfer-encoding accordingly.''' |
|
187 | 203 | cs = 'us-ascii' |
|
188 | 204 | if not display: |
|
189 | 205 | s, cs = _encode(ui, s, charsets) |
|
190 | 206 | return email.MIMEText.MIMEText(s, 'plain', cs) |
General Comments 0
You need to be logged in to leave comments.
Login now