1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995, 1996, 2001, 2002, 2003 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
69 (autoload 'starttls-open-stream
"starttls")
70 (autoload 'starttls-negotiate
"starttls")
71 (autoload 'mail-strip-quoted-names
"mail-utils")
72 (autoload 'message-make-date
"message")
73 (autoload 'message-make-message-id
"message")
74 (autoload 'rfc2104-hash
"rfc2104")
75 (autoload 'netrc-parse
"netrc")
76 (autoload 'netrc-machine
"netrc")
77 (autoload 'netrc-get
"netrc")
80 (defgroup smtpmail nil
81 "SMTP protocol for sending mail."
85 (defcustom smtpmail-default-smtp-server nil
86 "*Specify default SMTP server.
87 This only has effect if you specify it before loading the smtpmail library."
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"))
348 (format-time-string "%Y-%m-%d_%H:%M:%S")
349 (setq smtpmail-queue-counter
350 (1+ smtpmail-queue-counter
)))
352 (file-data (convert-standard-filename file-data
))
353 (file-elisp (concat file-data
".el"))
354 (buffer-data (create-file-buffer file-data
))
355 (buffer-elisp (create-file-buffer file-elisp
))
356 (buffer-scratch "*queue-mail*"))
357 (with-current-buffer buffer-data
359 (insert-buffer tembuf
)
360 (write-file file-data
)
361 (set-buffer buffer-elisp
)
364 "(setq smtpmail-recipient-address-list '"
365 (prin1-to-string smtpmail-recipient-address-list
)
367 (write-file file-elisp
)
368 (set-buffer (generate-new-buffer buffer-scratch
))
369 (insert (concat file-data
"\n"))
370 (append-to-file (point-min)
372 smtpmail-queue-index
)
374 (kill-buffer buffer-scratch
)
375 (kill-buffer buffer-data
)
376 (kill-buffer buffer-elisp
))))
379 (kill-buffer errbuf
)))))
382 (defun smtpmail-send-queued-mail ()
383 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
386 ;;; Get index, get first mail, send it, update index, get second
387 ;;; mail, send it, etc...
389 (insert-file-contents smtpmail-queue-index
)
390 (beginning-of-buffer)
392 (setq file-msg
(buffer-substring (point) (line-end-position)))
394 ;; Insert the message literally: it is already encoded as per
395 ;; the MIME headers, and code conversions might guess the
398 (let ((coding-system-for-read 'no-conversion
))
399 (insert-file-contents file-msg
))
400 (if (not (null smtpmail-recipient-address-list
))
401 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
403 (error "Sending failed; SMTP protocol error"))
404 (error "Sending failed; no recipients")))
405 (delete-file file-msg
)
406 (delete-file (concat file-msg
".el"))
407 (delete-region (point-at-bol) (point-at-bol 2)))
408 (write-region (point-min) (point-max) smtpmail-queue-index
))))
410 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
412 (defun smtpmail-fqdn ()
413 (if smtpmail-local-domain
414 (concat (system-name) "." smtpmail-local-domain
)
417 (defsubst smtpmail-cred-server
(cred)
420 (defsubst smtpmail-cred-port
(cred)
423 (defsubst smtpmail-cred-key
(cred)
426 (defsubst smtpmail-cred-user
(cred)
429 (defsubst smtpmail-cred-cert
(cred)
432 (defsubst smtpmail-cred-passwd
(cred)
435 (defun smtpmail-find-credentials (cred server port
)
438 (while (setq el
(pop l
))
439 (when (and (equal server
(smtpmail-cred-server el
))
440 (equal port
(smtpmail-cred-port el
)))
441 (throw 'done el
))))))
443 (defun smtpmail-maybe-append-domain (recipient)
444 (if (or (not smtpmail-sendto-domain
)
445 (string-match "@" recipient
))
447 (concat recipient
"@" smtpmail-sendto-domain
)))
449 (defun smtpmail-intersection (list1 list2
)
452 (when (memq el2 list1
)
456 (defun smtpmail-open-stream (process-buffer host port
)
457 (let ((cred (smtpmail-find-credentials
458 smtpmail-starttls-credentials host port
)))
459 (if (null (and cred
(condition-case ()
462 (call-process starttls-program
))
465 (open-network-stream "SMTP" process-buffer host port
)
466 (let* ((cred-key (smtpmail-cred-key cred
))
467 (cred-cert (smtpmail-cred-cert cred
))
469 (when (and (stringp cred-key
) (stringp cred-cert
)
471 (setq cred-key
(expand-file-name cred-key
)))
473 (setq cred-cert
(expand-file-name cred-cert
))))
474 (list "--key-file" cred-key
"--cert-file" cred-cert
))))
475 (starttls-open-stream "SMTP" process-buffer host port
)))))
477 (defun smtpmail-try-auth-methods (process supported-extensions host port
)
478 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions
)))
479 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs
)))
480 (cred (if (stringp smtpmail-auth-credentials
)
481 (let* ((netrc (netrc-parse smtpmail-auth-credentials
))
482 (hostentry (netrc-machine
483 netrc host
(format "%s" (or port
"smtp"))
487 (netrc-get hostentry
"login")
488 (netrc-get hostentry
"password"))))
489 (smtpmail-find-credentials
490 smtpmail-auth-credentials host port
)))
492 (or (smtpmail-cred-passwd cred
)
494 (format "SMTP password for %s:%s: "
495 (smtpmail-cred-server cred
)
496 (smtpmail-cred-port cred
))))))
501 (smtpmail-send-command process
(format "AUTH %s" mech
))
502 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
503 (not (integerp (car ret
)))
506 (when (eq (car ret
) 334)
507 (let* ((challenge (substring (cadr ret
) 4))
508 (decoded (base64-decode-string challenge
))
509 (hash (rfc2104-hash 'md5
64 16 passwd decoded
))
510 (response (concat (smtpmail-cred-user cred
) " " hash
))
511 (encoded (base64-encode-string response
)))
512 (smtpmail-send-command process
(format "%s" encoded
))
513 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
514 (not (integerp (car ret
)))
516 (throw 'done nil
)))))
518 (smtpmail-send-command process
"AUTH LOGIN")
519 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
520 (not (integerp (car ret
)))
523 (smtpmail-send-command
524 process
(base64-encode-string (smtpmail-cred-user cred
)))
525 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
526 (not (integerp (car ret
)))
529 (smtpmail-send-command process
(base64-encode-string passwd
))
530 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
531 (not (integerp (car ret
)))
535 (error "Mechanism %s not implemented" mech
)))
536 ;; Remember the password.
537 (when (and (not (stringp smtpmail-auth-credentials
))
538 (null (smtpmail-cred-passwd cred
)))
539 (setcar (cdr (cdr (cdr cred
))) passwd
)))))
541 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
)
543 (host (or smtpmail-smtp-server
544 (error "`smtpmail-smtp-server' not defined")))
545 (port smtpmail-smtp-service
)
546 (envelope-from (or (mail-envelope-from)
547 smtpmail-mail-address
552 (supported-extensions '()))
555 ;; get or create the trace buffer
557 (get-buffer-create (format "*trace of SMTP session to %s*" host
)))
559 ;; clear the trace buffer of old output
560 (with-current-buffer process-buffer
563 ;; open the connection to the server
564 (setq process
(smtpmail-open-stream process-buffer host port
))
565 (and (null process
) (throw 'done nil
))
567 ;; set the send-filter
568 (set-process-filter process
'smtpmail-process-filter
)
570 (with-current-buffer process-buffer
571 (set-buffer-process-coding-system 'raw-text-unix
'raw-text-unix
)
572 (make-local-variable 'smtpmail-read-point
)
573 (setq smtpmail-read-point
(point-min))
576 (if (or (null (car (setq greeting
(smtpmail-read-response process
))))
577 (not (integerp (car greeting
)))
578 (>= (car greeting
) 400))
586 (smtpmail-send-command process
(format "EHLO %s" (smtpmail-fqdn)))
588 (if (or (null (car (setq response-code
589 (smtpmail-read-response process
))))
590 (not (integerp (car response-code
)))
591 (>= (car response-code
) 400))
594 (smtpmail-send-command
595 process
(format "HELO %s" (smtpmail-fqdn)))
597 (if (or (null (car (setq response-code
598 (smtpmail-read-response process
))))
599 (not (integerp (car response-code
)))
600 (>= (car response-code
) 400))
602 (dolist (line (cdr (cdr response-code
)))
603 (let ((name (mapcar (lambda (s) (intern (downcase s
)))
604 (split-string (substring line
4) "[ ]"))))
605 (and (eq (length name
) 1)
606 (setq name
(car name
)))
608 (cond ((memq (if (consp name
) (car name
) name
)
609 '(verb xvrb
8bitmime onex xone
613 auth
=login auth starttls
))
614 (setq supported-extensions
615 (cons name supported-extensions
)))
616 (smtpmail-warn-about-unknown-extensions
617 (message "Unknown extension %s" name
)))))))
620 (smtpmail-find-credentials smtpmail-starttls-credentials host port
)
621 (member 'starttls supported-extensions
)
622 (numberp (process-id process
)))
624 (smtpmail-send-command process
(format "STARTTLS"))
625 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
626 (not (integerp (car response-code
)))
627 (>= (car response-code
) 400))
629 (starttls-negotiate process
)
630 (setq do-starttls nil
))
631 (setq do-ehlo nil
))))
633 (smtpmail-try-auth-methods process supported-extensions host port
)
635 (if (or (member 'onex supported-extensions
)
636 (member 'xone supported-extensions
))
638 (smtpmail-send-command process
(format "ONEX"))
639 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
640 (not (integerp (car response-code
)))
641 (>= (car response-code
) 400))
644 (if (and smtpmail-debug-verb
645 (or (member 'verb supported-extensions
)
646 (member 'xvrb supported-extensions
)))
648 (smtpmail-send-command process
(format "VERB"))
649 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
650 (not (integerp (car response-code
)))
651 (>= (car response-code
) 400))
654 (if (member 'xusr supported-extensions
)
656 (smtpmail-send-command process
(format "XUSR"))
657 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
658 (not (integerp (car response-code
)))
659 (>= (car response-code
) 400))
662 ;; MAIL FROM: <sender>
664 (if (or (member 'size supported-extensions
)
665 (assoc 'size supported-extensions
))
667 (with-current-buffer smtpmail-text-buffer
669 (+ (- (point-max) (point-min))
670 ;; Add one byte for each change-of-line
671 ;; because or CR-LF representation:
672 (count-lines (point-min) (point-max))
673 ;; For some reason, an empty line is
674 ;; added to the message. Maybe this
675 ;; is a bug, but it can't hurt to add
676 ;; those two bytes anyway:
680 (if (member '8bitmime supported-extensions
)
682 ;; Code should be added here that transforms
683 ;; the contents of the message buffer into
684 ;; something the receiving SMTP can handle.
685 ;; For a receiver that supports 8BITMIME, this
686 ;; may mean converting BINARY to BASE64, or
687 ;; adding Content-Transfer-Encoding and the
688 ;; other MIME headers. The code should also
689 ;; return an indication of what encoding the
690 ;; message buffer is now, i.e. ASCII or
696 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
697 (smtpmail-send-command process
(format "MAIL FROM: <%s>%s%s"
702 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
703 (not (integerp (car response-code
)))
704 (>= (car response-code
) 400))
708 ;; RCPT TO: <recipient>
710 (while (not (null (nth n recipient
)))
711 (smtpmail-send-command process
(format "RCPT TO: <%s>" (smtpmail-maybe-append-domain (nth n recipient
))))
714 (setq response-code
(smtpmail-read-response process
))
715 (if (or (null (car response-code
))
716 (not (integerp (car response-code
)))
717 (>= (car response-code
) 400))
723 (smtpmail-send-command process
"DATA")
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-data process smtpmail-text-buffer
)
735 (smtpmail-send-command process
".")
737 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
738 (not (integerp (car response-code
)))
739 (>= (car response-code
) 400))
744 ; (smtpmail-send-command process "QUIT")
745 ; (and (null (car (smtpmail-read-response process)))
749 (with-current-buffer (process-buffer process
)
750 (smtpmail-send-command process
"QUIT")
751 (smtpmail-read-response process
)
753 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
754 ; (not (integerp (car response-code)))
755 ; (>= (car response-code) 400))
758 (delete-process process
)
759 (unless smtpmail-debug-info
760 (kill-buffer process-buffer
)))))))
763 (defun smtpmail-process-filter (process output
)
764 (with-current-buffer (process-buffer process
)
765 (goto-char (point-max))
768 (defun smtpmail-read-response (process)
769 (let ((case-fold-search nil
)
770 (response-strings nil
)
771 (response-continue t
)
772 (return-value '(nil ()))
775 (while response-continue
776 (goto-char smtpmail-read-point
)
777 (while (not (search-forward "\r\n" nil t
))
778 (accept-process-output process
)
779 (goto-char smtpmail-read-point
))
781 (setq match-end
(point))
782 (setq response-strings
783 (cons (buffer-substring smtpmail-read-point
(- match-end
2))
786 (goto-char smtpmail-read-point
)
787 (if (looking-at "[0-9]+ ")
788 (let ((begin (match-beginning 0))
790 (if smtpmail-debug-info
791 (message "%s" (car response-strings
)))
793 (setq smtpmail-read-point match-end
)
795 ;; ignore lines that start with "0"
796 (if (looking-at "0[0-9]+ ")
798 (setq response-continue nil
)
801 (buffer-substring begin end
))
802 (nreverse response-strings
)))))
804 (if (looking-at "[0-9]+-")
805 (progn (if smtpmail-debug-info
806 (message "%s" (car response-strings
)))
807 (setq smtpmail-read-point match-end
)
808 (setq response-continue t
))
810 (setq smtpmail-read-point match-end
)
811 (setq response-continue nil
)
813 (cons nil
(nreverse response-strings
)))
816 (setq smtpmail-read-point match-end
)
820 (defun smtpmail-send-command (process command
)
821 (goto-char (point-max))
822 (if (= (aref command
0) ?P
)
823 (insert "PASS <omitted>\r\n")
824 (insert command
"\r\n"))
825 (setq smtpmail-read-point
(point))
826 (process-send-string process command
)
827 (process-send-string process
"\r\n"))
829 (defun smtpmail-send-data-1 (process data
)
830 (goto-char (point-max))
832 (if (and (multibyte-string-p data
)
833 smtpmail-code-conv-from
)
834 (setq data
(string-as-multibyte
835 (encode-coding-string data smtpmail-code-conv-from
))))
837 (if smtpmail-debug-info
838 (insert data
"\r\n"))
840 (setq smtpmail-read-point
(point))
841 ;; Escape "." at start of a line
842 (if (eq (string-to-char data
) ?.
)
843 (process-send-string process
"."))
844 (process-send-string process data
)
845 (process-send-string process
"\r\n")
848 (defun smtpmail-send-data (process buffer
)
855 (with-current-buffer buffer
856 (goto-char (point-min)))
859 (with-current-buffer buffer
861 (setq this-line
(point))
863 (setq this-line-end
(point))
864 (setq sending-data nil
)
865 (setq sending-data
(buffer-substring this-line this-line-end
))
866 (if (/= (forward-line 1) 0)
867 (setq data-continue nil
)))
869 (smtpmail-send-data-1 process sending-data
)
875 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end
)
876 "Get address list suitable for smtp RCPT TO: <address>."
878 (with-current-buffer smtpmail-address-buffer
881 ((case-fold-search t
)
882 (simple-address-list "")
886 (insert-buffer-substring smtpmail-text-buffer header-start header-end
)
887 (goto-char (point-min))
888 ;; RESENT-* fields should stop processing of regular fields.
891 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
893 "^Resent-\\(to\\|cc\\|bcc\\):"
894 "^\\(To:\\|Cc:\\|Bcc:\\)")))
896 (while (re-search-forward addr-regexp header-end t
)
898 (setq this-line
(match-beginning 0))
900 ;; get any continuation lines
901 (while (and (looking-at "^[ \t]+") (< (point) header-end
))
903 (setq this-line-end
(point-marker))
904 (setq simple-address-list
905 (concat simple-address-list
" "
906 (mail-strip-quoted-names (buffer-substring this-line this-line-end
))))
909 (insert " " simple-address-list
"\n")
910 (subst-char-in-region (point-min) (point-max) 10 ? t
);; newline --> blank
911 (subst-char-in-region (point-min) (point-max) ?
, ? t
);; comma --> blank
912 (subst-char-in-region (point-min) (point-max) 9 ? t
);; tab --> blank
914 (goto-char (point-min))
915 ;; tidyness in case hook is not robust when it looks at this
916 (while (re-search-forward "[ \t]+" header-end t
) (replace-match " "))
918 (goto-char (point-min))
919 (let (recipient-address-list)
920 (while (re-search-forward " \\([^ ]+\\) " (point-max) t
)
922 (setq recipient-address-list
(cons (buffer-substring (match-beginning 1) (match-end 1))
923 recipient-address-list
))
925 (setq smtpmail-recipient-address-list recipient-address-list
))
933 (defun smtpmail-do-bcc (header-end)
934 "Delete [Resent-]BCC: and their continuation lines from the header area.
935 There may be multiple BCC: lines, and each may have arbitrarily
936 many continuation lines."
937 (let ((case-fold-search t
))
939 (goto-char (point-min))
940 ;; iterate over all BCC: lines
941 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t
)
942 (delete-region (match-beginning 0)
943 (progn (forward-line 1) (point)))
944 ;; get rid of any continuation lines
945 (while (and (looking-at "^[ \t].*\n") (< (point) header-end
))
946 (replace-match ""))))))
951 ;;; smtpmail.el ends here