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 use message/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 ; or use ~/.authinfo
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")
76 (autoload 'netrc-parse "netrc")
79 (defgroup smtpmail nil
80 "SMTP protocol for sending mail."
84 (defcustom smtpmail-default-smtp-server nil
85 "*Specify default SMTP server."
86 :type '(choice (const nil) string)
89 (defcustom smtpmail-smtp-server
90 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
91 "*The name of the host running SMTP server."
92 :type '(choice (const nil) string)
95 (defcustom smtpmail-smtp-service 25
96 "*SMTP service port number.
97 The default value would be \"smtp\" or 25 ."
98 :type '(choice (integer :tag "Port") (string :tag "Service"))
101 (defcustom smtpmail-local-domain nil
102 "*Local domain name without a host name.
103 If the function (system-name) returns the full internet address,
104 don't define this value."
105 :type '(choice (const nil) string)
108 (defcustom smtpmail-sendto-domain nil
109 "*Local domain name without a host name.
110 This is appended (with an @-sign) to any specified recipients which do
111 not include an @-sign, so that each RCPT TO address is fully qualified.
112 \(Some configurations of sendmail require this.)
114 Don't bother to set this unless you have get an error like:
115 Sending failed; SMTP protocol error
116 when sending mail, and the *trace of SMTP session to <somewhere>*
117 buffer includes an exchange like:
119 501 <someone>: recipient address must contain a domain
121 :type '(choice (const nil) string)
124 (defcustom smtpmail-debug-info nil
125 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
126 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
131 (defcustom smtpmail-debug-verb nil
132 "Whether this library sends the SMTP VERB command or not.
133 The commands enables verbose information from the SMTP server."
137 (defcustom smtpmail-code-conv-from nil ;; *junet*
138 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
142 (defcustom smtpmail-queue-mail nil
143 "*Specify if mail is queued (if t) or sent immediately (if nil).
144 If queued, it is stored in the directory `smtpmail-queue-dir'
145 and sent with `smtpmail-send-queued-mail'."
149 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
150 "*Directory where `smtpmail.el' stores queued mail."
154 (defcustom smtpmail-auth-credentials "~/.authinfo"
155 "Specify username and password for servers, directly or via .netrc file.
156 This variable can either be a filename pointing to a file in netrc(5)
157 format, or list of four-element lists that contain, in order,
158 `servername' (a string), `port' (an integer), `user' (a string) and
159 `password' (a string, or nil to query the user when needed). If you
160 need to enter a `realm' too, add it to the user string, so that it
161 looks like `user@realm'."
163 (repeat (list (string :tag "Server")
164 (integer :tag "Port")
165 (string :tag "Username")
166 (choice (const :tag "Query when needed" nil)
167 (string :tag "Password")))))
171 (defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
172 "Specify STARTTLS keys and certificates for servers.
173 This is a list of four-element list with `servername' (a string),
174 `port' (an integer), `key' (a filename) and `certificate' (a filename)."
175 :type '(repeat (list (string :tag "Server")
176 (integer :tag "Port")
178 (file :tag "Certificate")))
182 (defcustom smtpmail-warn-about-unknown-extensions nil
183 "*If set, print warnings about unknown SMTP extensions.
184 This is mainly useful for development purposes, to learn about
185 new SMTP extensions that might be useful to support."
190 (defvar smtpmail-queue-index-file "index"
191 "File name of queued mail index,
192 This is relative to `smtpmail-queue-dir'.")
194 (defvar smtpmail-address-buffer)
195 (defvar smtpmail-recipient-address-list)
197 (defvar smtpmail-queue-counter 0)
199 ;; Buffer-local variable.
200 (defvar smtpmail-read-point)
202 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
203 smtpmail-queue-index-file))
205 (defconst smtpmail-auth-supported '(cram-md5 login)
206 "List of supported SMTP AUTH mechanisms.")
213 (defun smtpmail-send-it ()
214 (let ((errbuf (if mail-interactive
215 (generate-new-buffer " smtpmail errors")
217 (tembuf (generate-new-buffer " smtpmail temp"))
218 (case-fold-search nil)
220 (mailbuf (current-buffer))
221 (smtpmail-code-conv-from
222 (if enable-multibyte-characters
223 (let ((sendmail-coding-system smtpmail-code-conv-from))
224 (select-message-coding-system)))))
229 (insert-buffer-substring mailbuf)
230 (goto-char (point-max))
231 ;; require one newline at the end.
232 (or (= (preceding-char) ?\n)
234 ;; Change header-delimiter to be what sendmail expects.
235 (mail-sendmail-undelimit-header)
236 (setq delimline (point-marker))
237 ;; (sendmail-synch-aliases)
239 (expand-mail-aliases (point-min) delimline))
240 (goto-char (point-min))
241 ;; ignore any blank lines in the header
242 (while (and (re-search-forward "\n\n\n*" delimline t)
243 (< (point) delimline))
244 (replace-match "\n"))
245 (let ((case-fold-search t))
246 ;; We used to process Resent-... headers here,
247 ;; but it was not done properly, and the job
248 ;; is done correctly in smtpmail-deduce-address-list.
249 ;; Don't send out a blank subject line
250 (goto-char (point-min))
251 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
253 ;; This one matches a Subject just before the header delimiter.
254 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
255 (= (match-end 0) delimline))
257 ;; Put the "From:" field in unless for some odd reason
258 ;; they put one in themselves.
259 (goto-char (point-min))
260 (if (not (re-search-forward "^From:" delimline t))
261 (let* ((login user-mail-address)
262 (fullname (user-full-name)))
263 (cond ((eq mail-from-style 'angles)
264 (insert "From: " fullname)
265 (let ((fullname-start (+ (point-min) 6))
266 (fullname-end (point-marker)))
267 (goto-char fullname-start)
268 ;; Look for a character that cannot appear unquoted
269 ;; according to RFC 822.
270 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
273 ;; Quote fullname, escaping specials.
274 (goto-char fullname-start)
276 (while (re-search-forward "[\"\\]"
278 (replace-match "\\\\\\&" t))
280 (insert " <" login ">\n"))
281 ((eq mail-from-style 'parens)
282 (insert "From: " login " (")
283 (let ((fullname-start (point)))
285 (let ((fullname-end (point-marker)))
286 (goto-char fullname-start)
287 ;; RFC 822 says \ and nonmatching parentheses
288 ;; must be escaped in comments.
289 ;; Escape every instance of ()\ ...
290 (while (re-search-forward "[()\\]" fullname-end 1)
291 (replace-match "\\\\\\&" t))
292 ;; ... then undo escaping of matching parentheses,
293 ;; including matching nested parentheses.
294 (goto-char fullname-start)
295 (while (re-search-forward
296 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
298 (replace-match "\\1(\\3)" t)
299 (goto-char fullname-start))))
301 ((null mail-from-style)
302 (insert "From: " login "\n")))))
303 ;; Insert a `Message-Id:' field if there isn't one yet.
304 (goto-char (point-min))
305 (unless (re-search-forward "^Message-Id:" delimline t)
306 (insert "Message-Id: " (message-make-message-id) "\n"))
307 ;; Insert a `Date:' field if there isn't one yet.
308 (goto-char (point-min))
309 (unless (re-search-forward "^Date:" delimline t)
310 (insert "Date: " (message-make-date) "\n"))
311 ;; Insert an extra newline if we need it to work around
312 ;; Sun's bug that swallows newlines.
313 (goto-char (1+ delimline))
314 (if (eval mail-mailer-swallows-blank-line)
316 ;; Find and handle any FCC fields.
317 (goto-char (point-min))
318 (if (re-search-forward "^FCC:" delimline t)
319 (mail-do-fcc delimline))
321 (with-current-buffer errbuf
326 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
327 (setq smtpmail-recipient-address-list
328 (smtpmail-deduce-address-list tembuf (point-min) delimline))
329 (kill-buffer smtpmail-address-buffer)
331 (smtpmail-do-bcc delimline)
333 (if (not smtpmail-queue-mail)
334 (if (not (null smtpmail-recipient-address-list))
335 (if (not (smtpmail-via-smtp
336 smtpmail-recipient-address-list tembuf))
337 (error "Sending failed; SMTP protocol error"))
338 (error "Sending failed; no recipients"))
339 (let* ((file-data (concat
341 (concat (time-stamp-yyyy-mm-dd)
342 "_" (time-stamp-hh:mm:ss)
344 (setq smtpmail-queue-counter
345 (1+ smtpmail-queue-counter)))))
346 (file-elisp (concat file-data ".el"))
347 (buffer-data (create-file-buffer file-data))
348 (buffer-elisp (create-file-buffer file-elisp))
349 (buffer-scratch "*queue-mail*"))
350 (with-current-buffer buffer-data
352 (insert-buffer tembuf)
353 (write-file file-data)
354 (set-buffer buffer-elisp)
357 "(setq smtpmail-recipient-address-list '"
358 (prin1-to-string smtpmail-recipient-address-list)
360 (write-file file-elisp)
361 (set-buffer (generate-new-buffer buffer-scratch))
362 (insert (concat file-data "\n"))
363 (append-to-file (point-min)
365 smtpmail-queue-index)
367 (kill-buffer buffer-scratch)
368 (kill-buffer buffer-data)
369 (kill-buffer buffer-elisp))))
372 (kill-buffer errbuf)))))
374 (defun smtpmail-send-queued-mail ()
375 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
378 ;;; Get index, get first mail, send it, update index, get second
379 ;;; mail, send it, etc...
381 (insert-file-contents smtpmail-queue-index)
382 (beginning-of-buffer)
384 (setq file-msg (buffer-substring (point) (line-end-position)))
386 ;; Insert the message literally: it is already encoded as per
387 ;; the MIME headers, and code conversions might guess the
390 (let ((coding-system-for-read 'no-conversion))
391 (insert-file-contents file-msg))
392 (if (not (null smtpmail-recipient-address-list))
393 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
395 (error "Sending failed; SMTP protocol error"))
396 (error "Sending failed; no recipients")))
397 (delete-file file-msg)
398 (delete-file (concat file-msg ".el"))
400 (write-region (point-min) (point-max) smtpmail-queue-index))))
402 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
404 (defun smtpmail-fqdn ()
405 (if smtpmail-local-domain
406 (concat (system-name) "." smtpmail-local-domain)
409 (defsubst smtpmail-cred-server (cred)
412 (defsubst smtpmail-cred-port (cred)
415 (defsubst smtpmail-cred-key (cred)
418 (defsubst smtpmail-cred-user (cred)
421 (defsubst smtpmail-cred-cert (cred)
424 (defsubst smtpmail-cred-passwd (cred)
427 (defun smtpmail-find-credentials (cred server port)
430 (while (setq el (pop l))
431 (when (and (equal server (smtpmail-cred-server el))
432 (equal port (smtpmail-cred-port el)))
433 (throw 'done el))))))
435 (defun smtpmail-maybe-append-domain (recipient)
436 (if (or (not smtpmail-sendto-domain)
437 (string-match "@" recipient))
439 (concat recipient "@" smtpmail-sendto-domain)))
441 (defun smtpmail-intersection (list1 list2)
444 (when (memq el2 list1)
448 (defun smtpmail-open-stream (process-buffer host port)
449 (let ((cred (smtpmail-find-credentials
450 smtpmail-starttls-credentials host port)))
451 (if (null (and cred (condition-case ()
452 (call-process "starttls")
455 (open-network-stream "SMTP" process-buffer host port)
456 (let* ((cred-key (smtpmail-cred-key cred))
457 (cred-cert (smtpmail-cred-cert cred))
459 (when (and (stringp cred-key) (stringp cred-cert)
461 (setq cred-key (expand-file-name cred-key)))
463 (setq cred-cert (expand-file-name cred-cert))))
464 (list "--key-file" cred-key "--cert-file" cred-cert))))
465 (starttls-open-stream "SMTP" process-buffer host port)))))
467 (defun smtpmail-try-auth-methods (process supported-extensions host port)
468 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
469 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
470 (cred (if (stringp smtpmail-auth-credentials)
471 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
472 (hostentry (netrc-machine
473 netrc host (format "%s" (or port "smtp"))
477 (netrc-get hostentry "login")
478 (netrc-get hostentry "password"))))
479 (smtpmail-find-credentials
480 smtpmail-auth-credentials host port)))
482 (or (smtpmail-cred-passwd cred)
484 (format "SMTP password for %s:%s: "
485 (smtpmail-cred-server cred)
486 (smtpmail-cred-port cred))))))
491 (smtpmail-send-command process (format "AUTH %s" mech))
492 (if (or (null (car (setq ret (smtpmail-read-response process))))
493 (not (integerp (car ret)))
496 (when (eq (car ret) 334)
497 (let* ((challenge (substring (cadr ret) 4))
498 (decoded (base64-decode-string challenge))
499 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
500 (response (concat (smtpmail-cred-user cred) " " hash))
501 (encoded (base64-encode-string response)))
502 (smtpmail-send-command process (format "%s" encoded))
503 (if (or (null (car (setq ret (smtpmail-read-response process))))
504 (not (integerp (car ret)))
506 (throw 'done nil)))))
508 (smtpmail-send-command process "AUTH LOGIN")
509 (if (or (null (car (setq ret (smtpmail-read-response process))))
510 (not (integerp (car ret)))
513 (smtpmail-send-command
514 process (base64-encode-string (smtpmail-cred-user cred)))
515 (if (or (null (car (setq ret (smtpmail-read-response process))))
516 (not (integerp (car ret)))
519 (smtpmail-send-command process (base64-encode-string passwd))
520 (if (or (null (car (setq ret (smtpmail-read-response process))))
521 (not (integerp (car ret)))
525 (error "Mechanism %s not implemented" mech)))
526 ;; Remember the password.
527 (when (and (not (stringp smtpmail-auth-credentials))
528 (null (smtpmail-cred-passwd cred)))
529 (setcar (cdr (cdr (cdr cred))) passwd)))))
531 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
533 (host (or smtpmail-smtp-server
534 (error "`smtpmail-smtp-server' not defined")))
535 (port smtpmail-smtp-service)
539 (supported-extensions '()))
542 ;; get or create the trace buffer
544 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
546 ;; clear the trace buffer of old output
547 (with-current-buffer process-buffer
550 ;; open the connection to the server
551 (setq process (smtpmail-open-stream process-buffer host port))
552 (and (null process) (throw 'done nil))
554 ;; set the send-filter
555 (set-process-filter process 'smtpmail-process-filter)
557 (with-current-buffer process-buffer
558 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
559 (make-local-variable 'smtpmail-read-point)
560 (setq smtpmail-read-point (point-min))
563 (if (or (null (car (setq greeting (smtpmail-read-response process))))
564 (not (integerp (car greeting)))
565 (>= (car greeting) 400))
573 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
575 (if (or (null (car (setq response-code
576 (smtpmail-read-response process))))
577 (not (integerp (car response-code)))
578 (>= (car response-code) 400))
581 (smtpmail-send-command
582 process (format "HELO %s" (smtpmail-fqdn)))
584 (if (or (null (car (setq response-code
585 (smtpmail-read-response process))))
586 (not (integerp (car response-code)))
587 (>= (car response-code) 400))
589 (dolist (line (cdr (cdr response-code)))
590 (let ((name (mapcar (lambda (s) (intern (downcase s)))
591 (split-string (substring line 4) "[ ]"))))
592 (and (eq (length name) 1)
593 (setq name (car name)))
595 (cond ((memq (if (consp name) (car name) name)
596 '(verb xvrb 8bitmime onex xone
600 auth=login auth starttls))
601 (setq supported-extensions
602 (cons name supported-extensions)))
603 (smtpmail-warn-about-unknown-extensions
604 (message "Unknown extension %s" name)))))))
607 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
608 (member 'starttls supported-extensions)
609 (process-id process))
611 (smtpmail-send-command process (format "STARTTLS"))
612 (if (or (null (car (setq response-code (smtpmail-read-response process))))
613 (not (integerp (car response-code)))
614 (>= (car response-code) 400))
616 (starttls-negotiate process)
617 (setq do-starttls nil))
618 (setq do-ehlo nil))))
620 (smtpmail-try-auth-methods process supported-extensions host port)
622 (if (or (member 'onex supported-extensions)
623 (member 'xone supported-extensions))
625 (smtpmail-send-command process (format "ONEX"))
626 (if (or (null (car (setq response-code (smtpmail-read-response process))))
627 (not (integerp (car response-code)))
628 (>= (car response-code) 400))
631 (if (and smtpmail-debug-verb
632 (or (member 'verb supported-extensions)
633 (member 'xvrb supported-extensions)))
635 (smtpmail-send-command process (format "VERB"))
636 (if (or (null (car (setq response-code (smtpmail-read-response process))))
637 (not (integerp (car response-code)))
638 (>= (car response-code) 400))
641 (if (member 'xusr supported-extensions)
643 (smtpmail-send-command process (format "XUSR"))
644 (if (or (null (car (setq response-code (smtpmail-read-response process))))
645 (not (integerp (car response-code)))
646 (>= (car response-code) 400))
649 ;; MAIL FROM: <sender>
651 (if (or (member 'size supported-extensions)
652 (assoc 'size supported-extensions))
654 (with-current-buffer smtpmail-text-buffer
656 (+ (- (point-max) (point-min))
657 ;; Add one byte for each change-of-line
658 ;; because or CR-LF representation:
659 (count-lines (point-min) (point-max))
660 ;; For some reason, an empty line is
661 ;; added to the message. Maybe this
662 ;; is a bug, but it can't hurt to add
663 ;; those two bytes anyway:
667 (if (member '8bitmime supported-extensions)
669 ;; Code should be added here that transforms
670 ;; the contents of the message buffer into
671 ;; something the receiving SMTP can handle.
672 ;; For a receiver that supports 8BITMIME, this
673 ;; may mean converting BINARY to BASE64, or
674 ;; adding Content-Transfer-Encoding and the
675 ;; other MIME headers. The code should also
676 ;; return an indication of what encoding the
677 ;; message buffer is now, i.e. ASCII or
683 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
684 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
685 (or mail-envelope-from
690 (if (or (null (car (setq response-code (smtpmail-read-response process))))
691 (not (integerp (car response-code)))
692 (>= (car response-code) 400))
696 ;; RCPT TO: <recipient>
698 (while (not (null (nth n recipient)))
699 (smtpmail-send-command process (format "RCPT TO: <%s>" (smtpmail-maybe-append-domain (nth n recipient))))
702 (setq response-code (smtpmail-read-response process))
703 (if (or (null (car response-code))
704 (not (integerp (car response-code)))
705 (>= (car response-code) 400))
711 (smtpmail-send-command process "DATA")
713 (if (or (null (car (setq response-code (smtpmail-read-response process))))
714 (not (integerp (car response-code)))
715 (>= (car response-code) 400))
720 (smtpmail-send-data process smtpmail-text-buffer)
723 (smtpmail-send-command process ".")
725 (if (or (null (car (setq response-code (smtpmail-read-response process))))
726 (not (integerp (car response-code)))
727 (>= (car response-code) 400))
732 ; (smtpmail-send-command process "QUIT")
733 ; (and (null (car (smtpmail-read-response process)))
737 (with-current-buffer (process-buffer process)
738 (smtpmail-send-command process "QUIT")
739 (smtpmail-read-response process)
741 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
742 ; (not (integerp (car response-code)))
743 ; (>= (car response-code) 400))
746 (delete-process process))))))
749 (defun smtpmail-process-filter (process output)
750 (with-current-buffer (process-buffer process)
751 (goto-char (point-max))
754 (defun smtpmail-read-response (process)
755 (let ((case-fold-search nil)
756 (response-strings nil)
757 (response-continue t)
758 (return-value '(nil ()))
761 (while response-continue
762 (goto-char smtpmail-read-point)
763 (while (not (search-forward "\r\n" nil t))
764 (accept-process-output process)
765 (goto-char smtpmail-read-point))
767 (setq match-end (point))
768 (setq response-strings
769 (cons (buffer-substring smtpmail-read-point (- match-end 2))
772 (goto-char smtpmail-read-point)
773 (if (looking-at "[0-9]+ ")
774 (let ((begin (match-beginning 0))
776 (if smtpmail-debug-info
777 (message "%s" (car response-strings)))
779 (setq smtpmail-read-point match-end)
781 ;; ignore lines that start with "0"
782 (if (looking-at "0[0-9]+ ")
784 (setq response-continue nil)
787 (buffer-substring begin end))
788 (nreverse response-strings)))))
790 (if (looking-at "[0-9]+-")
791 (progn (if smtpmail-debug-info
792 (message "%s" (car response-strings)))
793 (setq smtpmail-read-point match-end)
794 (setq response-continue t))
796 (setq smtpmail-read-point match-end)
797 (setq response-continue nil)
799 (cons nil (nreverse response-strings)))
802 (setq smtpmail-read-point match-end)
806 (defun smtpmail-send-command (process command)
807 (goto-char (point-max))
808 (if (= (aref command 0) ?P)
809 (insert "PASS <omitted>\r\n")
810 (insert command "\r\n"))
811 (setq smtpmail-read-point (point))
812 (process-send-string process command)
813 (process-send-string process "\r\n"))
815 (defun smtpmail-send-data-1 (process data)
816 (goto-char (point-max))
818 (if (and (multibyte-string-p data)
819 smtpmail-code-conv-from)
820 (setq data (string-as-multibyte
821 (encode-coding-string data smtpmail-code-conv-from))))
823 (if smtpmail-debug-info
824 (insert data "\r\n"))
826 (setq smtpmail-read-point (point))
827 ;; Escape "." at start of a line
828 (if (eq (string-to-char data) ?.)
829 (process-send-string process "."))
830 (process-send-string process data)
831 (process-send-string process "\r\n")
834 (defun smtpmail-send-data (process buffer)
841 (with-current-buffer buffer
842 (goto-char (point-min)))
845 (with-current-buffer buffer
847 (setq this-line (point))
849 (setq this-line-end (point))
850 (setq sending-data nil)
851 (setq sending-data (buffer-substring this-line this-line-end))
852 (if (/= (forward-line 1) 0)
853 (setq data-continue nil)))
855 (smtpmail-send-data-1 process sending-data)
861 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
862 "Get address list suitable for smtp RCPT TO: <address>."
864 (with-current-buffer smtpmail-address-buffer
867 ((case-fold-search t)
868 (simple-address-list "")
872 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
873 (goto-char (point-min))
874 ;; RESENT-* fields should stop processing of regular fields.
877 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
879 "^Resent-\\(to\\|cc\\|bcc\\):"
880 "^\\(To:\\|Cc:\\|Bcc:\\)")))
882 (while (re-search-forward addr-regexp header-end t)
884 (setq this-line (match-beginning 0))
886 ;; get any continuation lines
887 (while (and (looking-at "^[ \t]+") (< (point) header-end))
889 (setq this-line-end (point-marker))
890 (setq simple-address-list
891 (concat simple-address-list " "
892 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
895 (insert " " simple-address-list "\n")
896 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
897 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
898 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
900 (goto-char (point-min))
901 ;; tidyness in case hook is not robust when it looks at this
902 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
904 (goto-char (point-min))
905 (let (recipient-address-list)
906 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
908 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
909 recipient-address-list))
911 (setq smtpmail-recipient-address-list recipient-address-list))
919 (defun smtpmail-do-bcc (header-end)
920 "Delete [Resent-]BCC: and their continuation lines from the header area.
921 There may be multiple BCC: lines, and each may have arbitrarily
922 many continuation lines."
923 (let ((case-fold-search t))
925 (goto-char (point-min))
926 ;; iterate over all BCC: lines
927 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
928 (delete-region (match-beginning 0)
929 (progn (forward-line 1) (point)))
930 ;; get rid of any continuation lines
931 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
932 (replace-match ""))))))
937 ;;; smtpmail.el ends here