1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995, 1996, 2001 Free Software Foundation, Inc.
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
6 ;; Maintainer: Simon Josefsson <simon@josefsson.org>
7 ;; w32 Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
8 ;; ESMTP support: Simon Leinen <simon@switch.ch>
9 ;; Hacked by Mike Taylor, 11th October 1999 to add support for
10 ;; automatically appending a domain to RCPT TO: addresses.
11 ;; AUTH=LOGIN support: Stephen Cranefield <scranefield@infoscience.otago.ac.nz>
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
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.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
33 ;; Send Mail to smtp host from smtpmail temp buffer.
35 ;; Please add these lines in your .emacs(_emacs) or use customize.
37 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
38 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you are using Gnus.
39 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
40 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
41 ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
42 ;;(setq smtpmail-debug-info t) ; only to debug problems
43 ;;(setq smtpmail-auth-credentials
44 ;; '(("YOUR SMTP HOST" 25 "username" "password")))
45 ;;(setq smtpmail-starttls-credentials
46 ;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
48 ;; To queue mail, set smtpmail-queue-mail to t and use
49 ;; smtpmail-send-queued-mail to send.
51 ;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
52 ;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
53 ;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
54 ;; Rewritten by Simon Josefsson to use same credential variable as AUTH
57 ;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
58 ;; Authentication by the AUTH mechanism.
59 ;; See http://www.ietf.org/rfc/rfc2554.txt
61 ;; Modified by Simon Josefsson <simon@josefsson.org>, 2000-10-07, to support
62 ;; STARTTLS. Requires external program
63 ;; ftp://ftp.opaopa.org/pub/elisp/starttls-*.tar.gz.
64 ;; See http://www.ietf.org/rfc/rfc2246.txt, http://www.ietf.org/rfc/rfc2487.txt
70 (autoload 'starttls-open-stream "starttls")
71 (autoload 'starttls-negotiate "starttls")
72 (autoload 'mail-strip-quoted-names "mail-utils")
73 (autoload 'message-make-date "message")
74 (autoload 'message-make-message-id "message")
75 (autoload 'rfc2104-hash "rfc2104")
78 (defgroup smtpmail nil
79 "SMTP protocol for sending mail."
83 (defcustom smtpmail-default-smtp-server nil
84 "*Specify default SMTP server."
85 :type '(choice (const nil) string)
88 (defcustom smtpmail-smtp-server
89 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
90 "*The name of the host running SMTP server."
91 :type '(choice (const nil) string)
94 (defcustom smtpmail-smtp-service 25
95 "*SMTP service port number.
96 The default value would be \"smtp\" or 25 ."
97 :type '(choice (integer :tag "Port") (string :tag "Service"))
100 (defcustom smtpmail-local-domain nil
101 "*Local domain name without a host name.
102 If the function (system-name) returns the full internet address,
103 don't define this value."
104 :type '(choice (const nil) string)
107 (defcustom smtpmail-sendto-domain nil
108 "*Local domain name without a host name.
109 This is appended (with an @-sign) to any specified recipients which do
110 not include an @-sign, so that each RCPT TO address is fully qualified.
111 \(Some configurations of sendmail require this.)
113 Don't bother to set this unless you have get an error like:
114 Sending failed; SMTP protocol error
115 when sending mail, and the *trace of SMTP session to <somewhere>*
116 buffer includes an exchange like:
118 501 <someone>: recipient address must contain a domain
120 :type '(choice (const nil) string)
123 (defcustom smtpmail-debug-info nil
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
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."
136 (defcustom smtpmail-code-conv-from nil ;; *junet*
137 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
141 (defcustom smtpmail-queue-mail nil
142 "*Specify if mail is queued (if t) or sent immediately (if nil).
143 If queued, it is stored in the directory `smtpmail-queue-dir'
144 and sent with `smtpmail-send-queued-mail'."
148 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
149 "*Directory where `smtpmail.el' stores queued mail."
153 (defcustom smtpmail-auth-credentials '(("" 25 "" nil))
154 "Specify username and password for servers.
155 It is a list of four-element lists that contain, in order,
156 `servername' (a string), `port' (an integer), `user' (a string) and
157 `password' (a string, or nil to query the user when needed).
158 If you need to enter a `realm' too, add it to the user string, so that
159 it looks like `user@realm'."
160 :type '(repeat (list (string :tag "Server")
161 (integer :tag "Port")
162 (string :tag "Username")
163 (choice (const :tag "Query when needed" nil)
164 (string :tag "Password"))))
168 (defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
169 "Specify STARTTLS keys and certificates for servers.
170 This is a list of four-element list with `servername' (a string),
171 `port' (an integer), `key' (a filename) and `certificate' (a filename)."
172 :type '(repeat (list (string :tag "Server")
173 (integer :tag "Port")
175 (file :tag "Certificate")))
179 (defcustom smtpmail-warn-about-unknown-extensions nil
180 "*If set, print warnings about unknown SMTP extensions.
181 This is mainly useful for development purposes, to learn about
182 new SMTP extensions that might be useful to support."
187 (defvar smtpmail-queue-index-file "index"
188 "File name of queued mail index,
189 This is relative to `smtpmail-queue-dir'.")
191 (defvar smtpmail-address-buffer)
192 (defvar smtpmail-recipient-address-list)
194 ;; Buffer-local variable.
195 (defvar smtpmail-read-point)
197 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
198 smtpmail-queue-index-file))
200 (defconst smtpmail-auth-supported '(cram-md5 login)
201 "List of supported SMTP AUTH mechanisms.")
208 (defun smtpmail-send-it ()
209 (let ((errbuf (if mail-interactive
210 (generate-new-buffer " smtpmail errors")
212 (tembuf (generate-new-buffer " smtpmail temp"))
213 (case-fold-search nil)
215 (mailbuf (current-buffer))
216 (smtpmail-code-conv-from
217 (if enable-multibyte-characters
218 (let ((sendmail-coding-system smtpmail-code-conv-from))
219 (select-message-coding-system)))))
224 (insert-buffer-substring mailbuf)
225 (goto-char (point-max))
226 ;; require one newline at the end.
227 (or (= (preceding-char) ?\n)
229 ;; Change header-delimiter to be what sendmail expects.
230 (mail-sendmail-undelimit-header)
231 (setq delimline (point-marker))
232 ;; (sendmail-synch-aliases)
234 (expand-mail-aliases (point-min) delimline))
235 (goto-char (point-min))
236 ;; ignore any blank lines in the header
237 (while (and (re-search-forward "\n\n\n*" delimline t)
238 (< (point) delimline))
239 (replace-match "\n"))
240 (let ((case-fold-search t))
241 ;; We used to process Resent-... headers here,
242 ;; but it was not done properly, and the job
243 ;; is done correctly in smtpmail-deduce-address-list.
244 ;; Don't send out a blank subject line
245 (goto-char (point-min))
246 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
248 ;; This one matches a Subject just before the header delimiter.
249 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
250 (= (match-end 0) delimline))
252 ;; Put the "From:" field in unless for some odd reason
253 ;; they put one in themselves.
254 (goto-char (point-min))
255 (if (not (re-search-forward "^From:" delimline t))
256 (let* ((login user-mail-address)
257 (fullname (user-full-name)))
258 (cond ((eq mail-from-style 'angles)
259 (insert "From: " fullname)
260 (let ((fullname-start (+ (point-min) 6))
261 (fullname-end (point-marker)))
262 (goto-char fullname-start)
263 ;; Look for a character that cannot appear unquoted
264 ;; according to RFC 822.
265 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
268 ;; Quote fullname, escaping specials.
269 (goto-char fullname-start)
271 (while (re-search-forward "[\"\\]"
273 (replace-match "\\\\\\&" t))
275 (insert " <" login ">\n"))
276 ((eq mail-from-style 'parens)
277 (insert "From: " login " (")
278 (let ((fullname-start (point)))
280 (let ((fullname-end (point-marker)))
281 (goto-char fullname-start)
282 ;; RFC 822 says \ and nonmatching parentheses
283 ;; must be escaped in comments.
284 ;; Escape every instance of ()\ ...
285 (while (re-search-forward "[()\\]" fullname-end 1)
286 (replace-match "\\\\\\&" t))
287 ;; ... then undo escaping of matching parentheses,
288 ;; including matching nested parentheses.
289 (goto-char fullname-start)
290 (while (re-search-forward
291 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
293 (replace-match "\\1(\\3)" t)
294 (goto-char fullname-start))))
296 ((null mail-from-style)
297 (insert "From: " login "\n")))))
298 ;; Insert a `Message-Id:' field if there isn't one yet.
299 (goto-char (point-min))
300 (unless (re-search-forward "^Message-Id:" delimline t)
301 (insert "Message-Id: " (message-make-message-id) "\n"))
302 ;; Insert a `Date:' field if there isn't one yet.
303 (goto-char (point-min))
304 (unless (re-search-forward "^Date:" delimline t)
305 (insert "Date: " (message-make-date) "\n"))
306 ;; Insert an extra newline if we need it to work around
307 ;; Sun's bug that swallows newlines.
308 (goto-char (1+ delimline))
309 (if (eval mail-mailer-swallows-blank-line)
311 ;; Find and handle any FCC fields.
312 (goto-char (point-min))
313 (if (re-search-forward "^FCC:" delimline t)
314 (mail-do-fcc delimline))
316 (with-current-buffer errbuf
321 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
322 (setq smtpmail-recipient-address-list
323 (smtpmail-deduce-address-list tembuf (point-min) delimline))
324 (kill-buffer smtpmail-address-buffer)
326 (smtpmail-do-bcc delimline)
328 (if (not smtpmail-queue-mail)
329 (if (not (null smtpmail-recipient-address-list))
330 (if (not (smtpmail-via-smtp
331 smtpmail-recipient-address-list tembuf))
332 (error "Sending failed; SMTP protocol error"))
333 (error "Sending failed; no recipients"))
334 (let* ((file-data (concat
336 (concat (time-stamp-yyyy-mm-dd)
337 "_" (time-stamp-hh:mm:ss))))
338 (file-data (convert-standard-filename file-data))
339 (file-elisp (concat file-data ".el"))
340 (buffer-data (create-file-buffer file-data))
341 (buffer-elisp (create-file-buffer file-elisp))
342 (buffer-scratch "*queue-mail*"))
343 (with-current-buffer buffer-data
345 (insert-buffer tembuf)
346 (write-file file-data)
347 (set-buffer buffer-elisp)
350 "(setq smtpmail-recipient-address-list '"
351 (prin1-to-string smtpmail-recipient-address-list)
353 (write-file file-elisp)
354 (set-buffer (generate-new-buffer buffer-scratch))
355 (insert (concat file-data "\n"))
356 (append-to-file (point-min)
358 smtpmail-queue-index)
360 (kill-buffer buffer-scratch)
361 (kill-buffer buffer-data)
362 (kill-buffer buffer-elisp))))
365 (kill-buffer errbuf)))))
367 (defun smtpmail-send-queued-mail ()
368 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
371 ;;; Get index, get first mail, send it, update index, get second
372 ;;; mail, send it, etc...
374 (insert-file-contents smtpmail-queue-index)
375 (beginning-of-buffer)
377 (setq file-msg (buffer-substring (point) (line-end-position)))
379 ;; Insert the message literally: it is already encoded as per
380 ;; the MIME headers, and code conversions might guess the
383 (let ((coding-system-for-read 'no-conversion))
384 (insert-file-contents file-msg))
385 (if (not (null smtpmail-recipient-address-list))
386 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
388 (error "Sending failed; SMTP protocol error"))
389 (error "Sending failed; no recipients")))
390 (delete-file file-msg)
391 (delete-file (concat file-msg ".el"))
393 (write-region (point-min) (point-max) smtpmail-queue-index))))
395 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
397 (defun smtpmail-fqdn ()
398 (if smtpmail-local-domain
399 (concat (system-name) "." smtpmail-local-domain)
402 (defsubst smtpmail-cred-server (cred)
405 (defsubst smtpmail-cred-port (cred)
408 (defsubst smtpmail-cred-key (cred)
411 (defsubst smtpmail-cred-user (cred)
414 (defsubst smtpmail-cred-cert (cred)
417 (defsubst smtpmail-cred-passwd (cred)
420 (defun smtpmail-find-credentials (cred server port)
423 (while (setq el (pop l))
424 (when (and (equal server (smtpmail-cred-server el))
425 (equal port (smtpmail-cred-port el)))
426 (throw 'done el))))))
428 (defun smtpmail-maybe-append-domain (recipient)
429 (if (or (not smtpmail-sendto-domain)
430 (string-match "@" recipient))
432 (concat recipient "@" smtpmail-sendto-domain)))
434 (defun smtpmail-intersection (list1 list2)
437 (when (memq el2 list1)
441 (defun smtpmail-open-stream (process-buffer host port)
442 (let ((cred (smtpmail-find-credentials
443 smtpmail-starttls-credentials host port)))
444 (if (null (and cred (condition-case ()
445 (call-process "starttls")
448 (open-network-stream "SMTP" process-buffer host port)
449 (let* ((cred-key (smtpmail-cred-key cred))
450 (cred-cert (smtpmail-cred-cert cred))
452 (when (and (stringp cred-key) (stringp cred-cert)
454 (setq cred-key (expand-file-name cred-key)))
456 (setq cred-cert (expand-file-name cred-cert))))
457 (list "--key-file" cred-key "--cert-file" cred-cert))))
458 (starttls-open-stream "SMTP" process-buffer host port)))))
460 (defun smtpmail-try-auth-methods (process supported-extensions host port)
461 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
462 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
463 (cred (smtpmail-find-credentials smtpmail-auth-credentials host port))
465 (or (smtpmail-cred-passwd cred)
467 (format "SMTP password for %s:%s: "
468 (smtpmail-cred-server cred)
469 (smtpmail-cred-port cred))))))
474 (smtpmail-send-command process (format "AUTH %s" mech))
475 (if (or (null (car (setq ret (smtpmail-read-response process))))
476 (not (integerp (car ret)))
479 (when (eq (car ret) 334)
480 (let* ((challenge (substring (cadr ret) 4))
481 (decoded (base64-decode-string challenge))
482 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
483 (response (concat (smtpmail-cred-user cred) " " hash))
484 (encoded (base64-encode-string response)))
485 (smtpmail-send-command process (format "%s" encoded))
486 (if (or (null (car (setq ret (smtpmail-read-response process))))
487 (not (integerp (car ret)))
489 (throw 'done nil)))))
491 (smtpmail-send-command process "AUTH LOGIN")
492 (if (or (null (car (setq ret (smtpmail-read-response process))))
493 (not (integerp (car ret)))
496 (smtpmail-send-command
497 process (base64-encode-string (smtpmail-cred-user cred)))
498 (if (or (null (car (setq ret (smtpmail-read-response process))))
499 (not (integerp (car ret)))
502 (smtpmail-send-command process (base64-encode-string passwd))
503 (if (or (null (car (setq ret (smtpmail-read-response process))))
504 (not (integerp (car ret)))
508 (error "Mechanism %s not implemented" mech)))
509 ;; Remember the password.
510 (unless (smtpmail-cred-passwd cred)
511 (setcar (cdr (cdr (cdr cred))) passwd)))))
513 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
515 (host (or smtpmail-smtp-server
516 (error "`smtpmail-smtp-server' not defined")))
517 (port smtpmail-smtp-service)
521 (supported-extensions '()))
524 ;; get or create the trace buffer
526 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
528 ;; clear the trace buffer of old output
529 (with-current-buffer process-buffer
532 ;; open the connection to the server
533 (setq process (smtpmail-open-stream process-buffer host port))
534 (and (null process) (throw 'done nil))
536 ;; set the send-filter
537 (set-process-filter process 'smtpmail-process-filter)
539 (with-current-buffer process-buffer
540 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
541 (make-local-variable 'smtpmail-read-point)
542 (setq smtpmail-read-point (point-min))
545 (if (or (null (car (setq greeting (smtpmail-read-response process))))
546 (not (integerp (car greeting)))
547 (>= (car greeting) 400))
555 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
557 (if (or (null (car (setq response-code
558 (smtpmail-read-response process))))
559 (not (integerp (car response-code)))
560 (>= (car response-code) 400))
563 (smtpmail-send-command
564 process (format "HELO %s" (smtpmail-fqdn)))
566 (if (or (null (car (setq response-code
567 (smtpmail-read-response process))))
568 (not (integerp (car response-code)))
569 (>= (car response-code) 400))
571 (dolist (line (cdr (cdr response-code)))
572 (let ((name (mapcar (lambda (s) (intern (downcase s)))
573 (split-string (substring line 4) "[ ]"))))
574 (and (eq (length name) 1)
575 (setq name (car name)))
577 (cond ((memq (if (consp name) (car name) name)
578 '(verb xvrb 8bitmime onex xone
582 auth=login auth starttls))
583 (setq supported-extensions
584 (cons name supported-extensions)))
585 (smtpmail-warn-about-unknown-extensions
586 (message "Unknown extension %s" name)))))))
589 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
590 (member 'starttls supported-extensions)
591 (process-id process))
593 (smtpmail-send-command process (format "STARTTLS"))
594 (if (or (null (car (setq response-code (smtpmail-read-response process))))
595 (not (integerp (car response-code)))
596 (>= (car response-code) 400))
598 (starttls-negotiate process)
599 (setq do-starttls nil))
600 (setq do-ehlo nil))))
602 (smtpmail-try-auth-methods process supported-extensions host port)
604 (if (or (member 'onex supported-extensions)
605 (member 'xone supported-extensions))
607 (smtpmail-send-command process (format "ONEX"))
608 (if (or (null (car (setq response-code (smtpmail-read-response process))))
609 (not (integerp (car response-code)))
610 (>= (car response-code) 400))
613 (if (and smtpmail-debug-verb
614 (or (member 'verb supported-extensions)
615 (member 'xvrb supported-extensions)))
617 (smtpmail-send-command process (format "VERB"))
618 (if (or (null (car (setq response-code (smtpmail-read-response process))))
619 (not (integerp (car response-code)))
620 (>= (car response-code) 400))
623 (if (member 'xusr supported-extensions)
625 (smtpmail-send-command process (format "XUSR"))
626 (if (or (null (car (setq response-code (smtpmail-read-response process))))
627 (not (integerp (car response-code)))
628 (>= (car response-code) 400))
631 ;; MAIL FROM: <sender>
633 (if (or (member 'size supported-extensions)
634 (assoc 'size supported-extensions))
636 (with-current-buffer smtpmail-text-buffer
638 (+ (- (point-max) (point-min))
639 ;; Add one byte for each change-of-line
640 ;; because or CR-LF representation:
641 (count-lines (point-min) (point-max))
642 ;; For some reason, an empty line is
643 ;; added to the message. Maybe this
644 ;; is a bug, but it can't hurt to add
645 ;; those two bytes anyway:
649 (if (member '8bitmime supported-extensions)
651 ;; Code should be added here that transforms
652 ;; the contents of the message buffer into
653 ;; something the receiving SMTP can handle.
654 ;; For a receiver that supports 8BITMIME, this
655 ;; may mean converting BINARY to BASE64, or
656 ;; adding Content-Transfer-Encoding and the
657 ;; other MIME headers. The code should also
658 ;; return an indication of what encoding the
659 ;; message buffer is now, i.e. ASCII or
665 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
666 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
667 (or mail-envelope-from
672 (if (or (null (car (setq response-code (smtpmail-read-response process))))
673 (not (integerp (car response-code)))
674 (>= (car response-code) 400))
678 ;; RCPT TO: <recipient>
680 (while (not (null (nth n recipient)))
681 (smtpmail-send-command process (format "RCPT TO: <%s>" (smtpmail-maybe-append-domain (nth n recipient))))
684 (setq response-code (smtpmail-read-response process))
685 (if (or (null (car response-code))
686 (not (integerp (car response-code)))
687 (>= (car response-code) 400))
693 (smtpmail-send-command process "DATA")
695 (if (or (null (car (setq response-code (smtpmail-read-response process))))
696 (not (integerp (car response-code)))
697 (>= (car response-code) 400))
702 (smtpmail-send-data process smtpmail-text-buffer)
705 (smtpmail-send-command process ".")
707 (if (or (null (car (setq response-code (smtpmail-read-response process))))
708 (not (integerp (car response-code)))
709 (>= (car response-code) 400))
714 ; (smtpmail-send-command process "QUIT")
715 ; (and (null (car (smtpmail-read-response process)))
719 (with-current-buffer (process-buffer process)
720 (smtpmail-send-command process "QUIT")
721 (smtpmail-read-response process)
723 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
724 ; (not (integerp (car response-code)))
725 ; (>= (car response-code) 400))
728 (delete-process process))))))
731 (defun smtpmail-process-filter (process output)
732 (with-current-buffer (process-buffer process)
733 (goto-char (point-max))
736 (defun smtpmail-read-response (process)
737 (let ((case-fold-search nil)
738 (response-strings nil)
739 (response-continue t)
740 (return-value '(nil ()))
743 (while response-continue
744 (goto-char smtpmail-read-point)
745 (while (not (search-forward "\r\n" nil t))
746 (accept-process-output process)
747 (goto-char smtpmail-read-point))
749 (setq match-end (point))
750 (setq response-strings
751 (cons (buffer-substring smtpmail-read-point (- match-end 2))
754 (goto-char smtpmail-read-point)
755 (if (looking-at "[0-9]+ ")
756 (let ((begin (match-beginning 0))
758 (if smtpmail-debug-info
759 (message "%s" (car response-strings)))
761 (setq smtpmail-read-point match-end)
763 ;; ignore lines that start with "0"
764 (if (looking-at "0[0-9]+ ")
766 (setq response-continue nil)
769 (buffer-substring begin end))
770 (nreverse response-strings)))))
772 (if (looking-at "[0-9]+-")
773 (progn (if smtpmail-debug-info
774 (message "%s" (car response-strings)))
775 (setq smtpmail-read-point match-end)
776 (setq response-continue t))
778 (setq smtpmail-read-point match-end)
779 (setq response-continue nil)
781 (cons nil (nreverse response-strings)))
784 (setq smtpmail-read-point match-end)
788 (defun smtpmail-send-command (process command)
789 (goto-char (point-max))
790 (if (= (aref command 0) ?P)
791 (insert "PASS <omitted>\r\n")
792 (insert command "\r\n"))
793 (setq smtpmail-read-point (point))
794 (process-send-string process command)
795 (process-send-string process "\r\n"))
797 (defun smtpmail-send-data-1 (process data)
798 (goto-char (point-max))
800 (if (and (multibyte-string-p data)
801 smtpmail-code-conv-from)
802 (setq data (string-as-multibyte
803 (encode-coding-string data smtpmail-code-conv-from))))
805 (if smtpmail-debug-info
806 (insert data "\r\n"))
808 (setq smtpmail-read-point (point))
809 ;; Escape "." at start of a line
810 (if (eq (string-to-char data) ?.)
811 (process-send-string process "."))
812 (process-send-string process data)
813 (process-send-string process "\r\n")
816 (defun smtpmail-send-data (process buffer)
823 (with-current-buffer buffer
824 (goto-char (point-min)))
827 (with-current-buffer buffer
829 (setq this-line (point))
831 (setq this-line-end (point))
832 (setq sending-data nil)
833 (setq sending-data (buffer-substring this-line this-line-end))
834 (if (/= (forward-line 1) 0)
835 (setq data-continue nil)))
837 (smtpmail-send-data-1 process sending-data)
843 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
844 "Get address list suitable for smtp RCPT TO: <address>."
846 (with-current-buffer smtpmail-address-buffer
849 ((case-fold-search t)
850 (simple-address-list "")
854 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
855 (goto-char (point-min))
856 ;; RESENT-* fields should stop processing of regular fields.
859 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
861 "^Resent-\\(to\\|cc\\|bcc\\):"
862 "^\\(To:\\|Cc:\\|Bcc:\\)")))
864 (while (re-search-forward addr-regexp header-end t)
866 (setq this-line (match-beginning 0))
868 ;; get any continuation lines
869 (while (and (looking-at "^[ \t]+") (< (point) header-end))
871 (setq this-line-end (point-marker))
872 (setq simple-address-list
873 (concat simple-address-list " "
874 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
877 (insert " " simple-address-list "\n")
878 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
879 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
880 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
882 (goto-char (point-min))
883 ;; tidyness in case hook is not robust when it looks at this
884 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
886 (goto-char (point-min))
887 (let (recipient-address-list)
888 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
890 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
891 recipient-address-list))
893 (setq smtpmail-recipient-address-list recipient-address-list))
901 (defun smtpmail-do-bcc (header-end)
902 "Delete [Resent-]BCC: and their continuation lines from the header area.
903 There may be multiple BCC: lines, and each may have arbitrarily
904 many continuation lines."
905 (let ((case-fold-search t))
907 (goto-char (point-min))
908 ;; iterate over all BCC: lines
909 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
910 (delete-region (match-beginning 0)
911 (progn (forward-line 1) (point)))
912 ;; get rid of any continuation lines
913 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
914 (replace-match ""))))))
919 ;;; smtpmail.el ends here