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")
77 (autoload 'netrc-machine "netrc")
78 (autoload 'netrc-get "netrc")
81 (defgroup smtpmail nil
82 "SMTP protocol for sending mail."
86 (defcustom smtpmail-default-smtp-server nil
87 "*Specify default SMTP server."
88 :type '(choice (const nil) string)
91 (defcustom smtpmail-smtp-server
92 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
93 "*The name of the host running SMTP server."
94 :type '(choice (const nil) string)
97 (defcustom smtpmail-smtp-service 25
98 "*SMTP service port number.
99 The default value would be \"smtp\" or 25 ."
100 :type '(choice (integer :tag "Port") (string :tag "Service"))
103 (defcustom smtpmail-local-domain nil
104 "*Local domain name without a host name.
105 If the function (system-name) returns the full internet address,
106 don't define this value."
107 :type '(choice (const nil) string)
110 (defcustom smtpmail-sendto-domain nil
111 "*Local domain name without a host name.
112 This is appended (with an @-sign) to any specified recipients which do
113 not include an @-sign, so that each RCPT TO address is fully qualified.
114 \(Some configurations of sendmail require this.)
116 Don't bother to set this unless you have get an error like:
117 Sending failed; SMTP protocol error
118 when sending mail, and the *trace of SMTP session to <somewhere>*
119 buffer includes an exchange like:
121 501 <someone>: recipient address must contain a domain
123 :type '(choice (const nil) string)
126 (defcustom smtpmail-debug-info nil
127 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
128 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
133 (defcustom smtpmail-debug-verb nil
134 "Whether this library sends the SMTP VERB command or not.
135 The commands enables verbose information from the SMTP server."
139 (defcustom smtpmail-code-conv-from nil ;; *junet*
140 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
144 (defcustom smtpmail-queue-mail nil
145 "*Specify if mail is queued (if t) or sent immediately (if nil).
146 If queued, it is stored in the directory `smtpmail-queue-dir'
147 and sent with `smtpmail-send-queued-mail'."
151 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
152 "*Directory where `smtpmail.el' stores queued mail."
156 (defcustom smtpmail-auth-credentials "~/.authinfo"
157 "Specify username and password for servers, directly or via .netrc file.
158 This variable can either be a filename pointing to a file in netrc(5)
159 format, or list of four-element lists that contain, in order,
160 `servername' (a string), `port' (an integer), `user' (a string) and
161 `password' (a string, or nil to query the user when needed). If you
162 need to enter a `realm' too, add it to the user string, so that it
163 looks like `user@realm'."
165 (repeat (list (string :tag "Server")
166 (integer :tag "Port")
167 (string :tag "Username")
168 (choice (const :tag "Query when needed" nil)
169 (string :tag "Password")))))
173 (defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
174 "Specify STARTTLS keys and certificates for servers.
175 This is a list of four-element list with `servername' (a string),
176 `port' (an integer), `key' (a filename) and `certificate' (a filename)."
177 :type '(repeat (list (string :tag "Server")
178 (integer :tag "Port")
180 (file :tag "Certificate")))
184 (defcustom smtpmail-warn-about-unknown-extensions nil
185 "*If set, print warnings about unknown SMTP extensions.
186 This is mainly useful for development purposes, to learn about
187 new SMTP extensions that might be useful to support."
192 (defvar smtpmail-queue-index-file "index"
193 "File name of queued mail index,
194 This is relative to `smtpmail-queue-dir'.")
196 (defvar smtpmail-address-buffer)
197 (defvar smtpmail-recipient-address-list)
199 (defvar smtpmail-queue-counter 0)
201 ;; Buffer-local variable.
202 (defvar smtpmail-read-point)
204 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
205 smtpmail-queue-index-file))
207 (defconst smtpmail-auth-supported '(cram-md5 login)
208 "List of supported SMTP AUTH mechanisms.")
214 (defvar smtpmail-mail-address nil
215 "Value of `user-mail-address' in ambient buffer.")
218 (defun smtpmail-send-it ()
219 (let ((errbuf (if mail-interactive
220 (generate-new-buffer " smtpmail errors")
222 (tembuf (generate-new-buffer " smtpmail temp"))
223 (case-fold-search nil)
225 (mailbuf (current-buffer))
226 (smtpmail-mail-address user-mail-address)
227 (smtpmail-code-conv-from
228 (if enable-multibyte-characters
229 (let ((sendmail-coding-system smtpmail-code-conv-from))
230 (select-message-coding-system)))))
235 (insert-buffer-substring mailbuf)
236 (goto-char (point-max))
237 ;; require one newline at the end.
238 (or (= (preceding-char) ?\n)
240 ;; Change header-delimiter to be what sendmail expects.
241 (mail-sendmail-undelimit-header)
242 (setq delimline (point-marker))
243 ;; (sendmail-synch-aliases)
245 (expand-mail-aliases (point-min) delimline))
246 (goto-char (point-min))
247 ;; ignore any blank lines in the header
248 (while (and (re-search-forward "\n\n\n*" delimline t)
249 (< (point) delimline))
250 (replace-match "\n"))
251 (let ((case-fold-search t))
252 ;; We used to process Resent-... headers here,
253 ;; but it was not done properly, and the job
254 ;; is done correctly in smtpmail-deduce-address-list.
255 ;; Don't send out a blank subject line
256 (goto-char (point-min))
257 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
259 ;; This one matches a Subject just before the header delimiter.
260 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
261 (= (match-end 0) delimline))
263 ;; Put the "From:" field in unless for some odd reason
264 ;; they put one in themselves.
265 (goto-char (point-min))
266 (if (not (re-search-forward "^From:" delimline t))
267 (let* ((login smtpmail-mail-address)
268 (fullname (user-full-name)))
269 (cond ((eq mail-from-style 'angles)
270 (insert "From: " fullname)
271 (let ((fullname-start (+ (point-min) 6))
272 (fullname-end (point-marker)))
273 (goto-char fullname-start)
274 ;; Look for a character that cannot appear unquoted
275 ;; according to RFC 822.
276 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
279 ;; Quote fullname, escaping specials.
280 (goto-char fullname-start)
282 (while (re-search-forward "[\"\\]"
284 (replace-match "\\\\\\&" t))
286 (insert " <" login ">\n"))
287 ((eq mail-from-style 'parens)
288 (insert "From: " login " (")
289 (let ((fullname-start (point)))
291 (let ((fullname-end (point-marker)))
292 (goto-char fullname-start)
293 ;; RFC 822 says \ and nonmatching parentheses
294 ;; must be escaped in comments.
295 ;; Escape every instance of ()\ ...
296 (while (re-search-forward "[()\\]" fullname-end 1)
297 (replace-match "\\\\\\&" t))
298 ;; ... then undo escaping of matching parentheses,
299 ;; including matching nested parentheses.
300 (goto-char fullname-start)
301 (while (re-search-forward
302 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
304 (replace-match "\\1(\\3)" t)
305 (goto-char fullname-start))))
307 ((null mail-from-style)
308 (insert "From: " login "\n")))))
309 ;; Insert a `Message-Id:' field if there isn't one yet.
310 (goto-char (point-min))
311 (unless (re-search-forward "^Message-Id:" delimline t)
312 (insert "Message-Id: " (message-make-message-id) "\n"))
313 ;; Insert a `Date:' field if there isn't one yet.
314 (goto-char (point-min))
315 (unless (re-search-forward "^Date:" delimline t)
316 (insert "Date: " (message-make-date) "\n"))
317 ;; Insert an extra newline if we need it to work around
318 ;; Sun's bug that swallows newlines.
319 (goto-char (1+ delimline))
320 (if (eval mail-mailer-swallows-blank-line)
322 ;; Find and handle any FCC fields.
323 (goto-char (point-min))
324 (if (re-search-forward "^FCC:" delimline t)
325 (mail-do-fcc delimline))
327 (with-current-buffer errbuf
332 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
333 (setq smtpmail-recipient-address-list
334 (smtpmail-deduce-address-list tembuf (point-min) delimline))
335 (kill-buffer smtpmail-address-buffer)
337 (smtpmail-do-bcc delimline)
339 (if (not smtpmail-queue-mail)
340 (if (not (null smtpmail-recipient-address-list))
341 (if (not (smtpmail-via-smtp
342 smtpmail-recipient-address-list tembuf))
343 (error "Sending failed; SMTP protocol error"))
344 (error "Sending failed; no recipients"))
345 (let* ((file-data (concat
347 (concat (time-stamp-yyyy-mm-dd)
348 "_" (time-stamp-hh:mm:ss)
350 (setq smtpmail-queue-counter
351 (1+ smtpmail-queue-counter)))))
352 (file-elisp (concat file-data ".el"))
353 (buffer-data (create-file-buffer file-data))
354 (buffer-elisp (create-file-buffer file-elisp))
355 (buffer-scratch "*queue-mail*"))
356 (with-current-buffer buffer-data
358 (insert-buffer tembuf)
359 (write-file file-data)
360 (set-buffer buffer-elisp)
363 "(setq smtpmail-recipient-address-list '"
364 (prin1-to-string smtpmail-recipient-address-list)
366 (write-file file-elisp)
367 (set-buffer (generate-new-buffer buffer-scratch))
368 (insert (concat file-data "\n"))
369 (append-to-file (point-min)
371 smtpmail-queue-index)
373 (kill-buffer buffer-scratch)
374 (kill-buffer buffer-data)
375 (kill-buffer buffer-elisp))))
378 (kill-buffer errbuf)))))
380 (defun smtpmail-send-queued-mail ()
381 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
384 ;;; Get index, get first mail, send it, update index, get second
385 ;;; mail, send it, etc...
387 (insert-file-contents smtpmail-queue-index)
388 (beginning-of-buffer)
390 (setq file-msg (buffer-substring (point) (line-end-position)))
392 ;; Insert the message literally: it is already encoded as per
393 ;; the MIME headers, and code conversions might guess the
396 (let ((coding-system-for-read 'no-conversion))
397 (insert-file-contents file-msg))
398 (if (not (null smtpmail-recipient-address-list))
399 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
401 (error "Sending failed; SMTP protocol error"))
402 (error "Sending failed; no recipients")))
403 (delete-file file-msg)
404 (delete-file (concat file-msg ".el"))
406 (write-region (point-min) (point-max) smtpmail-queue-index))))
408 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
410 (defun smtpmail-fqdn ()
411 (if smtpmail-local-domain
412 (concat (system-name) "." smtpmail-local-domain)
415 (defsubst smtpmail-cred-server (cred)
418 (defsubst smtpmail-cred-port (cred)
421 (defsubst smtpmail-cred-key (cred)
424 (defsubst smtpmail-cred-user (cred)
427 (defsubst smtpmail-cred-cert (cred)
430 (defsubst smtpmail-cred-passwd (cred)
433 (defun smtpmail-find-credentials (cred server port)
436 (while (setq el (pop l))
437 (when (and (equal server (smtpmail-cred-server el))
438 (equal port (smtpmail-cred-port el)))
439 (throw 'done el))))))
441 (defun smtpmail-maybe-append-domain (recipient)
442 (if (or (not smtpmail-sendto-domain)
443 (string-match "@" recipient))
445 (concat recipient "@" smtpmail-sendto-domain)))
447 (defun smtpmail-intersection (list1 list2)
450 (when (memq el2 list1)
454 (defun smtpmail-open-stream (process-buffer host port)
455 (let ((cred (smtpmail-find-credentials
456 smtpmail-starttls-credentials host port)))
457 (if (null (and cred (condition-case ()
458 (call-process "starttls")
461 (open-network-stream "SMTP" process-buffer host port)
462 (let* ((cred-key (smtpmail-cred-key cred))
463 (cred-cert (smtpmail-cred-cert cred))
465 (when (and (stringp cred-key) (stringp cred-cert)
467 (setq cred-key (expand-file-name cred-key)))
469 (setq cred-cert (expand-file-name cred-cert))))
470 (list "--key-file" cred-key "--cert-file" cred-cert))))
471 (starttls-open-stream "SMTP" process-buffer host port)))))
473 (defun smtpmail-try-auth-methods (process supported-extensions host port)
474 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
475 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
476 (cred (if (stringp smtpmail-auth-credentials)
477 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
478 (hostentry (netrc-machine
479 netrc host (format "%s" (or port "smtp"))
483 (netrc-get hostentry "login")
484 (netrc-get hostentry "password"))))
485 (smtpmail-find-credentials
486 smtpmail-auth-credentials host port)))
488 (or (smtpmail-cred-passwd cred)
490 (format "SMTP password for %s:%s: "
491 (smtpmail-cred-server cred)
492 (smtpmail-cred-port cred))))))
497 (smtpmail-send-command process (format "AUTH %s" mech))
498 (if (or (null (car (setq ret (smtpmail-read-response process))))
499 (not (integerp (car ret)))
502 (when (eq (car ret) 334)
503 (let* ((challenge (substring (cadr ret) 4))
504 (decoded (base64-decode-string challenge))
505 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
506 (response (concat (smtpmail-cred-user cred) " " hash))
507 (encoded (base64-encode-string response)))
508 (smtpmail-send-command process (format "%s" encoded))
509 (if (or (null (car (setq ret (smtpmail-read-response process))))
510 (not (integerp (car ret)))
512 (throw 'done nil)))))
514 (smtpmail-send-command process "AUTH LOGIN")
515 (if (or (null (car (setq ret (smtpmail-read-response process))))
516 (not (integerp (car ret)))
519 (smtpmail-send-command
520 process (base64-encode-string (smtpmail-cred-user cred)))
521 (if (or (null (car (setq ret (smtpmail-read-response process))))
522 (not (integerp (car ret)))
525 (smtpmail-send-command process (base64-encode-string passwd))
526 (if (or (null (car (setq ret (smtpmail-read-response process))))
527 (not (integerp (car ret)))
531 (error "Mechanism %s not implemented" mech)))
532 ;; Remember the password.
533 (when (and (not (stringp smtpmail-auth-credentials))
534 (null (smtpmail-cred-passwd cred)))
535 (setcar (cdr (cdr (cdr cred))) passwd)))))
537 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
539 (host (or smtpmail-smtp-server
540 (error "`smtpmail-smtp-server' not defined")))
541 (port smtpmail-smtp-service)
545 (supported-extensions '()))
548 ;; get or create the trace buffer
550 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
552 ;; clear the trace buffer of old output
553 (with-current-buffer process-buffer
556 ;; open the connection to the server
557 (setq process (smtpmail-open-stream process-buffer host port))
558 (and (null process) (throw 'done nil))
560 ;; set the send-filter
561 (set-process-filter process 'smtpmail-process-filter)
563 (with-current-buffer process-buffer
564 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
565 (make-local-variable 'smtpmail-read-point)
566 (setq smtpmail-read-point (point-min))
569 (if (or (null (car (setq greeting (smtpmail-read-response process))))
570 (not (integerp (car greeting)))
571 (>= (car greeting) 400))
579 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
581 (if (or (null (car (setq response-code
582 (smtpmail-read-response process))))
583 (not (integerp (car response-code)))
584 (>= (car response-code) 400))
587 (smtpmail-send-command
588 process (format "HELO %s" (smtpmail-fqdn)))
590 (if (or (null (car (setq response-code
591 (smtpmail-read-response process))))
592 (not (integerp (car response-code)))
593 (>= (car response-code) 400))
595 (dolist (line (cdr (cdr response-code)))
596 (let ((name (mapcar (lambda (s) (intern (downcase s)))
597 (split-string (substring line 4) "[ ]"))))
598 (and (eq (length name) 1)
599 (setq name (car name)))
601 (cond ((memq (if (consp name) (car name) name)
602 '(verb xvrb 8bitmime onex xone
606 auth=login auth starttls))
607 (setq supported-extensions
608 (cons name supported-extensions)))
609 (smtpmail-warn-about-unknown-extensions
610 (message "Unknown extension %s" name)))))))
613 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
614 (member 'starttls supported-extensions)
615 (process-id process))
617 (smtpmail-send-command process (format "STARTTLS"))
618 (if (or (null (car (setq response-code (smtpmail-read-response process))))
619 (not (integerp (car response-code)))
620 (>= (car response-code) 400))
622 (starttls-negotiate process)
623 (setq do-starttls nil))
624 (setq do-ehlo nil))))
626 (smtpmail-try-auth-methods process supported-extensions host port)
628 (if (or (member 'onex supported-extensions)
629 (member 'xone supported-extensions))
631 (smtpmail-send-command process (format "ONEX"))
632 (if (or (null (car (setq response-code (smtpmail-read-response process))))
633 (not (integerp (car response-code)))
634 (>= (car response-code) 400))
637 (if (and smtpmail-debug-verb
638 (or (member 'verb supported-extensions)
639 (member 'xvrb supported-extensions)))
641 (smtpmail-send-command process (format "VERB"))
642 (if (or (null (car (setq response-code (smtpmail-read-response process))))
643 (not (integerp (car response-code)))
644 (>= (car response-code) 400))
647 (if (member 'xusr supported-extensions)
649 (smtpmail-send-command process (format "XUSR"))
650 (if (or (null (car (setq response-code (smtpmail-read-response process))))
651 (not (integerp (car response-code)))
652 (>= (car response-code) 400))
655 ;; MAIL FROM: <sender>
657 (if (or (member 'size supported-extensions)
658 (assoc 'size supported-extensions))
660 (with-current-buffer smtpmail-text-buffer
662 (+ (- (point-max) (point-min))
663 ;; Add one byte for each change-of-line
664 ;; because or CR-LF representation:
665 (count-lines (point-min) (point-max))
666 ;; For some reason, an empty line is
667 ;; added to the message. Maybe this
668 ;; is a bug, but it can't hurt to add
669 ;; those two bytes anyway:
673 (if (member '8bitmime supported-extensions)
675 ;; Code should be added here that transforms
676 ;; the contents of the message buffer into
677 ;; something the receiving SMTP can handle.
678 ;; For a receiver that supports 8BITMIME, this
679 ;; may mean converting BINARY to BASE64, or
680 ;; adding Content-Transfer-Encoding and the
681 ;; other MIME headers. The code should also
682 ;; return an indication of what encoding the
683 ;; message buffer is now, i.e. ASCII or
689 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
690 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
691 (or mail-envelope-from
692 smtpmail-mail-address)
696 (if (or (null (car (setq response-code (smtpmail-read-response process))))
697 (not (integerp (car response-code)))
698 (>= (car response-code) 400))
702 ;; RCPT TO: <recipient>
704 (while (not (null (nth n recipient)))
705 (smtpmail-send-command process (format "RCPT TO: <%s>" (smtpmail-maybe-append-domain (nth n recipient))))
708 (setq response-code (smtpmail-read-response process))
709 (if (or (null (car response-code))
710 (not (integerp (car response-code)))
711 (>= (car response-code) 400))
717 (smtpmail-send-command process "DATA")
719 (if (or (null (car (setq response-code (smtpmail-read-response process))))
720 (not (integerp (car response-code)))
721 (>= (car response-code) 400))
726 (smtpmail-send-data process smtpmail-text-buffer)
729 (smtpmail-send-command process ".")
731 (if (or (null (car (setq response-code (smtpmail-read-response process))))
732 (not (integerp (car response-code)))
733 (>= (car response-code) 400))
738 ; (smtpmail-send-command process "QUIT")
739 ; (and (null (car (smtpmail-read-response process)))
743 (with-current-buffer (process-buffer process)
744 (smtpmail-send-command process "QUIT")
745 (smtpmail-read-response process)
747 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
748 ; (not (integerp (car response-code)))
749 ; (>= (car response-code) 400))
752 (delete-process process)
753 (unless smtpmail-debug-info
754 (kill-buffer process-buffer)))))))
757 (defun smtpmail-process-filter (process output)
758 (with-current-buffer (process-buffer process)
759 (goto-char (point-max))
762 (defun smtpmail-read-response (process)
763 (let ((case-fold-search nil)
764 (response-strings nil)
765 (response-continue t)
766 (return-value '(nil ()))
769 (while response-continue
770 (goto-char smtpmail-read-point)
771 (while (not (search-forward "\r\n" nil t))
772 (accept-process-output process)
773 (goto-char smtpmail-read-point))
775 (setq match-end (point))
776 (setq response-strings
777 (cons (buffer-substring smtpmail-read-point (- match-end 2))
780 (goto-char smtpmail-read-point)
781 (if (looking-at "[0-9]+ ")
782 (let ((begin (match-beginning 0))
784 (if smtpmail-debug-info
785 (message "%s" (car response-strings)))
787 (setq smtpmail-read-point match-end)
789 ;; ignore lines that start with "0"
790 (if (looking-at "0[0-9]+ ")
792 (setq response-continue nil)
795 (buffer-substring begin end))
796 (nreverse response-strings)))))
798 (if (looking-at "[0-9]+-")
799 (progn (if smtpmail-debug-info
800 (message "%s" (car response-strings)))
801 (setq smtpmail-read-point match-end)
802 (setq response-continue t))
804 (setq smtpmail-read-point match-end)
805 (setq response-continue nil)
807 (cons nil (nreverse response-strings)))
810 (setq smtpmail-read-point match-end)
814 (defun smtpmail-send-command (process command)
815 (goto-char (point-max))
816 (if (= (aref command 0) ?P)
817 (insert "PASS <omitted>\r\n")
818 (insert command "\r\n"))
819 (setq smtpmail-read-point (point))
820 (process-send-string process command)
821 (process-send-string process "\r\n"))
823 (defun smtpmail-send-data-1 (process data)
824 (goto-char (point-max))
826 (if (and (multibyte-string-p data)
827 smtpmail-code-conv-from)
828 (setq data (string-as-multibyte
829 (encode-coding-string data smtpmail-code-conv-from))))
831 (if smtpmail-debug-info
832 (insert data "\r\n"))
834 (setq smtpmail-read-point (point))
835 ;; Escape "." at start of a line
836 (if (eq (string-to-char data) ?.)
837 (process-send-string process "."))
838 (process-send-string process data)
839 (process-send-string process "\r\n")
842 (defun smtpmail-send-data (process buffer)
849 (with-current-buffer buffer
850 (goto-char (point-min)))
853 (with-current-buffer buffer
855 (setq this-line (point))
857 (setq this-line-end (point))
858 (setq sending-data nil)
859 (setq sending-data (buffer-substring this-line this-line-end))
860 (if (/= (forward-line 1) 0)
861 (setq data-continue nil)))
863 (smtpmail-send-data-1 process sending-data)
869 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
870 "Get address list suitable for smtp RCPT TO: <address>."
872 (with-current-buffer smtpmail-address-buffer
875 ((case-fold-search t)
876 (simple-address-list "")
880 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
881 (goto-char (point-min))
882 ;; RESENT-* fields should stop processing of regular fields.
885 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
887 "^Resent-\\(to\\|cc\\|bcc\\):"
888 "^\\(To:\\|Cc:\\|Bcc:\\)")))
890 (while (re-search-forward addr-regexp header-end t)
892 (setq this-line (match-beginning 0))
894 ;; get any continuation lines
895 (while (and (looking-at "^[ \t]+") (< (point) header-end))
897 (setq this-line-end (point-marker))
898 (setq simple-address-list
899 (concat simple-address-list " "
900 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
903 (insert " " simple-address-list "\n")
904 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
905 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
906 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
908 (goto-char (point-min))
909 ;; tidyness in case hook is not robust when it looks at this
910 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
912 (goto-char (point-min))
913 (let (recipient-address-list)
914 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
916 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
917 recipient-address-list))
919 (setq smtpmail-recipient-address-list recipient-address-list))
927 (defun smtpmail-do-bcc (header-end)
928 "Delete [Resent-]BCC: and their continuation lines from the header area.
929 There may be multiple BCC: lines, and each may have arbitrarily
930 many continuation lines."
931 (let ((case-fold-search t))
933 (goto-char (point-min))
934 ;; iterate over all BCC: lines
935 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
936 (delete-region (match-beginning 0)
937 (progn (forward-line 1) (point)))
938 ;; get rid of any continuation lines
939 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
940 (replace-match ""))))))
945 ;;; smtpmail.el ends here