1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995, 1996, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
6 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
7 ;; Maintainer: Simon Josefsson <simon@josefsson.org>
8 ;; w32 Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
9 ;; ESMTP support: Simon Leinen <simon@switch.ch>
10 ;; Hacked by Mike Taylor, 11th October 1999 to add support for
11 ;; automatically appending a domain to RCPT TO: addresses.
12 ;; AUTH=LOGIN support: Stephen Cranefield <scranefield@infoscience.otago.ac.nz>
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
34 ;; Send Mail to smtp host from smtpmail temp buffer.
36 ;; Please add these lines in your .emacs(_emacs) or use customize.
38 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
39 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
40 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
41 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
42 ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
43 ;;(setq smtpmail-debug-info t) ; only to debug problems
44 ;;(setq smtpmail-auth-credentials ; or use ~/.authinfo
45 ;; '(("YOUR SMTP HOST" 25 "username" "password")))
46 ;;(setq smtpmail-starttls-credentials
47 ;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
48 ;; Where the 25 equals the value of `smtpmail-smtp-service', it can be an
49 ;; integer or a string, just as long as they match (eq).
51 ;; To queue mail, set smtpmail-queue-mail to t and use
52 ;; smtpmail-send-queued-mail to send.
54 ;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
55 ;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
56 ;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
57 ;; Rewritten by Simon Josefsson to use same credential variable as AUTH
60 ;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
61 ;; Authentication by the AUTH mechanism.
62 ;; See http://www.ietf.org/rfc/rfc2554.txt
64 ;; Modified by Simon Josefsson <simon@josefsson.org>, 2000-10-07, to support
65 ;; STARTTLS. Requires external program
66 ;; ftp://ftp.opaopa.org/pub/elisp/starttls-*.tar.gz.
67 ;; See http://www.ietf.org/rfc/rfc2246.txt, http://www.ietf.org/rfc/rfc2487.txt
72 (autoload 'starttls-open-stream "starttls")
73 (autoload 'starttls-negotiate "starttls")
74 (autoload 'mail-strip-quoted-names "mail-utils")
75 (autoload 'message-make-date "message")
76 (autoload 'message-make-message-id "message")
77 (autoload 'rfc2104-hash "rfc2104")
78 (autoload 'netrc-parse "netrc")
79 (autoload 'netrc-machine "netrc")
80 (autoload 'netrc-get "netrc")
83 (defgroup smtpmail nil
84 "SMTP protocol for sending mail."
88 (defcustom smtpmail-default-smtp-server nil
89 "*Specify default SMTP server.
90 This only has effect if you specify it before loading the smtpmail library."
91 :type '(choice (const nil) string)
94 (defcustom smtpmail-smtp-server
95 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
96 "*The name of the host running SMTP server."
97 :type '(choice (const nil) string)
100 (defcustom smtpmail-smtp-service 25
101 "*SMTP service port number.
102 The default value would be \"smtp\" or 25 ."
103 :type '(choice (integer :tag "Port") (string :tag "Service"))
106 (defcustom smtpmail-local-domain nil
107 "*Local domain name without a host name.
108 If the function (system-name) returns the full internet address,
109 don't define this value."
110 :type '(choice (const nil) string)
113 (defcustom smtpmail-sendto-domain nil
114 "*Local domain name without a host name.
115 This is appended (with an @-sign) to any specified recipients which do
116 not include an @-sign, so that each RCPT TO address is fully qualified.
117 \(Some configurations of sendmail require this.)
119 Don't bother to set this unless you have get an error like:
120 Sending failed; SMTP protocol error
121 when sending mail, and the *trace of SMTP session to <somewhere>*
122 buffer includes an exchange like:
124 501 <someone>: recipient address must contain a domain
126 :type '(choice (const nil) string)
129 (defcustom smtpmail-debug-info nil
130 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
131 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
136 (defcustom smtpmail-debug-verb nil
137 "Whether this library sends the SMTP VERB command or not.
138 The commands enables verbose information from the SMTP server."
142 (defcustom smtpmail-code-conv-from nil ;; *junet*
143 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
147 (defcustom smtpmail-queue-mail nil
148 "*Specify if mail is queued (if t) or sent immediately (if nil).
149 If queued, it is stored in the directory `smtpmail-queue-dir'
150 and sent with `smtpmail-send-queued-mail'."
154 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
155 "*Directory where `smtpmail.el' stores queued mail."
159 (defcustom smtpmail-auth-credentials "~/.authinfo"
160 "Specify username and password for servers, directly or via .netrc file.
161 This variable can either be a filename pointing to a file in netrc(5)
162 format, or list of four-element lists that contain, in order,
163 `servername' (a string), `port' (an integer), `user' (a string) and
164 `password' (a string, or nil to query the user when needed). If you
165 need to enter a `realm' too, add it to the user string, so that it
166 looks like `user@realm'."
168 (repeat (list (string :tag "Server")
169 (integer :tag "Port")
170 (string :tag "Username")
171 (choice (const :tag "Query when needed" nil)
172 (string :tag "Password")))))
176 (defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
177 "Specify STARTTLS keys and certificates for servers.
178 This is a list of four-element list with `servername' (a string),
179 `port' (an integer), `key' (a filename) and `certificate' (a
181 If you do not have a certificate/key pair, leave the `key' and
182 `certificate' fields as `nil'. A key/certificate pair is only
183 needed if you want to use X.509 client authenticated
185 :type '(repeat (list (string :tag "Server")
186 (integer :tag "Port")
188 (file :tag "Certificate")))
192 (defcustom smtpmail-warn-about-unknown-extensions nil
193 "*If set, print warnings about unknown SMTP extensions.
194 This is mainly useful for development purposes, to learn about
195 new SMTP extensions that might be useful to support."
200 (defvar smtpmail-queue-index-file "index"
201 "File name of queued mail index,
202 This is relative to `smtpmail-queue-dir'.")
204 (defvar smtpmail-address-buffer)
205 (defvar smtpmail-recipient-address-list)
207 (defvar smtpmail-queue-counter 0)
209 ;; Buffer-local variable.
210 (defvar smtpmail-read-point)
212 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
213 smtpmail-queue-index-file))
215 (defconst smtpmail-auth-supported '(cram-md5 plain login)
216 "List of supported SMTP AUTH mechanisms.")
222 (defvar smtpmail-mail-address nil
223 "Value to use for envelope-from address for mail from ambient buffer.")
226 (defun smtpmail-send-it ()
227 (let ((errbuf (if mail-interactive
228 (generate-new-buffer " smtpmail errors")
230 (tembuf (generate-new-buffer " smtpmail temp"))
231 (case-fold-search nil)
233 (mailbuf (current-buffer))
234 ;; Examine this variable now, so that
235 ;; local binding in the mail buffer will take effect.
236 (smtpmail-mail-address
237 (or (and mail-specify-envelope-from (mail-envelope-from))
239 (smtpmail-code-conv-from
240 (if enable-multibyte-characters
241 (let ((sendmail-coding-system smtpmail-code-conv-from))
242 (select-message-coding-system)))))
247 ;; Use the same buffer-file-coding-system as in the mail
248 ;; buffer, otherwise any write-region invocations (e.g., in
249 ;; mail-do-fcc below) will annoy with asking for a suitable
251 (set-buffer-file-coding-system smtpmail-code-conv-from nil t)
252 (insert-buffer-substring mailbuf)
253 (goto-char (point-max))
254 ;; require one newline at the end.
255 (or (= (preceding-char) ?\n)
257 ;; Change header-delimiter to be what sendmail expects.
258 (mail-sendmail-undelimit-header)
259 (setq delimline (point-marker))
260 ;; (sendmail-synch-aliases)
262 (expand-mail-aliases (point-min) delimline))
263 (goto-char (point-min))
264 ;; ignore any blank lines in the header
265 (while (and (re-search-forward "\n\n\n*" delimline t)
266 (< (point) delimline))
267 (replace-match "\n"))
268 (let ((case-fold-search t))
269 ;; We used to process Resent-... headers here,
270 ;; but it was not done properly, and the job
271 ;; is done correctly in smtpmail-deduce-address-list.
272 ;; Don't send out a blank subject line
273 (goto-char (point-min))
274 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
276 ;; This one matches a Subject just before the header delimiter.
277 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
278 (= (match-end 0) delimline))
280 ;; Put the "From:" field in unless for some odd reason
281 ;; they put one in themselves.
282 (goto-char (point-min))
283 (if (not (re-search-forward "^From:" delimline t))
284 (let* ((login smtpmail-mail-address)
285 (fullname (user-full-name)))
286 (cond ((eq mail-from-style 'angles)
287 (insert "From: " fullname)
288 (let ((fullname-start (+ (point-min) 6))
289 (fullname-end (point-marker)))
290 (goto-char fullname-start)
291 ;; Look for a character that cannot appear unquoted
292 ;; according to RFC 822.
293 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
296 ;; Quote fullname, escaping specials.
297 (goto-char fullname-start)
299 (while (re-search-forward "[\"\\]"
301 (replace-match "\\\\\\&" t))
303 (insert " <" login ">\n"))
304 ((eq mail-from-style 'parens)
305 (insert "From: " login " (")
306 (let ((fullname-start (point)))
308 (let ((fullname-end (point-marker)))
309 (goto-char fullname-start)
310 ;; RFC 822 says \ and nonmatching parentheses
311 ;; must be escaped in comments.
312 ;; Escape every instance of ()\ ...
313 (while (re-search-forward "[()\\]" fullname-end 1)
314 (replace-match "\\\\\\&" t))
315 ;; ... then undo escaping of matching parentheses,
316 ;; including matching nested parentheses.
317 (goto-char fullname-start)
318 (while (re-search-forward
319 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
321 (replace-match "\\1(\\3)" t)
322 (goto-char fullname-start))))
324 ((null mail-from-style)
325 (insert "From: " login "\n")))))
326 ;; Insert a `Message-Id:' field if there isn't one yet.
327 (goto-char (point-min))
328 (unless (re-search-forward "^Message-Id:" delimline t)
329 (insert "Message-Id: " (message-make-message-id) "\n"))
330 ;; Insert a `Date:' field if there isn't one yet.
331 (goto-char (point-min))
332 (unless (re-search-forward "^Date:" delimline t)
333 (insert "Date: " (message-make-date) "\n"))
334 ;; Possibly add a MIME header for the current coding system
336 (goto-char (point-min))
337 (and (eq mail-send-nonascii 'mime)
338 (not (re-search-forward "^MIME-version:" delimline t))
339 (progn (skip-chars-forward "\0-\177")
340 (/= (point) (point-max)))
341 smtpmail-code-conv-from
343 (coding-system-get smtpmail-code-conv-from
345 (goto-char delimline)
346 (insert "MIME-version: 1.0\n"
347 "Content-type: text/plain; charset="
348 (symbol-name charset)
349 "\nContent-Transfer-Encoding: 8bit\n")))
350 ;; Insert an extra newline if we need it to work around
351 ;; Sun's bug that swallows newlines.
352 (goto-char (1+ delimline))
353 (if (eval mail-mailer-swallows-blank-line)
355 ;; Find and handle any FCC fields.
356 (goto-char (point-min))
357 (if (re-search-forward "^FCC:" delimline t)
358 ;; Force mail-do-fcc to use the encoding of the mail
359 ;; buffer to encode outgoing messages on FCC files.
360 (let ((coding-system-for-write smtpmail-code-conv-from))
361 (mail-do-fcc delimline)))
363 (with-current-buffer errbuf
368 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
369 (setq smtpmail-recipient-address-list
370 (smtpmail-deduce-address-list tembuf (point-min) delimline))
371 (kill-buffer smtpmail-address-buffer)
373 (smtpmail-do-bcc delimline)
375 (if (not smtpmail-queue-mail)
376 (if (not (null smtpmail-recipient-address-list))
377 (if (not (smtpmail-via-smtp
378 smtpmail-recipient-address-list tembuf))
379 (error "Sending failed; SMTP protocol error"))
380 (error "Sending failed; no recipients"))
384 (format-time-string "%Y-%m-%d_%H:%M:%S")
385 (setq smtpmail-queue-counter
386 (1+ smtpmail-queue-counter)))
388 (file-data (convert-standard-filename file-data))
389 (file-elisp (concat file-data ".el"))
390 (buffer-data (create-file-buffer file-data))
391 (buffer-elisp (create-file-buffer file-elisp))
392 (buffer-scratch "*queue-mail*"))
393 (unless (file-exists-p smtpmail-queue-dir)
394 (make-directory smtpmail-queue-dir t))
395 (with-current-buffer buffer-data
397 (set-buffer-file-coding-system smtpmail-code-conv-from nil t)
398 (insert-buffer-substring tembuf)
399 (write-file file-data)
400 (set-buffer buffer-elisp)
403 "(setq smtpmail-recipient-address-list '"
404 (prin1-to-string smtpmail-recipient-address-list)
406 (write-file file-elisp)
407 (set-buffer (generate-new-buffer buffer-scratch))
408 (insert (concat file-data "\n"))
409 (append-to-file (point-min)
411 smtpmail-queue-index)
413 (kill-buffer buffer-scratch)
414 (kill-buffer buffer-data)
415 (kill-buffer buffer-elisp))))
418 (kill-buffer errbuf)))))
421 (defun smtpmail-send-queued-mail ()
422 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
425 ;;; Get index, get first mail, send it, update index, get second
426 ;;; mail, send it, etc...
428 (insert-file-contents smtpmail-queue-index)
429 (goto-char (point-min))
431 (setq file-msg (buffer-substring (point) (line-end-position)))
433 ;; Insert the message literally: it is already encoded as per
434 ;; the MIME headers, and code conversions might guess the
437 (let ((coding-system-for-read 'no-conversion))
438 (insert-file-contents file-msg))
439 (let ((smtpmail-mail-address
440 (or (and mail-specify-envelope-from (mail-envelope-from))
442 (if (not (null smtpmail-recipient-address-list))
443 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
445 (error "Sending failed; SMTP protocol error"))
446 (error "Sending failed; no recipients"))))
447 (delete-file file-msg)
448 (delete-file (concat file-msg ".el"))
449 (delete-region (point-at-bol) (point-at-bol 2)))
450 (write-region (point-min) (point-max) smtpmail-queue-index))))
452 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
454 (defun smtpmail-fqdn ()
455 (if smtpmail-local-domain
456 (concat (system-name) "." smtpmail-local-domain)
459 (defsubst smtpmail-cred-server (cred)
462 (defsubst smtpmail-cred-port (cred)
465 (defsubst smtpmail-cred-key (cred)
468 (defsubst smtpmail-cred-user (cred)
471 (defsubst smtpmail-cred-cert (cred)
474 (defsubst smtpmail-cred-passwd (cred)
477 (defun smtpmail-find-credentials (cred server port)
480 (while (setq el (pop l))
481 (when (and (equal server (smtpmail-cred-server el))
482 (equal port (smtpmail-cred-port el)))
483 (throw 'done el))))))
485 (defun smtpmail-maybe-append-domain (recipient)
486 (if (or (not smtpmail-sendto-domain)
487 (string-match "@" recipient))
489 (concat recipient "@" smtpmail-sendto-domain)))
491 (defun smtpmail-intersection (list1 list2)
494 (when (memq el2 list1)
498 (defvar starttls-extra-args)
499 (defvar starttls-extra-arguments)
501 (defun smtpmail-open-stream (process-buffer host port)
502 (let ((cred (smtpmail-find-credentials
503 smtpmail-starttls-credentials host port)))
504 (if (null (and cred (condition-case ()
507 (call-process (if starttls-use-gnutls
508 starttls-gnutls-program
512 (open-network-stream "SMTP" process-buffer host port)
513 (let* ((cred-key (smtpmail-cred-key cred))
514 (cred-cert (smtpmail-cred-cert cred))
518 (when (and (stringp cred-key) (stringp cred-cert)
520 (setq cred-key (expand-file-name cred-key)))
522 (setq cred-cert (expand-file-name cred-cert))))
523 (list "--key-file" cred-key "--cert-file" cred-cert))))
524 (starttls-extra-arguments
526 starttls-extra-arguments
527 (when (and (stringp cred-key) (stringp cred-cert)
529 (setq cred-key (expand-file-name cred-key)))
531 (setq cred-cert (expand-file-name cred-cert))))
532 (list "--x509keyfile" cred-key "--x509certfile" cred-cert)))))
533 (starttls-open-stream "SMTP" process-buffer host port)))))
535 (defun smtpmail-try-auth-methods (process supported-extensions host port)
536 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
537 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
538 (cred (if (stringp smtpmail-auth-credentials)
539 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
540 (port-name (format "%s" (or port "smtp")))
541 (hostentry (netrc-machine netrc host port-name
545 (netrc-get hostentry "login")
546 (netrc-get hostentry "password"))))
547 (smtpmail-find-credentials
548 smtpmail-auth-credentials host port)))
550 (or (smtpmail-cred-passwd cred)
552 (format "SMTP password for %s:%s: "
553 (smtpmail-cred-server cred)
554 (smtpmail-cred-port cred))))))
556 (when (and cred mech)
559 (smtpmail-send-command process (upcase (format "AUTH %s" mech)))
560 (if (or (null (car (setq ret (smtpmail-read-response process))))
561 (not (integerp (car ret)))
564 (when (eq (car ret) 334)
565 (let* ((challenge (substring (cadr ret) 4))
566 (decoded (base64-decode-string challenge))
567 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
568 (response (concat (smtpmail-cred-user cred) " " hash))
569 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
570 ;; SMTP auth fails because the SMTP server identifies
571 ;; only the first part of the string (delimited by
572 ;; new line characters) as a response from the
573 ;; client, and the rest as distinct commands.
575 ;; In my case, the response string is 80 characters
576 ;; long. Without the no-line-break option for
577 ;; base64-encode-sting, only the first 76 characters
578 ;; are taken as a response to the server, and the
579 ;; authentication fails.
580 (encoded (base64-encode-string response t)))
581 (smtpmail-send-command process (format "%s" encoded))
582 (if (or (null (car (setq ret (smtpmail-read-response process))))
583 (not (integerp (car ret)))
585 (throw 'done nil)))))
587 (smtpmail-send-command process "AUTH LOGIN")
588 (if (or (null (car (setq ret (smtpmail-read-response process))))
589 (not (integerp (car ret)))
592 (smtpmail-send-command
593 process (base64-encode-string (smtpmail-cred-user cred) t))
594 (if (or (null (car (setq ret (smtpmail-read-response process))))
595 (not (integerp (car ret)))
598 (smtpmail-send-command process (base64-encode-string passwd t))
599 (if (or (null (car (setq ret (smtpmail-read-response process))))
600 (not (integerp (car ret)))
604 ;; We used to send an empty initial request, and wait for an
605 ;; empty response, and then send the password, but this
606 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
607 ;; is not sent if the server did not advertise AUTH PLAIN in
608 ;; the EHLO response. See RFC 2554 for more info.
609 (smtpmail-send-command process
610 (concat "AUTH PLAIN "
611 (base64-encode-string
613 (smtpmail-cred-user cred)
616 (if (or (null (car (setq ret (smtpmail-read-response process))))
617 (not (integerp (car ret)))
618 (not (equal (car ret) 235)))
622 (error "Mechanism %s not implemented" mech)))
623 ;; Remember the password.
624 (when (and (not (stringp smtpmail-auth-credentials))
625 (null (smtpmail-cred-passwd cred)))
626 (setcar (cdr (cdr (cdr cred))) passwd)))))
628 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
630 (host (or smtpmail-smtp-server
631 (error "`smtpmail-smtp-server' not defined")))
632 (port smtpmail-smtp-service)
633 ;; smtpmail-mail-address should be set to the appropriate
634 ;; buffer-local value by the caller, but in case not:
635 (envelope-from (or smtpmail-mail-address
636 (and mail-specify-envelope-from
637 (mail-envelope-from))
642 (supported-extensions '()))
645 ;; get or create the trace buffer
647 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
649 ;; clear the trace buffer of old output
650 (with-current-buffer process-buffer
651 (setq buffer-undo-list t)
654 ;; open the connection to the server
655 (setq process (smtpmail-open-stream process-buffer host port))
656 (and (null process) (throw 'done nil))
658 ;; set the send-filter
659 (set-process-filter process 'smtpmail-process-filter)
661 (with-current-buffer process-buffer
662 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
663 (make-local-variable 'smtpmail-read-point)
664 (setq smtpmail-read-point (point-min))
667 (if (or (null (car (setq greeting (smtpmail-read-response process))))
668 (not (integerp (car greeting)))
669 (>= (car greeting) 400))
677 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
679 (if (or (null (car (setq response-code
680 (smtpmail-read-response process))))
681 (not (integerp (car response-code)))
682 (>= (car response-code) 400))
685 (smtpmail-send-command
686 process (format "HELO %s" (smtpmail-fqdn)))
688 (if (or (null (car (setq response-code
689 (smtpmail-read-response process))))
690 (not (integerp (car response-code)))
691 (>= (car response-code) 400))
693 (dolist (line (cdr (cdr response-code)))
694 (let ((name (mapcar (lambda (s) (intern (downcase s)))
695 (split-string (substring line 4) "[ ]"))))
696 (and (eq (length name) 1)
697 (setq name (car name)))
699 (cond ((memq (if (consp name) (car name) name)
700 '(verb xvrb 8bitmime onex xone
704 auth=login auth starttls))
705 (setq supported-extensions
706 (cons name supported-extensions)))
707 (smtpmail-warn-about-unknown-extensions
708 (message "Unknown extension %s" name)))))))
711 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
712 (member 'starttls supported-extensions)
713 (numberp (process-id process)))
715 (smtpmail-send-command process (format "STARTTLS"))
716 (if (or (null (car (setq response-code (smtpmail-read-response process))))
717 (not (integerp (car response-code)))
718 (>= (car response-code) 400))
720 (starttls-negotiate process)
721 (setq do-starttls nil))
722 (setq do-ehlo nil))))
724 (smtpmail-try-auth-methods process supported-extensions host port)
726 (if (or (member 'onex supported-extensions)
727 (member 'xone supported-extensions))
729 (smtpmail-send-command process (format "ONEX"))
730 (if (or (null (car (setq response-code (smtpmail-read-response process))))
731 (not (integerp (car response-code)))
732 (>= (car response-code) 400))
735 (if (and smtpmail-debug-verb
736 (or (member 'verb supported-extensions)
737 (member 'xvrb supported-extensions)))
739 (smtpmail-send-command process (format "VERB"))
740 (if (or (null (car (setq response-code (smtpmail-read-response process))))
741 (not (integerp (car response-code)))
742 (>= (car response-code) 400))
745 (if (member 'xusr supported-extensions)
747 (smtpmail-send-command process (format "XUSR"))
748 (if (or (null (car (setq response-code (smtpmail-read-response process))))
749 (not (integerp (car response-code)))
750 (>= (car response-code) 400))
753 ;; MAIL FROM:<sender>
755 (if (or (member 'size supported-extensions)
756 (assoc 'size supported-extensions))
758 (with-current-buffer smtpmail-text-buffer
760 (+ (- (point-max) (point-min))
761 ;; Add one byte for each change-of-line
762 ;; because of CR-LF representation:
763 (count-lines (point-min) (point-max)))))
766 (if (member '8bitmime supported-extensions)
768 ;; Code should be added here that transforms
769 ;; the contents of the message buffer into
770 ;; something the receiving SMTP can handle.
771 ;; For a receiver that supports 8BITMIME, this
772 ;; may mean converting BINARY to BASE64, or
773 ;; adding Content-Transfer-Encoding and the
774 ;; other MIME headers. The code should also
775 ;; return an indication of what encoding the
776 ;; message buffer is now, i.e. ASCII or
782 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
783 (smtpmail-send-command process (format "MAIL FROM:<%s>%s%s"
788 (if (or (null (car (setq response-code (smtpmail-read-response process))))
789 (not (integerp (car response-code)))
790 (>= (car response-code) 400))
794 ;; RCPT TO:<recipient>
796 (while (not (null (nth n recipient)))
797 (smtpmail-send-command process (format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient))))
800 (setq response-code (smtpmail-read-response process))
801 (if (or (null (car response-code))
802 (not (integerp (car response-code)))
803 (>= (car response-code) 400))
809 (smtpmail-send-command process "DATA")
811 (if (or (null (car (setq response-code (smtpmail-read-response process))))
812 (not (integerp (car response-code)))
813 (>= (car response-code) 400))
818 (smtpmail-send-data process smtpmail-text-buffer)
821 (smtpmail-send-command process ".")
823 (if (or (null (car (setq response-code (smtpmail-read-response process))))
824 (not (integerp (car response-code)))
825 (>= (car response-code) 400))
830 ; (smtpmail-send-command process "QUIT")
831 ; (and (null (car (smtpmail-read-response process)))
835 (with-current-buffer (process-buffer process)
836 (smtpmail-send-command process "QUIT")
837 (smtpmail-read-response process)
839 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
840 ; (not (integerp (car response-code)))
841 ; (>= (car response-code) 400))
844 (delete-process process)
845 (unless smtpmail-debug-info
846 (kill-buffer process-buffer)))))))
849 (defun smtpmail-process-filter (process output)
850 (with-current-buffer (process-buffer process)
851 (goto-char (point-max))
854 (defun smtpmail-read-response (process)
855 (let ((case-fold-search nil)
856 (response-strings nil)
857 (response-continue t)
858 (return-value '(nil ()))
861 (while response-continue
862 (goto-char smtpmail-read-point)
863 (while (not (search-forward "\r\n" nil t))
864 (unless (memq (process-status process) '(open run))
866 (accept-process-output process)
867 (goto-char smtpmail-read-point))
869 (setq match-end (point))
870 (setq response-strings
871 (cons (buffer-substring smtpmail-read-point (- match-end 2))
874 (goto-char smtpmail-read-point)
875 (if (looking-at "[0-9]+ ")
876 (let ((begin (match-beginning 0))
878 (if smtpmail-debug-info
879 (message "%s" (car response-strings)))
881 (setq smtpmail-read-point match-end)
883 ;; ignore lines that start with "0"
884 (if (looking-at "0[0-9]+ ")
886 (setq response-continue nil)
888 (cons (string-to-number
889 (buffer-substring begin end))
890 (nreverse response-strings)))))
892 (if (looking-at "[0-9]+-")
893 (progn (if smtpmail-debug-info
894 (message "%s" (car response-strings)))
895 (setq smtpmail-read-point match-end)
896 (setq response-continue t))
898 (setq smtpmail-read-point match-end)
899 (setq response-continue nil)
901 (cons nil (nreverse response-strings)))))))
902 (setq smtpmail-read-point match-end))
906 (defun smtpmail-send-command (process command)
907 (goto-char (point-max))
908 (if (= (aref command 0) ?P)
909 (insert "PASS <omitted>\r\n")
910 (insert command "\r\n"))
911 (setq smtpmail-read-point (point))
912 (process-send-string process command)
913 (process-send-string process "\r\n"))
915 (defun smtpmail-send-data-1 (process data)
916 (goto-char (point-max))
918 (if (and (multibyte-string-p data)
919 smtpmail-code-conv-from)
920 (setq data (string-as-multibyte
921 (encode-coding-string data smtpmail-code-conv-from))))
923 (if smtpmail-debug-info
924 (insert data "\r\n"))
926 (setq smtpmail-read-point (point))
927 ;; Escape "." at start of a line
928 (if (eq (string-to-char data) ?.)
929 (process-send-string process "."))
930 (process-send-string process data)
931 (process-send-string process "\r\n")
934 (defun smtpmail-send-data (process buffer)
935 (let ((data-continue t) sending-data)
936 (with-current-buffer buffer
937 (goto-char (point-min)))
939 (with-current-buffer buffer
940 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
942 (setq data-continue (not (eobp))))
943 (smtpmail-send-data-1 process sending-data))))
945 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
946 "Get address list suitable for smtp RCPT TO: <address>."
948 (with-current-buffer smtpmail-address-buffer
951 ((case-fold-search t)
952 (simple-address-list "")
956 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
957 (goto-char (point-min))
958 ;; RESENT-* fields should stop processing of regular fields.
961 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
963 "^Resent-\\(to\\|cc\\|bcc\\):"
964 "^\\(To:\\|Cc:\\|Bcc:\\)")))
966 (while (re-search-forward addr-regexp header-end t)
968 (setq this-line (match-beginning 0))
970 ;; get any continuation lines
971 (while (and (looking-at "^[ \t]+") (< (point) header-end))
973 (setq this-line-end (point-marker))
974 (setq simple-address-list
975 (concat simple-address-list " "
976 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
979 (insert " " simple-address-list "\n")
980 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
981 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
982 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
984 (goto-char (point-min))
985 ;; tidyness in case hook is not robust when it looks at this
986 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
988 (goto-char (point-min))
989 (let (recipient-address-list)
990 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
992 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
993 recipient-address-list))
995 (setq smtpmail-recipient-address-list recipient-address-list))
1003 (defun smtpmail-do-bcc (header-end)
1004 "Delete [Resent-]BCC: and their continuation lines from the header area.
1005 There may be multiple BCC: lines, and each may have arbitrarily
1006 many continuation lines."
1007 (let ((case-fold-search t))
1009 (goto-char (point-min))
1010 ;; iterate over all BCC: lines
1011 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
1012 (delete-region (match-beginning 0)
1013 (progn (forward-line 1) (point)))
1014 ;; get rid of any continuation lines
1015 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
1016 (replace-match ""))))))
1021 ;;; arch-tag: a76992df-6d71-43b7-9e72-4bacc6c05466
1022 ;;; smtpmail.el ends here