(Handling C-s and C-q with flow control, Binding C-s and C-q): Update the
[bpt/emacs.git] / lisp / mail / smtpmail.el
CommitLineData
092af6d8 1;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
e889eabc
GM
2;;; ### Hacked by Mike Taylor, 11th October 1999 to add support for
3;;; automatically appending a domain to RCPT TO: addresses.
24975917 4
e2f7c221 5;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
24975917
RS
6
7;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
0e2701ca 8;; Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
c50d5ce0 9;; ESMTP support: Simon Leinen <simon@switch.ch>
24975917
RS
10;; Keywords: mail
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation; either version 2, or (at your option)
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
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
KH
35;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
36;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use `message'
24975917 37;;(setq smtpmail-default-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
308bc5d8
RS
42;; To queue mail, set smtpmail-queue-mail to t and use
43;; smtpmail-send-queued-mail to send.
44
45
24975917
RS
46;;; Code:
47
48(require 'sendmail)
308bc5d8 49(require 'time-stamp)
24975917
RS
50
51;;;
00ed33e7
RS
52(defgroup smtpmail nil
53 "SMTP protocol for sending mail."
54 :group 'mail)
24975917 55
00ed33e7
RS
56
57(defcustom smtpmail-default-smtp-server nil
58 "*Specify default SMTP server."
59 :type '(choice (const nil) string)
60 :group 'smtpmail)
61
62(defcustom smtpmail-smtp-server
e2f7c221 63 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
00ed33e7
RS
64 "*The name of the host running SMTP server."
65 :type '(choice (const nil) string)
66 :group 'smtpmail)
24975917 67
00ed33e7
RS
68(defcustom smtpmail-smtp-service 25
69 "*SMTP service port number. smtp or 25 ."
70 :type 'integer
71 :group 'smtpmail)
24975917 72
00ed33e7 73(defcustom smtpmail-local-domain nil
24975917
RS
74 "*Local domain name without a host name.
75If the function (system-name) returns the full internet address,
00ed33e7
RS
76don't define this value."
77 :type '(choice (const nil) string)
78 :group 'smtpmail)
79
e889eabc
GM
80(defcustom smtpmail-sendto-domain nil
81 "*Local domain name without a host name.
82This is appended (with an @-sign) to any specified recipients which do
83not include an @-sign, so that each RCPT TO address is fully qualified.
84\(Some configurations of sendmail require this.)
85
86Don't bother to set this unless you have get an error like:
87 Sending failed; SMTP protocol error
88when sending mail, and the *trace of SMTP session to <somewhere>*
89buffer includes an exchange like:
90 RCPT TO: <someone>
91 501 <someone>: recipient address must contain a domain
92"
93 :type '(choice (const nil) string)
94 :group 'smtpmail)
95
96(defun maybe-append-domain (recipient)
97 (if (or (not smtpmail-sendto-domain)
98 (string-match "@" recipient))
99 recipient
100 (concat recipient "@" smtpmail-sendto-domain)))
101
00ed33e7
RS
102(defcustom smtpmail-debug-info nil
103 "*smtpmail debug info printout. messages and process buffer."
104 :type 'boolean
105 :group 'smtpmail)
106
107(defcustom smtpmail-code-conv-from nil ;; *junet*
108 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
109 :type 'boolean
110 :group 'smtpmail)
24975917 111
308bc5d8
RS
112(defcustom smtpmail-queue-mail nil
113 "*Specify if mail is queued (if t) or sent immediately (if nil).
114If queued, it is stored in the directory `smtpmail-queue-dir'
115and sent with `smtpmail-send-queued-mail'."
116 :type 'boolean
117 :group 'smtpmail)
118
119(defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
120 "*Directory where `smtpmail.el' stores queued mail."
121 :type 'directory
122 :group 'smtpmail)
123
124(defvar smtpmail-queue-index-file "index"
125 "File name of queued mail index,
126This is relative to `smtpmail-queue-dir'.")
127
fb035bbf
RS
128(defvar smtpmail-address-buffer)
129(defvar smtpmail-recipient-address-list)
130
131;; Buffer-local variable.
132(defvar smtpmail-read-point)
133
308bc5d8
RS
134(defvar smtpmail-queue-index (concat smtpmail-queue-dir
135 smtpmail-queue-index-file))
136
24975917
RS
137;;;
138;;;
139;;;
140
f38d3514 141;;;###autoload
24975917 142(defun smtpmail-send-it ()
e2f7c221 143 (require 'mail-utils)
24975917
RS
144 (let ((errbuf (if mail-interactive
145 (generate-new-buffer " smtpmail errors")
146 0))
147 (tembuf (generate-new-buffer " smtpmail temp"))
148 (case-fold-search nil)
24975917 149 delimline
83af570e
KH
150 (mailbuf (current-buffer))
151 (smtpmail-code-conv-from
152 (if enable-multibyte-characters
153 (let ((sendmail-coding-system smtpmail-code-conv-from))
154 (select-message-coding-system)))))
24975917
RS
155 (unwind-protect
156 (save-excursion
157 (set-buffer tembuf)
158 (erase-buffer)
159 (insert-buffer-substring mailbuf)
160 (goto-char (point-max))
161 ;; require one newline at the end.
162 (or (= (preceding-char) ?\n)
163 (insert ?\n))
164 ;; Change header-delimiter to be what sendmail expects.
92a3f23d 165 (mail-sendmail-undelimit-header)
24975917 166 (setq delimline (point-marker))
e2f7c221 167;; (sendmail-synch-aliases)
24975917
RS
168 (if mail-aliases
169 (expand-mail-aliases (point-min) delimline))
170 (goto-char (point-min))
171 ;; ignore any blank lines in the header
172 (while (and (re-search-forward "\n\n\n*" delimline t)
173 (< (point) delimline))
174 (replace-match "\n"))
175 (let ((case-fold-search t))
5feeeae2
RS
176 ;; We used to process Resent-... headers here,
177 ;; but it was not done properly, and the job
178 ;; is done correctly in smtpmail-deduce-address-list.
24975917
RS
179 ;; Don't send out a blank subject line
180 (goto-char (point-min))
5feeeae2
RS
181 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
182 (replace-match "")
183 ;; This one matches a Subject just before the header delimiter.
184 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
185 (= (match-end 0) delimline))
186 (replace-match "")))
0e2701ca
RS
187 ;; Put the "From:" field in unless for some odd reason
188 ;; they put one in themselves.
189 (goto-char (point-min))
190 (if (not (re-search-forward "^From:" delimline t))
191 (let* ((login user-mail-address)
192 (fullname (user-full-name)))
193 (cond ((eq mail-from-style 'angles)
194 (insert "From: " fullname)
195 (let ((fullname-start (+ (point-min) 6))
196 (fullname-end (point-marker)))
197 (goto-char fullname-start)
198 ;; Look for a character that cannot appear unquoted
199 ;; according to RFC 822.
200 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
201 fullname-end 1)
202 (progn
203 ;; Quote fullname, escaping specials.
204 (goto-char fullname-start)
205 (insert "\"")
206 (while (re-search-forward "[\"\\]"
207 fullname-end 1)
208 (replace-match "\\\\\\&" t))
209 (insert "\""))))
210 (insert " <" login ">\n"))
211 ((eq mail-from-style 'parens)
212 (insert "From: " login " (")
213 (let ((fullname-start (point)))
214 (insert fullname)
215 (let ((fullname-end (point-marker)))
216 (goto-char fullname-start)
217 ;; RFC 822 says \ and nonmatching parentheses
218 ;; must be escaped in comments.
219 ;; Escape every instance of ()\ ...
220 (while (re-search-forward "[()\\]" fullname-end 1)
221 (replace-match "\\\\\\&" t))
222 ;; ... then undo escaping of matching parentheses,
223 ;; including matching nested parentheses.
224 (goto-char fullname-start)
225 (while (re-search-forward
226 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
227 fullname-end 1)
228 (replace-match "\\1(\\3)" t)
229 (goto-char fullname-start))))
230 (insert ")\n"))
231 ((null mail-from-style)
232 (insert "From: " login "\n")))))
24975917
RS
233 ;; Insert an extra newline if we need it to work around
234 ;; Sun's bug that swallows newlines.
235 (goto-char (1+ delimline))
236 (if (eval mail-mailer-swallows-blank-line)
237 (newline))
0e2701ca
RS
238 ;; Find and handle any FCC fields.
239 (goto-char (point-min))
240 (if (re-search-forward "^FCC:" delimline t)
241 (mail-do-fcc delimline))
24975917
RS
242 (if mail-interactive
243 (save-excursion
244 (set-buffer errbuf)
245 (erase-buffer))))
246 ;;
247 ;;
248 ;;
249 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
250 (setq smtpmail-recipient-address-list
5feeeae2 251 (smtpmail-deduce-address-list tembuf (point-min) delimline))
24975917 252 (kill-buffer smtpmail-address-buffer)
308bc5d8 253
24975917 254 (smtpmail-do-bcc delimline)
308bc5d8
RS
255 ; Send or queue
256 (if (not smtpmail-queue-mail)
257 (if (not (null smtpmail-recipient-address-list))
258 (if (not (smtpmail-via-smtp
259 smtpmail-recipient-address-list tembuf))
260 (error "Sending failed; SMTP protocol error"))
261 (error "Sending failed; no recipients"))
262 (let* ((file-data (concat
263 smtpmail-queue-dir
3375a61c
KH
264 (concat (time-stamp-yyyy-mm-dd)
265 "_" (time-stamp-hh:mm:ss))))
e9166487
AI
266 (file-data (convert-standard-filename file-data))
267 (file-elisp (concat file-data ".el"))
308bc5d8
RS
268 (buffer-data (create-file-buffer file-data))
269 (buffer-elisp (create-file-buffer file-elisp))
270 (buffer-scratch "*queue-mail*"))
271 (save-excursion
272 (set-buffer buffer-data)
273 (erase-buffer)
274 (insert-buffer tembuf)
275 (write-file file-data)
276 (set-buffer buffer-elisp)
277 (erase-buffer)
278 (insert (concat
279 "(setq smtpmail-recipient-address-list '"
280 (prin1-to-string smtpmail-recipient-address-list)
281 ")\n"))
282 (write-file file-elisp)
283 (set-buffer (generate-new-buffer buffer-scratch))
284 (insert (concat file-data "\n"))
285 (append-to-file (point-min)
286 (point-max)
287 smtpmail-queue-index)
288 )
289 (kill-buffer buffer-scratch)
290 (kill-buffer buffer-data)
291 (kill-buffer buffer-elisp))))
24975917
RS
292 (kill-buffer tembuf)
293 (if (bufferp errbuf)
294 (kill-buffer errbuf)))))
295
308bc5d8
RS
296(defun smtpmail-send-queued-mail ()
297 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
298 (interactive)
299 ;;; Get index, get first mail, send it, get second mail, etc...
300 (let ((buffer-index (find-file-noselect smtpmail-queue-index))
301 (file-msg "")
302 (tembuf nil))
303 (save-excursion
304 (set-buffer buffer-index)
305 (beginning-of-buffer)
306 (while (not (eobp))
307 (setq file-msg (buffer-substring (point) (save-excursion
308 (end-of-line)
309 (point))))
310 (load file-msg)
311 (setq tembuf (find-file-noselect file-msg))
312 (if (not (null smtpmail-recipient-address-list))
313 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
314 tembuf))
315 (error "Sending failed; SMTP protocol error"))
316 (error "Sending failed; no recipients"))
317 (delete-file file-msg)
318 (delete-file (concat file-msg ".el"))
319 (kill-buffer tembuf)
320 (kill-line 1))
321 (set-buffer buffer-index)
322 (save-buffer smtpmail-queue-index)
323 (kill-buffer buffer-index)
324 )))
24975917
RS
325
326;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
327
328(defun smtpmail-fqdn ()
329 (if smtpmail-local-domain
330 (concat (system-name) "." smtpmail-local-domain)
331 (system-name)))
332
333(defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
334 (let ((process nil)
f38d3514
KH
335 (host (or smtpmail-smtp-server
336 (error "`smtpmail-smtp-server' not defined")))
8805249b 337 (port smtpmail-smtp-service)
24975917 338 response-code
8805249b 339 greeting
c50d5ce0
RS
340 process-buffer
341 (supported-extensions '()))
24975917
RS
342 (unwind-protect
343 (catch 'done
344 ;; get or create the trace buffer
345 (setq process-buffer
346 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
347
348 ;; clear the trace buffer of old output
349 (save-excursion
350 (set-buffer process-buffer)
351 (erase-buffer))
352
353 ;; open the connection to the server
354 (setq process (open-network-stream "SMTP" process-buffer host port))
355 (and (null process) (throw 'done nil))
356
357 ;; set the send-filter
358 (set-process-filter process 'smtpmail-process-filter)
359
360 (save-excursion
361 (set-buffer process-buffer)
4b876894 362 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
24975917
RS
363 (make-local-variable 'smtpmail-read-point)
364 (setq smtpmail-read-point (point-min))
365
366
367 (if (or (null (car (setq greeting (smtpmail-read-response process))))
368 (not (integerp (car greeting)))
369 (>= (car greeting) 400))
370 (throw 'done nil)
371 )
372
c50d5ce0
RS
373 ;; EHLO
374 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
24975917
RS
375
376 (if (or (null (car (setq response-code (smtpmail-read-response process))))
377 (not (integerp (car response-code)))
378 (>= (car response-code) 400))
c50d5ce0
RS
379 (progn
380 ;; HELO
381 (smtpmail-send-command process (format "HELO %s" (smtpmail-fqdn)))
382
383 (if (or (null (car (setq response-code (smtpmail-read-response process))))
384 (not (integerp (car response-code)))
385 (>= (car response-code) 400))
386 (throw 'done nil)))
387 (let ((extension-lines (cdr (cdr response-code))))
388 (while extension-lines
2c79f5b8 389 (let ((name (intern (downcase (car (split-string (substring (car extension-lines) 4) "[ ]"))))))
c50d5ce0
RS
390 (and name
391 (cond ((memq name '(verb xvrb 8bitmime onex xone
392 expn size dsn etrn
393 help xusr))
394 (setq supported-extensions
395 (cons name supported-extensions)))
396 (t (message "unknown extension %s"
397 name)))))
398 (setq extension-lines (cdr extension-lines)))))
399
400 (if (or (member 'onex supported-extensions)
401 (member 'xone supported-extensions))
402 (progn
403 (smtpmail-send-command process (format "ONEX"))
404 (if (or (null (car (setq response-code (smtpmail-read-response process))))
405 (not (integerp (car response-code)))
406 (>= (car response-code) 400))
407 (throw 'done nil))))
408
409 (if (and smtpmail-debug-info
410 (or (member 'verb supported-extensions)
411 (member 'xvrb supported-extensions)))
412 (progn
413 (smtpmail-send-command process (format "VERB"))
414 (if (or (null (car (setq response-code (smtpmail-read-response process))))
415 (not (integerp (car response-code)))
416 (>= (car response-code) 400))
417 (throw 'done nil))))
418
419 (if (member 'xusr supported-extensions)
420 (progn
421 (smtpmail-send-command process (format "XUSR"))
422 (if (or (null (car (setq response-code (smtpmail-read-response process))))
423 (not (integerp (car response-code)))
424 (>= (car response-code) 400))
425 (throw 'done nil))))
24975917
RS
426
427 ;; MAIL FROM: <sender>
c50d5ce0
RS
428 (let ((size-part
429 (if (member 'size supported-extensions)
430 (format " SIZE=%d"
431 (save-excursion
432 (set-buffer smtpmail-text-buffer)
433 ;; size estimate:
434 (+ (- (point-max) (point-min))
435 ;; Add one byte for each change-of-line
436 ;; because or CR-LF representation:
437 (count-lines (point-min) (point-max))
438 ;; For some reason, an empty line is
439 ;; added to the message. Maybe this
440 ;; is a bug, but it can't hurt to add
441 ;; those two bytes anyway:
442 2)))
443 ""))
444 (body-part
445 (if (member '8bitmime supported-extensions)
446 ;; FIXME:
447 ;; Code should be added here that transforms
448 ;; the contents of the message buffer into
449 ;; something the receiving SMTP can handle.
450 ;; For a receiver that supports 8BITMIME, this
451 ;; may mean converting BINARY to BASE64, or
452 ;; adding Content-Transfer-Encoding and the
453 ;; other MIME headers. The code should also
454 ;; return an indication of what encoding the
455 ;; message buffer is now, i.e. ASCII or
456 ;; 8BITMIME.
457 (if nil
458 " BODY=8BITMIME"
459 "")
460 "")))
461; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
462 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
463 user-mail-address
464 size-part
465 body-part))
466
467 (if (or (null (car (setq response-code (smtpmail-read-response process))))
468 (not (integerp (car response-code)))
469 (>= (car response-code) 400))
470 (throw 'done nil)
471 ))
24975917
RS
472
473 ;; RCPT TO: <recipient>
8805249b
RS
474 (let ((n 0))
475 (while (not (null (nth n recipient)))
e889eabc 476 (smtpmail-send-command process (format "RCPT TO: <%s>" (maybe-append-domain (nth n recipient))))
8805249b
RS
477 (setq n (1+ n))
478
c50d5ce0
RS
479 (setq response-code (smtpmail-read-response process))
480 (if (or (null (car response-code))
8805249b
RS
481 (not (integerp (car response-code)))
482 (>= (car response-code) 400))
483 (throw 'done nil)
484 )
485 ))
24975917
RS
486
487 ;; DATA
488 (smtpmail-send-command process "DATA")
489
490 (if (or (null (car (setq response-code (smtpmail-read-response process))))
491 (not (integerp (car response-code)))
492 (>= (car response-code) 400))
493 (throw 'done nil)
494 )
495
496 ;; Mail contents
497 (smtpmail-send-data process smtpmail-text-buffer)
498
499 ;;DATA end "."
500 (smtpmail-send-command process ".")
501
502 (if (or (null (car (setq response-code (smtpmail-read-response process))))
503 (not (integerp (car response-code)))
504 (>= (car response-code) 400))
505 (throw 'done nil)
506 )
507
508 ;;QUIT
509; (smtpmail-send-command process "QUIT")
510; (and (null (car (smtpmail-read-response process)))
511; (throw 'done nil))
512 t ))
513 (if process
514 (save-excursion
515 (set-buffer (process-buffer process))
516 (smtpmail-send-command process "QUIT")
517 (smtpmail-read-response process)
518
519; (if (or (null (car (setq response-code (smtpmail-read-response process))))
520; (not (integerp (car response-code)))
521; (>= (car response-code) 400))
522; (throw 'done nil)
523; )
524 (delete-process process))))))
525
526
24975917
RS
527(defun smtpmail-process-filter (process output)
528 (save-excursion
529 (set-buffer (process-buffer process))
530 (goto-char (point-max))
531 (insert output)))
532
24975917
RS
533(defun smtpmail-read-response (process)
534 (let ((case-fold-search nil)
c50d5ce0 535 (response-strings nil)
24975917 536 (response-continue t)
c50d5ce0 537 (return-value '(nil ()))
24975917
RS
538 match-end)
539
24975917 540 (while response-continue
c8d16dbd 541 (goto-char smtpmail-read-point)
24975917
RS
542 (while (not (search-forward "\r\n" nil t))
543 (accept-process-output process)
544 (goto-char smtpmail-read-point))
545
546 (setq match-end (point))
c50d5ce0
RS
547 (setq response-strings
548 (cons (buffer-substring smtpmail-read-point (- match-end 2))
549 response-strings))
24975917
RS
550
551 (goto-char smtpmail-read-point)
552 (if (looking-at "[0-9]+ ")
c50d5ce0
RS
553 (let ((begin (match-beginning 0))
554 (end (match-end 0)))
555 (if smtpmail-debug-info
556 (message "%s" (car response-strings)))
24975917 557
c50d5ce0 558 (setq smtpmail-read-point match-end)
24975917 559
c50d5ce0
RS
560 ;; ignore lines that start with "0"
561 (if (looking-at "0[0-9]+ ")
562 nil
563 (setq response-continue nil)
564 (setq return-value
565 (cons (string-to-int
566 (buffer-substring begin end))
567 (nreverse response-strings)))))
24975917
RS
568
569 (if (looking-at "[0-9]+-")
c50d5ce0
RS
570 (progn (if smtpmail-debug-info
571 (message "%s" (car response-strings)))
572 (setq smtpmail-read-point match-end)
24975917
RS
573 (setq response-continue t))
574 (progn
575 (setq smtpmail-read-point match-end)
576 (setq response-continue nil)
577 (setq return-value
c50d5ce0 578 (cons nil (nreverse response-strings)))
24975917
RS
579 )
580 )))
581 (setq smtpmail-read-point match-end)
582 return-value))
583
584
24975917
RS
585(defun smtpmail-send-command (process command)
586 (goto-char (point-max))
587 (if (= (aref command 0) ?P)
588 (insert "PASS <omitted>\r\n")
589 (insert command "\r\n"))
590 (setq smtpmail-read-point (point))
591 (process-send-string process command)
592 (process-send-string process "\r\n"))
593
24975917
RS
594(defun smtpmail-send-data-1 (process data)
595 (goto-char (point-max))
596
83af570e
KH
597 (if (and (multibyte-string-p data)
598 smtpmail-code-conv-from)
599 (setq data (string-as-multibyte
600 (encode-coding-string data smtpmail-code-conv-from))))
24975917
RS
601
602 (if smtpmail-debug-info
603 (insert data "\r\n"))
604
605 (setq smtpmail-read-point (point))
57810560
KH
606 ;; Escape "." at start of a line
607 (if (eq (string-to-char data) ?.)
24975917 608 (process-send-string process "."))
57810560 609 (process-send-string process data)
24975917
RS
610 (process-send-string process "\r\n")
611 )
612
613(defun smtpmail-send-data (process buffer)
614 (let
615 ((data-continue t)
616 (sending-data nil)
617 this-line
618 this-line-end)
619
620 (save-excursion
621 (set-buffer buffer)
622 (goto-char (point-min)))
623
624 (while data-continue
625 (save-excursion
626 (set-buffer buffer)
627 (beginning-of-line)
628 (setq this-line (point))
629 (end-of-line)
630 (setq this-line-end (point))
631 (setq sending-data nil)
632 (setq sending-data (buffer-substring this-line this-line-end))
633 (if (/= (forward-line 1) 0)
634 (setq data-continue nil)))
635
636 (smtpmail-send-data-1 process sending-data)
637 )
638 )
639 )
640
641
642(defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
643 "Get address list suitable for smtp RCPT TO: <address>."
644 (require 'mail-utils) ;; pick up mail-strip-quoted-names
24975917 645
1188dd37
AI
646 (unwind-protect
647 (save-excursion
648 (set-buffer smtpmail-address-buffer) (erase-buffer)
649 (let
650 ((case-fold-search t)
651 (simple-address-list "")
652 this-line
653 this-line-end
654 addr-regexp)
24975917
RS
655 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
656 (goto-char (point-min))
13f1d088
KH
657 ;; RESENT-* fields should stop processing of regular fields.
658 (save-excursion
5feeeae2
RS
659 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" header-end t)
660 (setq addr-regexp "^Resent-\\(to\\|cc\\|bcc\\):")
661 (setq addr-regexp "^\\(To:\\|Cc:\\|Bcc:\\)")))
13f1d088
KH
662
663 (while (re-search-forward addr-regexp header-end t)
24975917
RS
664 (replace-match "")
665 (setq this-line (match-beginning 0))
666 (forward-line 1)
667 ;; get any continuation lines
668 (while (and (looking-at "^[ \t]+") (< (point) header-end))
669 (forward-line 1))
670 (setq this-line-end (point-marker))
671 (setq simple-address-list
672 (concat simple-address-list " "
673 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
674 )
675 (erase-buffer)
676 (insert-string " ")
677 (insert-string simple-address-list)
678 (insert-string "\n")
679 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
680 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
681 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
682
683 (goto-char (point-min))
684 ;; tidyness in case hook is not robust when it looks at this
685 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
686
687 (goto-char (point-min))
8805249b 688 (let (recipient-address-list)
e2f7c221 689 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
8805249b 690 (backward-char 1)
e2f7c221
RS
691 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
692 recipient-address-list))
8805249b
RS
693 )
694 (setq smtpmail-recipient-address-list recipient-address-list))
24975917
RS
695
696 )
1188dd37 697 )
24975917
RS
698 )
699 )
700
701
702(defun smtpmail-do-bcc (header-end)
5feeeae2 703 "Delete [Resent-]BCC: and their continuation lines from the header area.
24975917
RS
704There may be multiple BCC: lines, and each may have arbitrarily
705many continuation lines."
706 (let ((case-fold-search t))
067427f5
KH
707 (save-excursion
708 (goto-char (point-min))
709 ;; iterate over all BCC: lines
710 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
711 (delete-region (match-beginning 0)
712 (progn (forward-line 1) (point)))
713 ;; get rid of any continuation lines
714 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
715 (replace-match ""))))))
24975917
RS
716
717
718(provide 'smtpmail)
719
092af6d8 720;;; smtpmail.el ends here