Commit | Line | Data |
---|---|---|
092af6d8 | 1 | ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail |
24975917 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1995-1996, 2001-2011 Free Software Foundation, Inc. |
24975917 RS |
4 | |
5 | ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp> | |
9056f1c9 RS |
6 | ;; Maintainer: Simon Josefsson <simon@josefsson.org> |
7 | ;; w32 Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu> | |
c50d5ce0 | 8 | ;; ESMTP support: Simon Leinen <simon@switch.ch> |
2b5c7e03 GM |
9 | ;; Hacked by Mike Taylor, 11th October 1999 to add support for |
10 | ;; automatically appending a domain to RCPT TO: addresses. | |
9056f1c9 | 11 | ;; AUTH=LOGIN support: Stephen Cranefield <scranefield@infoscience.otago.ac.nz> |
24975917 RS |
12 | ;; Keywords: mail |
13 | ||
14 | ;; This file is part of GNU Emacs. | |
15 | ||
b1fc2b50 | 16 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
24975917 | 17 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
18 | ;; the Free Software Foundation, either version 3 of the License, or |
19 | ;; (at your option) any later version. | |
24975917 RS |
20 | |
21 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
22 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | ;; GNU General Public License for more details. | |
25 | ||
26 | ;; You should have received a copy of the GNU General Public License | |
b1fc2b50 | 27 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
24975917 RS |
28 | |
29 | ;;; Commentary: | |
30 | ||
31 | ;; Send Mail to smtp host from smtpmail temp buffer. | |
24975917 | 32 | |
f38d3514 | 33 | ;; Please add these lines in your .emacs(_emacs) or use customize. |
24975917 | 34 | ;; |
f38d3514 | 35 | ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail' |
08b10dd4 | 36 | ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus |
95f41d9a | 37 | ;;(setq smtpmail-smtp-server "YOUR SMTP HOST") |
24975917 | 38 | ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME") |
e889eabc | 39 | ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME") |
f38d3514 | 40 | ;;(setq smtpmail-debug-info t) ; only to debug problems |
24975917 | 41 | |
75da36cc RW |
42 | ;; To queue mail, set `smtpmail-queue-mail' to t and use |
43 | ;; `smtpmail-send-queued-mail' to send. | |
308bc5d8 | 44 | |
9056f1c9 RS |
45 | ;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>, |
46 | ;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism. | |
47 | ;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html | |
48 | ;; Rewritten by Simon Josefsson to use same credential variable as AUTH | |
49 | ;; support below. | |
50 | ||
51 | ;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP | |
52 | ;; Authentication by the AUTH mechanism. | |
53 | ;; See http://www.ietf.org/rfc/rfc2554.txt | |
54 | ||
24975917 RS |
55 | ;;; Code: |
56 | ||
57 | (require 'sendmail) | |
3e79eb87 | 58 | (require 'auth-source) |
9056f1c9 | 59 | (autoload 'mail-strip-quoted-names "mail-utils") |
21bf0d6c SM |
60 | (autoload 'message-make-date "message") |
61 | (autoload 'message-make-message-id "message") | |
9056f1c9 | 62 | (autoload 'rfc2104-hash "rfc2104") |
b1cd57bc | 63 | (autoload 'password-read "password-cache") |
e64a3841 | 64 | |
24975917 | 65 | ;;; |
00ed33e7 RS |
66 | (defgroup smtpmail nil |
67 | "SMTP protocol for sending mail." | |
68 | :group 'mail) | |
24975917 | 69 | |
00ed33e7 | 70 | |
4906cd3d | 71 | (defcustom smtpmail-default-smtp-server nil |
0fc1e8fe | 72 | "Specify default SMTP server. |
4906cd3d LMI |
73 | This only has effect if you specify it before loading the smtpmail library." |
74 | :type '(choice (const nil) string) | |
75 | :group 'smtpmail) | |
00ed33e7 | 76 | |
a8ba4429 | 77 | (defcustom smtpmail-smtp-server |
e2f7c221 | 78 | (or (getenv "SMTPSERVER") smtpmail-default-smtp-server) |
0fc1e8fe | 79 | "The name of the host running SMTP server." |
00ed33e7 RS |
80 | :type '(choice (const nil) string) |
81 | :group 'smtpmail) | |
24975917 | 82 | |
00ed33e7 | 83 | (defcustom smtpmail-smtp-service 25 |
0fc1e8fe | 84 | "SMTP service port number. |
1429d866 | 85 | The default value would be \"smtp\" or 25." |
9056f1c9 | 86 | :type '(choice (integer :tag "Port") (string :tag "Service")) |
00ed33e7 | 87 | :group 'smtpmail) |
24975917 | 88 | |
f5e3c598 LMI |
89 | (defcustom smtpmail-smtp-user nil |
90 | "User name to use when looking up credentials." | |
91 | :type '(choice (const nil) string) | |
92 | :group 'smtpmail) | |
93 | ||
00ed33e7 | 94 | (defcustom smtpmail-local-domain nil |
0fc1e8fe | 95 | "Local domain name without a host name. |
1429d866 | 96 | If the function `system-name' returns the full internet address, |
00ed33e7 RS |
97 | don't define this value." |
98 | :type '(choice (const nil) string) | |
99 | :group 'smtpmail) | |
100 | ||
95f41d9a LMI |
101 | (defcustom smtpmail-stream-type nil |
102 | "Connection type SMTP connections. | |
56ec5115 LMI |
103 | This may be either nil (possibly upgraded to STARTTLS if |
104 | possible), or `starttls' (refuse to send if STARTTLS isn't | |
105 | available), or `plain' (never use STARTTLS).." | |
95f41d9a LMI |
106 | :version "24.1" |
107 | :group 'smtpmail | |
56ec5115 LMI |
108 | :type '(choice (const :tag "Possibly upgrade to STARTTLS" nil) |
109 | (const :tag "Always use STARTTLS" starttls) | |
110 | (const :tag "Never use STARTTLS" plain))) | |
95f41d9a | 111 | |
e889eabc | 112 | (defcustom smtpmail-sendto-domain nil |
0fc1e8fe | 113 | "Local domain name without a host name. |
e889eabc GM |
114 | This is appended (with an @-sign) to any specified recipients which do |
115 | not include an @-sign, so that each RCPT TO address is fully qualified. | |
116 | \(Some configurations of sendmail require this.) | |
117 | ||
118 | Don't bother to set this unless you have get an error like: | |
95f41d9a | 119 | Sending failed; 501 <someone>: recipient address must contain a domain." |
e889eabc GM |
120 | :type '(choice (const nil) string) |
121 | :group 'smtpmail) | |
122 | ||
00ed33e7 | 123 | (defcustom smtpmail-debug-info nil |
9056f1c9 RS |
124 | "Whether to print info in buffer *trace of SMTP session to <somewhere>*. |
125 | See also `smtpmail-debug-verb' which determines if the SMTP protocol should | |
126 | be verbose as well." | |
127 | :type 'boolean | |
128 | :group 'smtpmail) | |
129 | ||
130 | (defcustom smtpmail-debug-verb nil | |
131 | "Whether this library sends the SMTP VERB command or not. | |
132 | The commands enables verbose information from the SMTP server." | |
00ed33e7 RS |
133 | :type 'boolean |
134 | :group 'smtpmail) | |
135 | ||
0fc1e8fe GM |
136 | (defcustom smtpmail-code-conv-from nil |
137 | "Coding system for encoding outgoing mail. | |
138 | Used for the value of `sendmail-coding-system' when | |
139 | `select-message-coding-system' is called. " | |
140 | :type 'coding-system | |
00ed33e7 | 141 | :group 'smtpmail) |
24975917 | 142 | |
a8ba4429 | 143 | (defcustom smtpmail-queue-mail nil |
0fc1e8fe | 144 | "Non-nil means mail is queued; otherwise it is sent immediately. |
308bc5d8 RS |
145 | If queued, it is stored in the directory `smtpmail-queue-dir' |
146 | and sent with `smtpmail-send-queued-mail'." | |
147 | :type 'boolean | |
148 | :group 'smtpmail) | |
149 | ||
150 | (defcustom smtpmail-queue-dir "~/Mail/queued-mail/" | |
0fc1e8fe | 151 | "Directory where `smtpmail.el' stores queued mail." |
308bc5d8 RS |
152 | :type 'directory |
153 | :group 'smtpmail) | |
154 | ||
1b62b062 | 155 | (defcustom smtpmail-warn-about-unknown-extensions nil |
0fc1e8fe | 156 | "If set, print warnings about unknown SMTP extensions. |
1b62b062 GM |
157 | This is mainly useful for development purposes, to learn about |
158 | new SMTP extensions that might be useful to support." | |
159 | :type 'boolean | |
fa87f673 | 160 | :version "21.1" |
1b62b062 GM |
161 | :group 'smtpmail) |
162 | ||
0fc1e8fe | 163 | (defcustom smtpmail-queue-index-file "index" |
1429d866 | 164 | "File name of queued mail index. |
0fc1e8fe GM |
165 | This is relative to `smtpmail-queue-dir'." |
166 | :type 'string | |
167 | :group 'smtpmail) | |
168 | ||
169 | ;; End of customizable variables. | |
170 | ||
308bc5d8 | 171 | |
fb035bbf RS |
172 | (defvar smtpmail-address-buffer) |
173 | (defvar smtpmail-recipient-address-list) | |
174 | ||
08b10dd4 RS |
175 | (defvar smtpmail-queue-counter 0) |
176 | ||
fb035bbf RS |
177 | ;; Buffer-local variable. |
178 | (defvar smtpmail-read-point) | |
179 | ||
e81c51f0 | 180 | (defconst smtpmail-auth-supported '(cram-md5 plain login) |
9927d250 SJ |
181 | "List of supported SMTP AUTH mechanisms. |
182 | The list is in preference order.") | |
9056f1c9 | 183 | |
850ed7b3 | 184 | (defvar smtpmail-mail-address nil |
2265c623 | 185 | "Value to use for envelope-from address for mail from ambient buffer.") |
850ed7b3 | 186 | |
f38d3514 | 187 | ;;;###autoload |
24975917 RS |
188 | (defun smtpmail-send-it () |
189 | (let ((errbuf (if mail-interactive | |
190 | (generate-new-buffer " smtpmail errors") | |
191 | 0)) | |
192 | (tembuf (generate-new-buffer " smtpmail temp")) | |
193 | (case-fold-search nil) | |
24975917 | 194 | delimline |
95f41d9a | 195 | result |
83af570e | 196 | (mailbuf (current-buffer)) |
ff981226 GM |
197 | ;; Examine this variable now, so that |
198 | ;; local binding in the mail buffer will take effect. | |
2265c623 GM |
199 | (smtpmail-mail-address |
200 | (or (and mail-specify-envelope-from (mail-envelope-from)) | |
201 | user-mail-address)) | |
83af570e KH |
202 | (smtpmail-code-conv-from |
203 | (if enable-multibyte-characters | |
204 | (let ((sendmail-coding-system smtpmail-code-conv-from)) | |
205 | (select-message-coding-system))))) | |
24975917 | 206 | (unwind-protect |
937e6a56 | 207 | (with-current-buffer tembuf |
24975917 | 208 | (erase-buffer) |
75da36cc RW |
209 | ;; Use the same `buffer-file-coding-system' as in the mail |
210 | ;; buffer, otherwise any `write-region' invocations (e.g., in | |
362e23e1 EZ |
211 | ;; mail-do-fcc below) will annoy with asking for a suitable |
212 | ;; encoding. | |
213 | (set-buffer-file-coding-system smtpmail-code-conv-from nil t) | |
24975917 RS |
214 | (insert-buffer-substring mailbuf) |
215 | (goto-char (point-max)) | |
216 | ;; require one newline at the end. | |
217 | (or (= (preceding-char) ?\n) | |
218 | (insert ?\n)) | |
219 | ;; Change header-delimiter to be what sendmail expects. | |
92a3f23d | 220 | (mail-sendmail-undelimit-header) |
24975917 | 221 | (setq delimline (point-marker)) |
75da36cc | 222 | ;; (sendmail-synch-aliases) |
24975917 RS |
223 | (if mail-aliases |
224 | (expand-mail-aliases (point-min) delimline)) | |
225 | (goto-char (point-min)) | |
226 | ;; ignore any blank lines in the header | |
227 | (while (and (re-search-forward "\n\n\n*" delimline t) | |
228 | (< (point) delimline)) | |
229 | (replace-match "\n")) | |
230 | (let ((case-fold-search t)) | |
5feeeae2 RS |
231 | ;; We used to process Resent-... headers here, |
232 | ;; but it was not done properly, and the job | |
75da36cc | 233 | ;; is done correctly in `smtpmail-deduce-address-list'. |
24975917 RS |
234 | ;; Don't send out a blank subject line |
235 | (goto-char (point-min)) | |
5feeeae2 RS |
236 | (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t) |
237 | (replace-match "") | |
238 | ;; This one matches a Subject just before the header delimiter. | |
239 | (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t) | |
240 | (= (match-end 0) delimline)) | |
241 | (replace-match ""))) | |
0e2701ca RS |
242 | ;; Put the "From:" field in unless for some odd reason |
243 | ;; they put one in themselves. | |
244 | (goto-char (point-min)) | |
245 | (if (not (re-search-forward "^From:" delimline t)) | |
850ed7b3 | 246 | (let* ((login smtpmail-mail-address) |
0e2701ca RS |
247 | (fullname (user-full-name))) |
248 | (cond ((eq mail-from-style 'angles) | |
249 | (insert "From: " fullname) | |
250 | (let ((fullname-start (+ (point-min) 6)) | |
251 | (fullname-end (point-marker))) | |
252 | (goto-char fullname-start) | |
253 | ;; Look for a character that cannot appear unquoted | |
254 | ;; according to RFC 822. | |
255 | (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" | |
256 | fullname-end 1) | |
257 | (progn | |
258 | ;; Quote fullname, escaping specials. | |
259 | (goto-char fullname-start) | |
260 | (insert "\"") | |
261 | (while (re-search-forward "[\"\\]" | |
262 | fullname-end 1) | |
263 | (replace-match "\\\\\\&" t)) | |
264 | (insert "\"")))) | |
265 | (insert " <" login ">\n")) | |
266 | ((eq mail-from-style 'parens) | |
267 | (insert "From: " login " (") | |
268 | (let ((fullname-start (point))) | |
269 | (insert fullname) | |
270 | (let ((fullname-end (point-marker))) | |
271 | (goto-char fullname-start) | |
272 | ;; RFC 822 says \ and nonmatching parentheses | |
273 | ;; must be escaped in comments. | |
274 | ;; Escape every instance of ()\ ... | |
275 | (while (re-search-forward "[()\\]" fullname-end 1) | |
276 | (replace-match "\\\\\\&" t)) | |
277 | ;; ... then undo escaping of matching parentheses, | |
278 | ;; including matching nested parentheses. | |
279 | (goto-char fullname-start) | |
a8ba4429 | 280 | (while (re-search-forward |
0e2701ca RS |
281 | "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)" |
282 | fullname-end 1) | |
283 | (replace-match "\\1(\\3)" t) | |
284 | (goto-char fullname-start)))) | |
285 | (insert ")\n")) | |
286 | ((null mail-from-style) | |
287 | (insert "From: " login "\n"))))) | |
21bf0d6c SM |
288 | ;; Insert a `Message-Id:' field if there isn't one yet. |
289 | (goto-char (point-min)) | |
290 | (unless (re-search-forward "^Message-Id:" delimline t) | |
291 | (insert "Message-Id: " (message-make-message-id) "\n")) | |
292 | ;; Insert a `Date:' field if there isn't one yet. | |
293 | (goto-char (point-min)) | |
294 | (unless (re-search-forward "^Date:" delimline t) | |
295 | (insert "Date: " (message-make-date) "\n")) | |
362e23e1 EZ |
296 | ;; Possibly add a MIME header for the current coding system |
297 | (let (charset) | |
298 | (goto-char (point-min)) | |
299 | (and (eq mail-send-nonascii 'mime) | |
300 | (not (re-search-forward "^MIME-version:" delimline t)) | |
301 | (progn (skip-chars-forward "\0-\177") | |
302 | (/= (point) (point-max))) | |
303 | smtpmail-code-conv-from | |
304 | (setq charset | |
305 | (coding-system-get smtpmail-code-conv-from | |
306 | 'mime-charset)) | |
307 | (goto-char delimline) | |
308 | (insert "MIME-version: 1.0\n" | |
309 | "Content-type: text/plain; charset=" | |
310 | (symbol-name charset) | |
311 | "\nContent-Transfer-Encoding: 8bit\n"))) | |
24975917 RS |
312 | ;; Insert an extra newline if we need it to work around |
313 | ;; Sun's bug that swallows newlines. | |
314 | (goto-char (1+ delimline)) | |
315 | (if (eval mail-mailer-swallows-blank-line) | |
316 | (newline)) | |
0e2701ca RS |
317 | ;; Find and handle any FCC fields. |
318 | (goto-char (point-min)) | |
319 | (if (re-search-forward "^FCC:" delimline t) | |
75da36cc | 320 | ;; Force `mail-do-fcc' to use the encoding of the mail |
362e23e1 EZ |
321 | ;; buffer to encode outgoing messages on FCC files. |
322 | (let ((coding-system-for-write smtpmail-code-conv-from)) | |
323 | (mail-do-fcc delimline))) | |
24975917 | 324 | (if mail-interactive |
21bf0d6c | 325 | (with-current-buffer errbuf |
24975917 | 326 | (erase-buffer)))) |
b8d747b9 KH |
327 | ;; Encode the header according to RFC2047. |
328 | (mail-encode-header (point-min) delimline) | |
24975917 | 329 | ;; |
24975917 RS |
330 | (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*")) |
331 | (setq smtpmail-recipient-address-list | |
75da36cc | 332 | (smtpmail-deduce-address-list tembuf (point-min) delimline)) |
24975917 | 333 | (kill-buffer smtpmail-address-buffer) |
a8ba4429 | 334 | |
24975917 | 335 | (smtpmail-do-bcc delimline) |
75da36cc | 336 | ;; Send or queue |
308bc5d8 RS |
337 | (if (not smtpmail-queue-mail) |
338 | (if (not (null smtpmail-recipient-address-list)) | |
95f41d9a LMI |
339 | (when (setq result |
340 | (smtpmail-via-smtp | |
341 | smtpmail-recipient-address-list tembuf)) | |
342 | (error "Sending failed: %s" result)) | |
308bc5d8 | 343 | (error "Sending failed; no recipients")) |
bf6e31bc SJ |
344 | (let* ((file-data |
345 | (expand-file-name | |
346 | (format "%s_%i" | |
b2364eaa | 347 | (format-time-string "%Y-%m-%d_%H:%M:%S") |
bf6e31bc SJ |
348 | (setq smtpmail-queue-counter |
349 | (1+ smtpmail-queue-counter))) | |
350 | smtpmail-queue-dir)) | |
351 | (file-data (convert-standard-filename file-data)) | |
352 | (file-elisp (concat file-data ".el")) | |
308bc5d8 RS |
353 | (buffer-data (create-file-buffer file-data)) |
354 | (buffer-elisp (create-file-buffer file-elisp)) | |
355 | (buffer-scratch "*queue-mail*")) | |
2fbc1118 JB |
356 | (unless (file-exists-p smtpmail-queue-dir) |
357 | (make-directory smtpmail-queue-dir t)) | |
21bf0d6c | 358 | (with-current-buffer buffer-data |
308bc5d8 | 359 | (erase-buffer) |
95e4cc85 EZ |
360 | (set-buffer-file-coding-system |
361 | ;; We will be reading the file with no-conversion in | |
362 | ;; smtpmail-send-queued-mail below, so write it out | |
363 | ;; with Unix EOLs. | |
364 | (coding-system-change-eol-conversion | |
365 | (or smtpmail-code-conv-from 'undecided) | |
366 | 'unix) | |
367 | nil t) | |
b0aea09e | 368 | (insert-buffer-substring tembuf) |
308bc5d8 RS |
369 | (write-file file-data) |
370 | (set-buffer buffer-elisp) | |
371 | (erase-buffer) | |
372 | (insert (concat | |
373 | "(setq smtpmail-recipient-address-list '" | |
374 | (prin1-to-string smtpmail-recipient-address-list) | |
a8ba4429 | 375 | ")\n")) |
308bc5d8 RS |
376 | (write-file file-elisp) |
377 | (set-buffer (generate-new-buffer buffer-scratch)) | |
378 | (insert (concat file-data "\n")) | |
a8ba4429 SS |
379 | (append-to-file (point-min) |
380 | (point-max) | |
0fc1e8fe GM |
381 | (expand-file-name smtpmail-queue-index-file |
382 | smtpmail-queue-dir))) | |
308bc5d8 RS |
383 | (kill-buffer buffer-scratch) |
384 | (kill-buffer buffer-data) | |
385 | (kill-buffer buffer-elisp)))) | |
24975917 RS |
386 | (kill-buffer tembuf) |
387 | (if (bufferp errbuf) | |
388 | (kill-buffer errbuf))))) | |
389 | ||
74f39ce9 | 390 | ;;;###autoload |
308bc5d8 RS |
391 | (defun smtpmail-send-queued-mail () |
392 | "Send mail that was queued as a result of setting `smtpmail-queue-mail'." | |
393 | (interactive) | |
7e3fa9f5 | 394 | (with-temp-buffer |
75da36cc RW |
395 | ;; Get index, get first mail, send it, update index, get second |
396 | ;; mail, send it, etc... | |
0fc1e8fe GM |
397 | (let ((file-msg "") |
398 | (qfile (expand-file-name smtpmail-queue-index-file | |
95f41d9a LMI |
399 | smtpmail-queue-dir)) |
400 | result) | |
0fc1e8fe | 401 | (insert-file-contents qfile) |
88f2c9ad | 402 | (goto-char (point-min)) |
308bc5d8 | 403 | (while (not (eobp)) |
21bf0d6c | 404 | (setq file-msg (buffer-substring (point) (line-end-position))) |
308bc5d8 | 405 | (load file-msg) |
19a0baf7 EZ |
406 | ;; Insert the message literally: it is already encoded as per |
407 | ;; the MIME headers, and code conversions might guess the | |
408 | ;; encoding wrongly. | |
7e3fa9f5 EZ |
409 | (with-temp-buffer |
410 | (let ((coding-system-for-read 'no-conversion)) | |
411 | (insert-file-contents file-msg)) | |
ff981226 GM |
412 | (let ((smtpmail-mail-address |
413 | (or (and mail-specify-envelope-from (mail-envelope-from)) | |
414 | user-mail-address))) | |
415 | (if (not (null smtpmail-recipient-address-list)) | |
95f41d9a LMI |
416 | (when (setq result (smtpmail-via-smtp |
417 | smtpmail-recipient-address-list | |
418 | (current-buffer))) | |
419 | (error "Sending failed: %s" result)) | |
ff981226 | 420 | (error "Sending failed; no recipients")))) |
308bc5d8 RS |
421 | (delete-file file-msg) |
422 | (delete-file (concat file-msg ".el")) | |
95754b9f | 423 | (delete-region (point-at-bol) (point-at-bol 2))) |
0fc1e8fe | 424 | (write-region (point-min) (point-max) qfile)))) |
24975917 | 425 | |
24975917 RS |
426 | (defun smtpmail-fqdn () |
427 | (if smtpmail-local-domain | |
428 | (concat (system-name) "." smtpmail-local-domain) | |
429 | (system-name))) | |
430 | ||
9056f1c9 RS |
431 | (defsubst smtpmail-cred-server (cred) |
432 | (nth 0 cred)) | |
433 | ||
434 | (defsubst smtpmail-cred-port (cred) | |
435 | (nth 1 cred)) | |
436 | ||
437 | (defsubst smtpmail-cred-key (cred) | |
438 | (nth 2 cred)) | |
439 | ||
440 | (defsubst smtpmail-cred-user (cred) | |
441 | (nth 2 cred)) | |
442 | ||
443 | (defsubst smtpmail-cred-cert (cred) | |
444 | (nth 3 cred)) | |
445 | ||
446 | (defsubst smtpmail-cred-passwd (cred) | |
447 | (nth 3 cred)) | |
448 | ||
449 | (defun smtpmail-find-credentials (cred server port) | |
450 | (catch 'done | |
451 | (let ((l cred) el) | |
452 | (while (setq el (pop l)) | |
453 | (when (and (equal server (smtpmail-cred-server el)) | |
454 | (equal port (smtpmail-cred-port el))) | |
455 | (throw 'done el)))))) | |
456 | ||
2b5c7e03 GM |
457 | (defun smtpmail-maybe-append-domain (recipient) |
458 | (if (or (not smtpmail-sendto-domain) | |
459 | (string-match "@" recipient)) | |
460 | recipient | |
461 | (concat recipient "@" smtpmail-sendto-domain))) | |
462 | ||
9056f1c9 RS |
463 | (defun smtpmail-intersection (list1 list2) |
464 | (let ((result nil)) | |
465 | (dolist (el2 list2) | |
466 | (when (memq el2 list1) | |
467 | (push el2 result))) | |
468 | (nreverse result))) | |
469 | ||
75da36cc | 470 | ;; `password-read' autoloads password-cache. |
48a731fe GM |
471 | (declare-function password-cache-add "password-cache" (key password)) |
472 | ||
95f41d9a LMI |
473 | (defun smtpmail-command-or-throw (process string &optional code) |
474 | (let (ret) | |
475 | (smtpmail-send-command process string) | |
476 | (unless (smtpmail-ok-p (setq ret (smtpmail-read-response process)) | |
477 | code) | |
7d36ad46 LMI |
478 | (throw 'done (format "%s in response to %s" |
479 | (smtpmail-response-text ret) | |
480 | string))) | |
95f41d9a LMI |
481 | ret)) |
482 | ||
483 | (defun smtpmail-try-auth-methods (process supported-extensions host port | |
484 | &optional ask-for-password) | |
97bb1093 LMI |
485 | (setq port |
486 | (if port | |
487 | (format "%s" port) | |
488 | "smtp")) | |
9056f1c9 | 489 | (let* ((mechs (cdr-safe (assoc 'auth supported-extensions))) |
9927d250 | 490 | (mech (car (smtpmail-intersection mechs smtpmail-auth-supported))) |
95f41d9a LMI |
491 | (auth-source-creation-prompts |
492 | '((user . "SMTP user at %h: ") | |
493 | (secret . "SMTP password for %u@%h: "))) | |
494 | (auth-info (car | |
ddb7ffee | 495 | (auth-source-search |
ddb7ffee | 496 | :host host |
97bb1093 | 497 | :port port |
f5e3c598 | 498 | :user smtpmail-smtp-user |
40098786 | 499 | :max 1 |
ddb7ffee LMI |
500 | :require (and ask-for-password |
501 | '(:user :secret)) | |
502 | :create ask-for-password))) | |
95f41d9a LMI |
503 | (user (plist-get auth-info :user)) |
504 | (password (plist-get auth-info :secret)) | |
505 | (save-function (and ask-for-password | |
506 | (plist-get auth-info :save-function))) | |
9056f1c9 | 507 | ret) |
97bb1093 LMI |
508 | (when (and user |
509 | (not password)) | |
510 | ;; The user has stored the user name, but not the password, so | |
511 | ;; ask for the password, even if we're not forcing that through | |
512 | ;; `ask-for-password'. | |
513 | (setq auth-info | |
514 | (car | |
515 | (auth-source-search | |
516 | :max 1 | |
517 | :host host | |
518 | :port port | |
f5e3c598 | 519 | :user smtpmail-smtp-user |
97bb1093 LMI |
520 | :require '(:user :secret) |
521 | :create t)) | |
522 | password (plist-get auth-info :secret))) | |
95f41d9a LMI |
523 | (when (functionp password) |
524 | (setq password (funcall password))) | |
525 | (cond | |
526 | ((or (not mech) | |
527 | (not user) | |
528 | (not password)) | |
529 | ;; No mechanism, or no credentials. | |
530 | mech) | |
531 | ((eq mech 'cram-md5) | |
532 | (setq ret (smtpmail-command-or-throw process "AUTH CRAM-MD5")) | |
533 | (when (eq (car ret) 334) | |
534 | (let* ((challenge (substring (cadr ret) 4)) | |
535 | (decoded (base64-decode-string challenge)) | |
536 | (hash (rfc2104-hash 'md5 64 16 password decoded)) | |
537 | (response (concat user " " hash)) | |
538 | ;; Osamu Yamane <yamane@green.ocn.ne.jp>: | |
539 | ;; SMTP auth fails because the SMTP server identifies | |
540 | ;; only the first part of the string (delimited by | |
541 | ;; new line characters) as a response from the | |
542 | ;; client, and the rest as distinct commands. | |
543 | ||
544 | ;; In my case, the response string is 80 characters | |
545 | ;; long. Without the no-line-break option for | |
546 | ;; `base64-encode-string', only the first 76 characters | |
547 | ;; are taken as a response to the server, and the | |
548 | ;; authentication fails. | |
549 | (encoded (base64-encode-string response t))) | |
550 | (smtpmail-command-or-throw process encoded) | |
551 | (when save-function | |
552 | (funcall save-function))))) | |
553 | ((eq mech 'login) | |
554 | (smtpmail-command-or-throw process "AUTH LOGIN") | |
555 | (smtpmail-command-or-throw | |
556 | process (base64-encode-string user t)) | |
557 | (smtpmail-command-or-throw process (base64-encode-string password t)) | |
558 | (when save-function | |
559 | (funcall save-function))) | |
560 | ((eq mech 'plain) | |
561 | ;; We used to send an empty initial request, and wait for an | |
562 | ;; empty response, and then send the password, but this | |
563 | ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this | |
564 | ;; is not sent if the server did not advertise AUTH PLAIN in | |
565 | ;; the EHLO response. See RFC 2554 for more info. | |
566 | (smtpmail-command-or-throw | |
567 | process | |
568 | (concat "AUTH PLAIN " | |
569 | (base64-encode-string (concat "\0" user "\0" password) t)) | |
570 | 235) | |
571 | (when save-function | |
572 | (funcall save-function))) | |
573 | (t | |
574 | (error "Mechanism %s not implemented" mech))))) | |
575 | ||
576 | (defun smtpmail-response-code (string) | |
577 | (when string | |
578 | (with-temp-buffer | |
579 | (insert string) | |
580 | (goto-char (point-min)) | |
581 | (and (re-search-forward "^\\([0-9]+\\) " nil t) | |
582 | (string-to-number (match-string 1)))))) | |
583 | ||
584 | (defun smtpmail-ok-p (response &optional code) | |
585 | (and (car response) | |
586 | (integerp (car response)) | |
587 | (< (car response) 400) | |
588 | (or (null code) | |
589 | (= code (car response))))) | |
590 | ||
591 | (defun smtpmail-response-text (response) | |
592 | (mapconcat 'identity (cdr response) "\n")) | |
593 | ||
594 | (defun smtpmail-query-smtp-server () | |
595 | (let ((server (read-string "Outgoing SMTP mail server: ")) | |
9a70f03d | 596 | (ports '("smtp" 587)) |
95f41d9a LMI |
597 | stream port) |
598 | (when (and smtpmail-smtp-server | |
599 | (not (member smtpmail-smtp-server ports))) | |
600 | (push smtpmail-smtp-server ports)) | |
601 | (while (and (not smtpmail-smtp-server) | |
602 | (setq port (pop ports))) | |
603 | (when (setq stream (ignore-errors | |
604 | (open-network-stream "smtp" nil server port))) | |
9988520a LMI |
605 | (customize-save-variable 'smtpmail-smtp-server server) |
606 | (customize-save-variable 'smtpmail-smtp-service port) | |
95f41d9a LMI |
607 | (delete-process stream))) |
608 | (unless smtpmail-smtp-server | |
609 | (error "Couldn't contact an SMTP server")))) | |
610 | ||
611 | (defun smtpmail-via-smtp (recipient smtpmail-text-buffer | |
612 | &optional ask-for-password) | |
613 | (unless smtpmail-smtp-server | |
614 | (smtpmail-query-smtp-server)) | |
24975917 | 615 | (let ((process nil) |
f38d3514 KH |
616 | (host (or smtpmail-smtp-server |
617 | (error "`smtpmail-smtp-server' not defined"))) | |
8805249b | 618 | (port smtpmail-smtp-service) |
75da36cc | 619 | ;; `smtpmail-mail-address' should be set to the appropriate |
ff981226 GM |
620 | ;; buffer-local value by the caller, but in case not: |
621 | (envelope-from (or smtpmail-mail-address | |
622 | (and mail-specify-envelope-from | |
623 | (mail-envelope-from)) | |
624 | user-mail-address)) | |
24975917 | 625 | response-code |
c50d5ce0 | 626 | process-buffer |
95f41d9a LMI |
627 | result |
628 | auth-mechanisms | |
c50d5ce0 | 629 | (supported-extensions '())) |
24975917 RS |
630 | (unwind-protect |
631 | (catch 'done | |
632 | ;; get or create the trace buffer | |
633 | (setq process-buffer | |
95f41d9a LMI |
634 | (get-buffer-create |
635 | (format "*trace of SMTP session to %s*" host))) | |
24975917 RS |
636 | |
637 | ;; clear the trace buffer of old output | |
21bf0d6c | 638 | (with-current-buffer process-buffer |
9cf328cc | 639 | (setq buffer-undo-list t) |
24975917 RS |
640 | (erase-buffer)) |
641 | ||
642 | ;; open the connection to the server | |
c65c9622 LMI |
643 | (let ((coding-system-for-read 'binary) |
644 | (coding-system-for-write 'binary)) | |
645 | (setq result | |
646 | (open-network-stream | |
647 | "smtpmail" process-buffer host port | |
648 | :type smtpmail-stream-type | |
649 | :return-list t | |
650 | :capability-command (format "EHLO %s\r\n" (smtpmail-fqdn)) | |
651 | :end-of-command "^[0-9]+ .*\r\n" | |
652 | :success "^2.*\n" | |
653 | :always-query-capabilities t | |
654 | :starttls-function | |
655 | (lambda (capabilities) | |
656 | (and (string-match "-STARTTLS" capabilities) | |
657 | "STARTTLS\r\n")) | |
658 | :client-certificate t | |
659 | :use-starttls-if-possible t))) | |
95f41d9a LMI |
660 | |
661 | ;; If we couldn't access the server at all, we give up. | |
662 | (unless (setq process (car result)) | |
468d09d4 LMI |
663 | (throw 'done (if (plist-get (cdr result) :error) |
664 | (plist-get (cdr result) :error) | |
665 | "Unable to contact server"))) | |
24975917 RS |
666 | |
667 | ;; set the send-filter | |
668 | (set-process-filter process 'smtpmail-process-filter) | |
669 | ||
95f41d9a LMI |
670 | (let* ((greeting (plist-get (cdr result) :greeting)) |
671 | (code (smtpmail-response-code greeting))) | |
672 | (unless code | |
673 | (throw 'done (format "No greeting: %s" greeting))) | |
674 | (when (>= code 400) | |
675 | (throw 'done (format "Connection not allowed: %s" greeting)))) | |
676 | ||
21bf0d6c | 677 | (with-current-buffer process-buffer |
4b876894 | 678 | (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix) |
24975917 RS |
679 | (make-local-variable 'smtpmail-read-point) |
680 | (setq smtpmail-read-point (point-min)) | |
681 | ||
95f41d9a LMI |
682 | (let* ((capabilities (plist-get (cdr result) :capabilities)) |
683 | (code (smtpmail-response-code capabilities))) | |
684 | (if (or (null code) | |
685 | (>= code 400)) | |
686 | ;; The server didn't accept EHLO, so we fall back on HELO. | |
687 | (smtpmail-command-or-throw | |
688 | process (format "HELO %s" (smtpmail-fqdn))) | |
689 | ;; EHLO was successful, so we parse the extensions. | |
690 | (dolist (line (delete | |
691 | "" | |
692 | (split-string | |
693 | (plist-get (cdr result) :capabilities) | |
694 | "\r\n"))) | |
695 | (let ((name | |
696 | (with-case-table ascii-case-table | |
697 | (mapcar (lambda (s) (intern (downcase s))) | |
698 | (split-string (substring line 4) "[ ]"))))) | |
699 | (when (= (length name) 1) | |
700 | (setq name (car name))) | |
701 | (when name | |
702 | (cond ((memq (if (consp name) (car name) name) | |
703 | '(verb xvrb 8bitmime onex xone | |
704 | expn size dsn etrn | |
705 | enhancedstatuscodes | |
706 | help xusr | |
707 | auth=login auth starttls)) | |
708 | (setq supported-extensions | |
709 | (cons name supported-extensions))) | |
710 | (smtpmail-warn-about-unknown-extensions | |
711 | (message "Unknown extension %s" name)))))))) | |
712 | ||
713 | (setq auth-mechanisms | |
714 | (smtpmail-try-auth-methods | |
715 | process supported-extensions host port | |
716 | ask-for-password)) | |
717 | ||
718 | (when (or (member 'onex supported-extensions) | |
719 | (member 'xone supported-extensions)) | |
720 | (smtpmail-command-or-throw process (format "ONEX"))) | |
721 | ||
722 | (when (and smtpmail-debug-verb | |
723 | (or (member 'verb supported-extensions) | |
724 | (member 'xvrb supported-extensions))) | |
725 | (smtpmail-command-or-throw process (format "VERB"))) | |
726 | ||
727 | (when (member 'xusr supported-extensions) | |
728 | (smtpmail-command-or-throw process (format "XUSR"))) | |
729 | ||
a8df98fd | 730 | ;; MAIL FROM:<sender> |
c50d5ce0 | 731 | (let ((size-part |
9056f1c9 RS |
732 | (if (or (member 'size supported-extensions) |
733 | (assoc 'size supported-extensions)) | |
c50d5ce0 | 734 | (format " SIZE=%d" |
21bf0d6c | 735 | (with-current-buffer smtpmail-text-buffer |
c50d5ce0 RS |
736 | ;; size estimate: |
737 | (+ (- (point-max) (point-min)) | |
738 | ;; Add one byte for each change-of-line | |
73921ac1 GM |
739 | ;; because of CR-LF representation: |
740 | (count-lines (point-min) (point-max))))) | |
c50d5ce0 RS |
741 | "")) |
742 | (body-part | |
743 | (if (member '8bitmime supported-extensions) | |
744 | ;; FIXME: | |
745 | ;; Code should be added here that transforms | |
746 | ;; the contents of the message buffer into | |
747 | ;; something the receiving SMTP can handle. | |
748 | ;; For a receiver that supports 8BITMIME, this | |
749 | ;; may mean converting BINARY to BASE64, or | |
750 | ;; adding Content-Transfer-Encoding and the | |
751 | ;; other MIME headers. The code should also | |
752 | ;; return an indication of what encoding the | |
753 | ;; message buffer is now, i.e. ASCII or | |
754 | ;; 8BITMIME. | |
755 | (if nil | |
756 | " BODY=8BITMIME" | |
757 | "") | |
758 | ""))) | |
7d36ad46 | 759 | (smtpmail-send-command |
95f41d9a | 760 | process (format "MAIL FROM:<%s>%s%s" |
7d36ad46 LMI |
761 | envelope-from size-part body-part)) |
762 | (cond | |
763 | ((smtpmail-ok-p (setq result (smtpmail-read-response process))) | |
764 | ;; Success. | |
765 | ) | |
766 | ((and auth-mechanisms | |
767 | (not ask-for-password) | |
768 | (= (car result) 530)) | |
769 | ;; We got a "530 auth required", so we close and try | |
770 | ;; again, this time asking the user for a password. | |
771 | (smtpmail-send-command process "QUIT") | |
772 | (smtpmail-read-response process) | |
773 | (delete-process process) | |
396f7c9d | 774 | (setq process nil) |
7d36ad46 LMI |
775 | (throw 'done |
776 | (smtpmail-via-smtp recipient smtpmail-text-buffer t))) | |
777 | (t | |
778 | ;; Return the error code. | |
779 | (throw 'done | |
780 | (smtpmail-response-text result))))) | |
a8ba4429 | 781 | |
a8df98fd | 782 | ;; RCPT TO:<recipient> |
8805249b RS |
783 | (let ((n 0)) |
784 | (while (not (null (nth n recipient))) | |
95f41d9a LMI |
785 | (smtpmail-send-command |
786 | process (format "RCPT TO:<%s>" | |
787 | (smtpmail-maybe-append-domain | |
788 | (nth n recipient)))) | |
789 | (cond | |
790 | ((smtpmail-ok-p (setq result (smtpmail-read-response process))) | |
791 | ;; Success. | |
792 | nil) | |
793 | ((and auth-mechanisms | |
794 | (not ask-for-password) | |
4e190b80 LMI |
795 | (>= (car result) 550) |
796 | (<= (car result) 554)) | |
797 | ;; We got a "550 relay not permitted" (or the like), | |
798 | ;; and the server accepts credentials, so we try | |
799 | ;; again, but ask for a password first. | |
95f41d9a LMI |
800 | (smtpmail-send-command process "QUIT") |
801 | (smtpmail-read-response process) | |
802 | (delete-process process) | |
396f7c9d | 803 | (setq process nil) |
95f41d9a LMI |
804 | (throw 'done |
805 | (smtpmail-via-smtp recipient smtpmail-text-buffer t))) | |
806 | (t | |
807 | ;; Return the error code. | |
808 | (throw 'done | |
809 | (smtpmail-response-text result)))) | |
810 | (setq n (1+ n)))) | |
811 | ||
812 | ;; Send the contents. | |
813 | (smtpmail-command-or-throw process "DATA") | |
24975917 | 814 | (smtpmail-send-data process smtpmail-text-buffer) |
75da36cc | 815 | ;; DATA end "." |
95f41d9a LMI |
816 | (smtpmail-command-or-throw process ".") |
817 | ;; Return success. | |
818 | nil)) | |
819 | (when (and process | |
820 | (buffer-live-p process-buffer)) | |
821 | (with-current-buffer (process-buffer process) | |
822 | (smtpmail-send-command process "QUIT") | |
823 | (smtpmail-read-response process) | |
824 | (delete-process process) | |
825 | (unless smtpmail-debug-info | |
826 | (kill-buffer process-buffer))))))) | |
24975917 RS |
827 | |
828 | ||
24975917 | 829 | (defun smtpmail-process-filter (process output) |
21bf0d6c | 830 | (with-current-buffer (process-buffer process) |
24975917 RS |
831 | (goto-char (point-max)) |
832 | (insert output))) | |
833 | ||
24975917 RS |
834 | (defun smtpmail-read-response (process) |
835 | (let ((case-fold-search nil) | |
c50d5ce0 | 836 | (response-strings nil) |
24975917 | 837 | (response-continue t) |
c50d5ce0 | 838 | (return-value '(nil ())) |
24975917 | 839 | match-end) |
2ceed428 SJ |
840 | (catch 'done |
841 | (while response-continue | |
842 | (goto-char smtpmail-read-point) | |
843 | (while (not (search-forward "\r\n" nil t)) | |
844 | (unless (memq (process-status process) '(open run)) | |
845 | (throw 'done nil)) | |
846 | (accept-process-output process) | |
847 | (goto-char smtpmail-read-point)) | |
848 | ||
849 | (setq match-end (point)) | |
850 | (setq response-strings | |
851 | (cons (buffer-substring smtpmail-read-point (- match-end 2)) | |
852 | response-strings)) | |
853 | ||
854 | (goto-char smtpmail-read-point) | |
855 | (if (looking-at "[0-9]+ ") | |
856 | (let ((begin (match-beginning 0)) | |
857 | (end (match-end 0))) | |
858 | (if smtpmail-debug-info | |
859 | (message "%s" (car response-strings))) | |
860 | ||
861 | (setq smtpmail-read-point match-end) | |
862 | ||
863 | ;; ignore lines that start with "0" | |
864 | (if (looking-at "0[0-9]+ ") | |
865 | nil | |
866 | (setq response-continue nil) | |
867 | (setq return-value | |
027a4b6b | 868 | (cons (string-to-number |
2ceed428 SJ |
869 | (buffer-substring begin end)) |
870 | (nreverse response-strings))))) | |
871 | ||
872 | (if (looking-at "[0-9]+-") | |
873 | (progn (if smtpmail-debug-info | |
874 | (message "%s" (car response-strings))) | |
875 | (setq smtpmail-read-point match-end) | |
876 | (setq response-continue t)) | |
877 | (progn | |
878 | (setq smtpmail-read-point match-end) | |
c50d5ce0 RS |
879 | (setq response-continue nil) |
880 | (setq return-value | |
2ceed428 SJ |
881 | (cons nil (nreverse response-strings))))))) |
882 | (setq smtpmail-read-point match-end)) | |
24975917 RS |
883 | return-value)) |
884 | ||
885 | ||
24975917 RS |
886 | (defun smtpmail-send-command (process command) |
887 | (goto-char (point-max)) | |
888 | (if (= (aref command 0) ?P) | |
889 | (insert "PASS <omitted>\r\n") | |
890 | (insert command "\r\n")) | |
891 | (setq smtpmail-read-point (point)) | |
892 | (process-send-string process command) | |
893 | (process-send-string process "\r\n")) | |
894 | ||
24975917 RS |
895 | (defun smtpmail-send-data-1 (process data) |
896 | (goto-char (point-max)) | |
897 | ||
83af570e KH |
898 | (if (and (multibyte-string-p data) |
899 | smtpmail-code-conv-from) | |
900 | (setq data (string-as-multibyte | |
901 | (encode-coding-string data smtpmail-code-conv-from)))) | |
a8ba4429 | 902 | |
24975917 RS |
903 | (if smtpmail-debug-info |
904 | (insert data "\r\n")) | |
905 | ||
906 | (setq smtpmail-read-point (point)) | |
57810560 KH |
907 | ;; Escape "." at start of a line |
908 | (if (eq (string-to-char data) ?.) | |
24975917 | 909 | (process-send-string process ".")) |
57810560 | 910 | (process-send-string process data) |
75da36cc | 911 | (process-send-string process "\r\n")) |
24975917 RS |
912 | |
913 | (defun smtpmail-send-data (process buffer) | |
1257e755 SM |
914 | (let ((data-continue t) sending-data |
915 | (pr (with-current-buffer buffer | |
916 | (make-progress-reporter "Sending email" | |
917 | (point-min) (point-max))))) | |
21bf0d6c | 918 | (with-current-buffer buffer |
24975917 | 919 | (goto-char (point-min))) |
24975917 | 920 | (while data-continue |
21bf0d6c | 921 | (with-current-buffer buffer |
1257e755 | 922 | (progress-reporter-update pr (point)) |
dd64e5e5 GM |
923 | (setq sending-data (buffer-substring (point-at-bol) (point-at-eol))) |
924 | (end-of-line 2) | |
925 | (setq data-continue (not (eobp)))) | |
1257e755 SM |
926 | (smtpmail-send-data-1 process sending-data)) |
927 | (progress-reporter-done pr))) | |
24975917 RS |
928 | |
929 | (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end) | |
930 | "Get address list suitable for smtp RCPT TO: <address>." | |
1188dd37 | 931 | (unwind-protect |
21bf0d6c SM |
932 | (with-current-buffer smtpmail-address-buffer |
933 | (erase-buffer) | |
75da36cc RW |
934 | (let ((case-fold-search t) |
935 | (simple-address-list "") | |
936 | this-line | |
937 | this-line-end | |
938 | addr-regexp) | |
24975917 RS |
939 | (insert-buffer-substring smtpmail-text-buffer header-start header-end) |
940 | (goto-char (point-min)) | |
13f1d088 KH |
941 | ;; RESENT-* fields should stop processing of regular fields. |
942 | (save-excursion | |
21bf0d6c SM |
943 | (setq addr-regexp |
944 | (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" | |
945 | header-end t) | |
946 | "^Resent-\\(to\\|cc\\|bcc\\):" | |
947 | "^\\(To:\\|Cc:\\|Bcc:\\)"))) | |
13f1d088 KH |
948 | |
949 | (while (re-search-forward addr-regexp header-end t) | |
24975917 RS |
950 | (replace-match "") |
951 | (setq this-line (match-beginning 0)) | |
952 | (forward-line 1) | |
953 | ;; get any continuation lines | |
954 | (while (and (looking-at "^[ \t]+") (< (point) header-end)) | |
955 | (forward-line 1)) | |
956 | (setq this-line-end (point-marker)) | |
957 | (setq simple-address-list | |
958 | (concat simple-address-list " " | |
75da36cc | 959 | (mail-strip-quoted-names (buffer-substring this-line this-line-end))))) |
24975917 | 960 | (erase-buffer) |
92dfd10c | 961 | (insert " " simple-address-list "\n") |
75da36cc RW |
962 | (subst-char-in-region (point-min) (point-max) 10 ? t) ; newline --> blank |
963 | (subst-char-in-region (point-min) (point-max) ?, ? t) ; comma --> blank | |
964 | (subst-char-in-region (point-min) (point-max) 9 ? t) ; tab --> blank | |
24975917 RS |
965 | |
966 | (goto-char (point-min)) | |
967 | ;; tidyness in case hook is not robust when it looks at this | |
968 | (while (re-search-forward "[ \t]+" header-end t) (replace-match " ")) | |
969 | ||
970 | (goto-char (point-min)) | |
8805249b | 971 | (let (recipient-address-list) |
e2f7c221 | 972 | (while (re-search-forward " \\([^ ]+\\) " (point-max) t) |
8805249b | 973 | (backward-char 1) |
e2f7c221 | 974 | (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1)) |
75da36cc RW |
975 | recipient-address-list))) |
976 | (setq smtpmail-recipient-address-list recipient-address-list)))))) | |
24975917 RS |
977 | |
978 | (defun smtpmail-do-bcc (header-end) | |
5feeeae2 | 979 | "Delete [Resent-]BCC: and their continuation lines from the header area. |
24975917 RS |
980 | There may be multiple BCC: lines, and each may have arbitrarily |
981 | many continuation lines." | |
982 | (let ((case-fold-search t)) | |
067427f5 KH |
983 | (save-excursion |
984 | (goto-char (point-min)) | |
985 | ;; iterate over all BCC: lines | |
986 | (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t) | |
987 | (delete-region (match-beginning 0) | |
988 | (progn (forward-line 1) (point))) | |
989 | ;; get rid of any continuation lines | |
990 | (while (and (looking-at "^[ \t].*\n") (< (point) header-end)) | |
991 | (replace-match "")))))) | |
24975917 | 992 | |
24975917 RS |
993 | (provide 'smtpmail) |
994 | ||
092af6d8 | 995 | ;;; smtpmail.el ends here |