1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995, 1996, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 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 3 of the License, or
20 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
32 ;; Send Mail to smtp host from smtpmail temp buffer.
34 ;; Please add these lines in your .emacs(_emacs) or use customize.
36 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
37 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
38 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
39 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
40 ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
41 ;;(setq smtpmail-debug-info t) ; only to debug problems
42 ;;(setq smtpmail-auth-credentials ; or use ~/.authinfo
43 ;; '(("YOUR SMTP HOST" 25 "username" "password")))
44 ;;(setq smtpmail-starttls-credentials
45 ;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
46 ;; Where the 25 equals the value of `smtpmail-smtp-service', it can be an
47 ;; integer or a string, just as long as they match (eq).
49 ;; To queue mail, set smtpmail-queue-mail to t and use
50 ;; smtpmail-send-queued-mail to send.
52 ;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
53 ;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
54 ;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
55 ;; Rewritten by Simon Josefsson to use same credential variable as AUTH
58 ;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
59 ;; Authentication by the AUTH mechanism.
60 ;; See http://www.ietf.org/rfc/rfc2554.txt
62 ;; Modified by Simon Josefsson <simon@josefsson.org>, 2000-10-07, to support
63 ;; STARTTLS. Requires external program
64 ;; ftp://ftp.opaopa.org/pub/elisp/starttls-*.tar.gz.
65 ;; See http://www.ietf.org/rfc/rfc2246.txt, http://www.ietf.org/rfc/rfc2487.txt
69 ;; This version of `smtpmail.el' should only be used with Emacs 21.
70 (if (featurep 'xemacs)
71 (error "Please use `smtpmail.el' from the mail-lib package.")
72 (when (>= emacs-major-version 22)
73 (error "Please use `smtpmail.el' bundled with Emacs.")))
76 (autoload 'starttls-open-stream "starttls")
77 (autoload 'starttls-negotiate "starttls")
78 (autoload 'mail-strip-quoted-names "mail-utils")
79 (autoload 'message-make-date "message")
80 (autoload 'message-make-message-id "message")
81 (autoload 'rfc2104-hash "rfc2104")
82 (autoload 'netrc-parse "netrc")
83 (autoload 'netrc-machine "netrc")
84 (autoload 'netrc-get "netrc")
87 (autoload 'auth-source-user-or-password "auth-source"))
90 (defgroup smtpmail nil
91 "SMTP protocol for sending mail."
95 (defcustom smtpmail-default-smtp-server nil
96 "*Specify default SMTP server.
97 This only has effect if you specify it before loading the smtpmail library."
98 :type '(choice (const nil) string)
101 (defcustom smtpmail-smtp-server
102 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
103 "*The name of the host running SMTP server."
104 :type '(choice (const nil) string)
107 (defcustom smtpmail-smtp-service 25
108 "*SMTP service port number.
109 The default value would be \"smtp\" or 25."
110 :type '(choice (integer :tag "Port") (string :tag "Service"))
113 (defcustom smtpmail-local-domain nil
114 "*Local domain name without a host name.
115 If the function `system-name' returns the full internet address,
116 don't define this value."
117 :type '(choice (const nil) string)
120 (defcustom smtpmail-sendto-domain nil
121 "*Local domain name without a host name.
122 This is appended (with an @-sign) to any specified recipients which do
123 not include an @-sign, so that each RCPT TO address is fully qualified.
124 \(Some configurations of sendmail require this.)
126 Don't bother to set this unless you have get an error like:
127 Sending failed; SMTP protocol error
128 when sending mail, and the *trace of SMTP session to <somewhere>*
129 buffer includes an exchange like:
131 501 <someone>: recipient address must contain a domain
133 :type '(choice (const nil) string)
136 (defcustom smtpmail-debug-info nil
137 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
138 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
143 (defcustom smtpmail-debug-verb nil
144 "Whether this library sends the SMTP VERB command or not.
145 The commands enables verbose information from the SMTP server."
149 (defcustom smtpmail-code-conv-from nil ;; *junet*
150 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
154 (defcustom smtpmail-queue-mail nil
155 "*If set, mail is queued; otherwise it is sent immediately.
156 If queued, it is stored in the directory `smtpmail-queue-dir'
157 and sent with `smtpmail-send-queued-mail'."
161 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
162 "*Directory where `smtpmail.el' stores queued mail."
166 (defcustom smtpmail-auth-credentials "~/.authinfo"
167 "Specify username and password for servers, directly or via .netrc file.
168 This variable can either be a filename pointing to a file in netrc(5)
169 format, or list of four-element lists that contain, in order,
170 `servername' (a string), `port' (an integer), `user' (a string) and
171 `password' (a string, or nil to query the user when needed). If you
172 need to enter a `realm' too, add it to the user string, so that it
173 looks like `user@realm'."
175 (repeat (list (string :tag "Server")
176 (integer :tag "Port")
177 (string :tag "Username")
178 (choice (const :tag "Query when needed" nil)
179 (string :tag "Password")))))
183 (defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
184 "Specify STARTTLS keys and certificates for servers.
185 This is a list of four-element list with `servername' (a string),
186 `port' (an integer), `key' (a filename) and `certificate' (a
188 If you do not have a certificate/key pair, leave the `key' and
189 `certificate' fields as `nil'. A key/certificate pair is only
190 needed if you want to use X.509 client authenticated
192 :type '(repeat (list (string :tag "Server")
193 (integer :tag "Port")
195 (file :tag "Certificate")))
199 (defcustom smtpmail-warn-about-unknown-extensions nil
200 "*If set, print warnings about unknown SMTP extensions.
201 This is mainly useful for development purposes, to learn about
202 new SMTP extensions that might be useful to support."
207 (defvar smtpmail-queue-index-file "index"
208 "File name of queued mail index.
209 This is relative to `smtpmail-queue-dir'.")
211 (defvar smtpmail-address-buffer)
212 (defvar smtpmail-recipient-address-list)
214 (defvar smtpmail-queue-counter 0)
216 ;; Buffer-local variable.
217 (defvar smtpmail-read-point)
219 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
220 smtpmail-queue-index-file))
222 (defconst smtpmail-auth-supported '(cram-md5 plain login)
223 "List of supported SMTP AUTH mechanisms.")
229 (defvar smtpmail-mail-address nil
230 "Value to use for envelope-from address for mail from ambient buffer.")
233 (defun smtpmail-send-it ()
234 (let ((errbuf (if mail-interactive
235 (generate-new-buffer " smtpmail errors")
237 (tembuf (generate-new-buffer " smtpmail temp"))
238 (case-fold-search nil)
240 (mailbuf (current-buffer))
241 ;; Examine this variable now, so that
242 ;; local binding in the mail buffer will take effect.
243 (smtpmail-mail-address
244 (or (and mail-specify-envelope-from (mail-envelope-from))
246 (smtpmail-code-conv-from
247 (if enable-multibyte-characters
248 (let ((sendmail-coding-system smtpmail-code-conv-from))
249 (select-message-coding-system)))))
254 ;; Use the same buffer-file-coding-system as in the mail
255 ;; buffer, otherwise any write-region invocations (e.g., in
256 ;; mail-do-fcc below) will annoy with asking for a suitable
259 ;; This file (`gnus/contrib/smtpmail.el') is only useful for Emacs
260 ;; which doesn't support the third argument (NOMODIFY) of
261 ;; `set-buffer-file-coding-system'.
262 (set-buffer-file-coding-system smtpmail-code-conv-from nil)
263 (set-buffer-modified-p nil)
264 (force-mode-line-update)
265 (insert-buffer-substring mailbuf)
266 (goto-char (point-max))
267 ;; require one newline at the end.
268 (or (= (preceding-char) ?\n)
270 ;; Change header-delimiter to be what sendmail expects.
271 (mail-sendmail-undelimit-header)
272 (setq delimline (point-marker))
273 ;; (sendmail-synch-aliases)
275 (expand-mail-aliases (point-min) delimline))
276 (goto-char (point-min))
277 ;; ignore any blank lines in the header
278 (while (and (re-search-forward "\n\n\n*" delimline t)
279 (< (point) delimline))
280 (replace-match "\n"))
281 (let ((case-fold-search t))
282 ;; We used to process Resent-... headers here,
283 ;; but it was not done properly, and the job
284 ;; is done correctly in smtpmail-deduce-address-list.
285 ;; Don't send out a blank subject line
286 (goto-char (point-min))
287 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
289 ;; This one matches a Subject just before the header delimiter.
290 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
291 (= (match-end 0) delimline))
293 ;; Put the "From:" field in unless for some odd reason
294 ;; they put one in themselves.
295 (goto-char (point-min))
296 (if (not (re-search-forward "^From:" delimline t))
297 (let* ((login smtpmail-mail-address)
298 (fullname (user-full-name)))
299 (cond ((eq mail-from-style 'angles)
300 (insert "From: " fullname)
301 (let ((fullname-start (+ (point-min) 6))
302 (fullname-end (point-marker)))
303 (goto-char fullname-start)
304 ;; Look for a character that cannot appear unquoted
305 ;; according to RFC 822.
306 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
309 ;; Quote fullname, escaping specials.
310 (goto-char fullname-start)
312 (while (re-search-forward "[\"\\]"
314 (replace-match "\\\\\\&" t))
316 (insert " <" login ">\n"))
317 ((eq mail-from-style 'parens)
318 (insert "From: " login " (")
319 (let ((fullname-start (point)))
321 (let ((fullname-end (point-marker)))
322 (goto-char fullname-start)
323 ;; RFC 822 says \ and nonmatching parentheses
324 ;; must be escaped in comments.
325 ;; Escape every instance of ()\ ...
326 (while (re-search-forward "[()\\]" fullname-end 1)
327 (replace-match "\\\\\\&" t))
328 ;; ... then undo escaping of matching parentheses,
329 ;; including matching nested parentheses.
330 (goto-char fullname-start)
331 (while (re-search-forward
332 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
334 (replace-match "\\1(\\3)" t)
335 (goto-char fullname-start))))
337 ((null mail-from-style)
338 (insert "From: " login "\n")))))
339 ;; Insert a `Message-Id:' field if there isn't one yet.
340 (goto-char (point-min))
341 (unless (re-search-forward "^Message-Id:" delimline t)
342 (insert "Message-Id: " (message-make-message-id) "\n"))
343 ;; Insert a `Date:' field if there isn't one yet.
344 (goto-char (point-min))
345 (unless (re-search-forward "^Date:" delimline t)
346 (insert "Date: " (message-make-date) "\n"))
347 ;; Possibly add a MIME header for the current coding system
349 (goto-char (point-min))
350 (and (eq mail-send-nonascii 'mime)
351 (not (re-search-forward "^MIME-version:" delimline t))
352 (progn (skip-chars-forward "\0-\177")
353 (/= (point) (point-max)))
354 smtpmail-code-conv-from
356 (coding-system-get smtpmail-code-conv-from
358 (goto-char delimline)
359 (insert "MIME-version: 1.0\n"
360 "Content-type: text/plain; charset="
361 (symbol-name charset)
362 "\nContent-Transfer-Encoding: 8bit\n")))
363 ;; Insert an extra newline if we need it to work around
364 ;; Sun's bug that swallows newlines.
365 (goto-char (1+ delimline))
366 (if (eval mail-mailer-swallows-blank-line)
368 ;; Find and handle any FCC fields.
369 (goto-char (point-min))
370 (if (re-search-forward "^FCC:" delimline t)
371 ;; Force mail-do-fcc to use the encoding of the mail
372 ;; buffer to encode outgoing messages on FCC files.
373 (let ((coding-system-for-write smtpmail-code-conv-from))
374 (mail-do-fcc delimline)))
376 (with-current-buffer errbuf
381 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
382 (setq smtpmail-recipient-address-list
383 (smtpmail-deduce-address-list tembuf (point-min) delimline))
384 (kill-buffer smtpmail-address-buffer)
386 (smtpmail-do-bcc delimline)
388 (if (not smtpmail-queue-mail)
389 (if (not (null smtpmail-recipient-address-list))
390 (if (not (smtpmail-via-smtp
391 smtpmail-recipient-address-list tembuf))
392 (error "Sending failed; SMTP protocol error"))
393 (error "Sending failed; no recipients"))
397 (format-time-string "%Y-%m-%d_%H:%M:%S")
398 (setq smtpmail-queue-counter
399 (1+ smtpmail-queue-counter)))
401 (file-data (convert-standard-filename file-data))
402 (file-elisp (concat file-data ".el"))
403 (buffer-data (create-file-buffer file-data))
404 (buffer-elisp (create-file-buffer file-elisp))
405 (buffer-scratch "*queue-mail*"))
406 (unless (file-exists-p smtpmail-queue-dir)
407 (make-directory smtpmail-queue-dir t))
408 (with-current-buffer buffer-data
410 (set-buffer-file-coding-system smtpmail-code-conv-from nil)
411 (insert-buffer-substring tembuf)
412 (write-file file-data)
413 (set-buffer buffer-elisp)
416 "(setq smtpmail-recipient-address-list '"
417 (prin1-to-string smtpmail-recipient-address-list)
419 (write-file file-elisp)
420 (set-buffer (generate-new-buffer buffer-scratch))
421 (insert (concat file-data "\n"))
422 (append-to-file (point-min)
424 smtpmail-queue-index)
426 (kill-buffer buffer-scratch)
427 (kill-buffer buffer-data)
428 (kill-buffer buffer-elisp))))
431 (kill-buffer errbuf)))))
434 (defun smtpmail-send-queued-mail ()
435 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
438 ;;; Get index, get first mail, send it, update index, get second
439 ;;; mail, send it, etc...
441 (insert-file-contents smtpmail-queue-index)
442 (goto-char (point-min))
444 (setq file-msg (buffer-substring (point) (line-end-position)))
446 ;; Insert the message literally: it is already encoded as per
447 ;; the MIME headers, and code conversions might guess the
450 (let ((coding-system-for-read 'no-conversion))
451 (insert-file-contents file-msg))
452 (let ((smtpmail-mail-address
453 (or (and mail-specify-envelope-from (mail-envelope-from))
455 (if (not (null smtpmail-recipient-address-list))
456 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
458 (error "Sending failed; SMTP protocol error"))
459 (error "Sending failed; no recipients"))))
460 (delete-file file-msg)
461 (delete-file (concat file-msg ".el"))
462 (delete-region (point-at-bol) (point-at-bol 2)))
463 (write-region (point-min) (point-max) smtpmail-queue-index))))
465 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
467 (defun smtpmail-fqdn ()
468 (if smtpmail-local-domain
469 (concat (system-name) "." smtpmail-local-domain)
472 (defsubst smtpmail-cred-server (cred)
475 (defsubst smtpmail-cred-port (cred)
478 (defsubst smtpmail-cred-key (cred)
481 (defsubst smtpmail-cred-user (cred)
484 (defsubst smtpmail-cred-cert (cred)
487 (defsubst smtpmail-cred-passwd (cred)
490 (defun smtpmail-find-credentials (cred server port)
493 (while (setq el (pop l))
494 (when (and (equal server (smtpmail-cred-server el))
495 (equal port (smtpmail-cred-port el)))
496 (throw 'done el))))))
498 (defun smtpmail-maybe-append-domain (recipient)
499 (if (or (not smtpmail-sendto-domain)
500 (string-match "@" recipient))
502 (concat recipient "@" smtpmail-sendto-domain)))
504 (defun smtpmail-intersection (list1 list2)
507 (when (memq el2 list1)
511 (defvar starttls-extra-args)
512 (defvar starttls-extra-arguments)
514 (defun smtpmail-open-stream (process-buffer host port)
515 (let ((cred (smtpmail-find-credentials
516 smtpmail-starttls-credentials host port)))
517 (if (null (and cred (condition-case ()
520 (call-process (if starttls-use-gnutls
521 starttls-gnutls-program
525 (open-network-stream "SMTP" process-buffer host port)
526 (let* ((cred-key (smtpmail-cred-key cred))
527 (cred-cert (smtpmail-cred-cert cred))
531 (when (and (stringp cred-key) (stringp cred-cert)
533 (setq cred-key (expand-file-name cred-key)))
535 (setq cred-cert (expand-file-name cred-cert))))
536 (list "--key-file" cred-key "--cert-file" cred-cert))))
537 (starttls-extra-arguments
539 starttls-extra-arguments
540 (when (and (stringp cred-key) (stringp cred-cert)
542 (setq cred-key (expand-file-name cred-key)))
544 (setq cred-cert (expand-file-name cred-cert))))
545 (list "--x509keyfile" cred-key "--x509certfile" cred-cert)))))
546 (starttls-open-stream "SMTP" process-buffer host port)))))
548 (defun smtpmail-try-auth-methods (process supported-extensions host port)
549 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
550 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
551 (auth-user (auth-source-user-or-password
552 "login" host (or port "smtp")))
553 (auth-pass (auth-source-user-or-password
554 "password" host (or port "smtp")))
555 (cred (if (and auth-user auth-pass) ; try user-auth-* before netrc-*
556 (list host port auth-user auth-pass)
557 ;; else, if auth-source didn't return them...
558 (if (stringp smtpmail-auth-credentials)
559 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
560 (port-name (format "%s" (or port "smtp")))
561 (hostentry (netrc-machine netrc host port-name
565 (netrc-get hostentry "login")
566 (netrc-get hostentry "password"))))
567 ;; else, try smtpmail-find-credentials since
568 ;; smtpmail-auth-credentials is not a string
569 (smtpmail-find-credentials
570 smtpmail-auth-credentials host port))))
571 (prompt (when cred (format "SMTP password for %s:%s: "
572 (smtpmail-cred-server cred)
573 (smtpmail-cred-port cred))))
575 (or (smtpmail-cred-passwd cred)
577 (format "SMTP password for %s:%s: "
578 (smtpmail-cred-server cred)
579 (smtpmail-cred-port cred))))))
581 (when (and cred mech)
584 (smtpmail-send-command process (upcase (format "AUTH %s" mech)))
585 (if (or (null (car (setq ret (smtpmail-read-response process))))
586 (not (integerp (car ret)))
589 (when (eq (car ret) 334)
590 (let* ((challenge (substring (cadr ret) 4))
591 (decoded (base64-decode-string challenge))
592 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
593 (response (concat (smtpmail-cred-user cred) " " hash))
594 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
595 ;; SMTP auth fails because the SMTP server identifies
596 ;; only the first part of the string (delimited by
597 ;; new line characters) as a response from the
598 ;; client, and the rest as distinct commands.
600 ;; In my case, the response string is 80 characters
601 ;; long. Without the no-line-break option for
602 ;; base64-encode-sting, only the first 76 characters
603 ;; are taken as a response to the server, and the
604 ;; authentication fails.
605 (encoded (base64-encode-string response t)))
606 (smtpmail-send-command process (format "%s" encoded))
607 (if (or (null (car (setq ret (smtpmail-read-response process))))
608 (not (integerp (car ret)))
610 (throw 'done nil)))))
612 (smtpmail-send-command process "AUTH LOGIN")
613 (if (or (null (car (setq ret (smtpmail-read-response process))))
614 (not (integerp (car ret)))
617 (smtpmail-send-command
618 process (base64-encode-string (smtpmail-cred-user cred) t))
619 (if (or (null (car (setq ret (smtpmail-read-response process))))
620 (not (integerp (car ret)))
623 (smtpmail-send-command process (base64-encode-string passwd t))
624 (if (or (null (car (setq ret (smtpmail-read-response process))))
625 (not (integerp (car ret)))
629 ;; We used to send an empty initial request, and wait for an
630 ;; empty response, and then send the password, but this
631 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
632 ;; is not sent if the server did not advertise AUTH PLAIN in
633 ;; the EHLO response. See RFC 2554 for more info.
634 (smtpmail-send-command process
635 (concat "AUTH PLAIN "
636 (base64-encode-string
638 (smtpmail-cred-user cred)
641 (if (or (null (car (setq ret (smtpmail-read-response process))))
642 (not (integerp (car ret)))
643 (not (equal (car ret) 235)))
647 (error "Mechanism %s not implemented" mech)))
648 ;; Remember the password.
649 (when (and (not (stringp smtpmail-auth-credentials))
650 (null (smtpmail-cred-passwd cred)))
651 (setcar (cdr (cdr (cdr cred))) passwd)))))
653 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
655 (host (or smtpmail-smtp-server
656 (error "`smtpmail-smtp-server' not defined")))
657 (port smtpmail-smtp-service)
658 ;; smtpmail-mail-address should be set to the appropriate
659 ;; buffer-local value by the caller, but in case not:
660 (envelope-from (or smtpmail-mail-address
661 (and mail-specify-envelope-from
662 (mail-envelope-from))
667 (supported-extensions '()))
670 ;; get or create the trace buffer
672 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
674 ;; clear the trace buffer of old output
675 (with-current-buffer process-buffer
676 (setq buffer-undo-list t)
679 ;; open the connection to the server
680 (setq process (smtpmail-open-stream process-buffer host port))
681 (and (null process) (throw 'done nil))
683 ;; set the send-filter
684 (set-process-filter process 'smtpmail-process-filter)
686 (with-current-buffer process-buffer
687 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
688 (make-local-variable 'smtpmail-read-point)
689 (setq smtpmail-read-point (point-min))
692 (if (or (null (car (setq greeting (smtpmail-read-response process))))
693 (not (integerp (car greeting)))
694 (>= (car greeting) 400))
702 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
704 (if (or (null (car (setq response-code
705 (smtpmail-read-response process))))
706 (not (integerp (car response-code)))
707 (>= (car response-code) 400))
710 (smtpmail-send-command
711 process (format "HELO %s" (smtpmail-fqdn)))
713 (if (or (null (car (setq response-code
714 (smtpmail-read-response process))))
715 (not (integerp (car response-code)))
716 (>= (car response-code) 400))
718 (dolist (line (cdr (cdr response-code)))
719 (let ((name (mapcar (lambda (s) (intern (downcase s)))
720 (split-string (substring line 4) "[ ]"))))
721 (and (eq (length name) 1)
722 (setq name (car name)))
724 (cond ((memq (if (consp name) (car name) name)
725 '(verb xvrb 8bitmime onex xone
729 auth=login auth starttls))
730 (setq supported-extensions
731 (cons name supported-extensions)))
732 (smtpmail-warn-about-unknown-extensions
733 (message "Unknown extension %s" name)))))))
736 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
737 (member 'starttls supported-extensions)
738 (numberp (process-id process)))
740 (smtpmail-send-command process (format "STARTTLS"))
741 (if (or (null (car (setq response-code (smtpmail-read-response process))))
742 (not (integerp (car response-code)))
743 (>= (car response-code) 400))
745 (starttls-negotiate process)
746 (setq do-starttls nil))
747 (setq do-ehlo nil))))
749 (smtpmail-try-auth-methods process supported-extensions host port)
751 (if (or (member 'onex supported-extensions)
752 (member 'xone supported-extensions))
754 (smtpmail-send-command process (format "ONEX"))
755 (if (or (null (car (setq response-code (smtpmail-read-response process))))
756 (not (integerp (car response-code)))
757 (>= (car response-code) 400))
760 (if (and smtpmail-debug-verb
761 (or (member 'verb supported-extensions)
762 (member 'xvrb supported-extensions)))
764 (smtpmail-send-command process (format "VERB"))
765 (if (or (null (car (setq response-code (smtpmail-read-response process))))
766 (not (integerp (car response-code)))
767 (>= (car response-code) 400))
770 (if (member 'xusr supported-extensions)
772 (smtpmail-send-command process (format "XUSR"))
773 (if (or (null (car (setq response-code (smtpmail-read-response process))))
774 (not (integerp (car response-code)))
775 (>= (car response-code) 400))
778 ;; MAIL FROM:<sender>
780 (if (or (member 'size supported-extensions)
781 (assoc 'size supported-extensions))
783 (with-current-buffer smtpmail-text-buffer
785 (+ (- (point-max) (point-min))
786 ;; Add one byte for each change-of-line
787 ;; because of CR-LF representation:
788 (count-lines (point-min) (point-max)))))
791 (if (member '8bitmime supported-extensions)
793 ;; Code should be added here that transforms
794 ;; the contents of the message buffer into
795 ;; something the receiving SMTP can handle.
796 ;; For a receiver that supports 8BITMIME, this
797 ;; may mean converting BINARY to BASE64, or
798 ;; adding Content-Transfer-Encoding and the
799 ;; other MIME headers. The code should also
800 ;; return an indication of what encoding the
801 ;; message buffer is now, i.e. ASCII or
807 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
808 (smtpmail-send-command process (format "MAIL FROM:<%s>%s%s"
813 (if (or (null (car (setq response-code (smtpmail-read-response process))))
814 (not (integerp (car response-code)))
815 (>= (car response-code) 400))
819 ;; RCPT TO:<recipient>
821 (while (not (null (nth n recipient)))
822 (smtpmail-send-command process (format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient))))
825 (setq response-code (smtpmail-read-response process))
826 (if (or (null (car response-code))
827 (not (integerp (car response-code)))
828 (>= (car response-code) 400))
834 (smtpmail-send-command process "DATA")
836 (if (or (null (car (setq response-code (smtpmail-read-response process))))
837 (not (integerp (car response-code)))
838 (>= (car response-code) 400))
843 (smtpmail-send-data process smtpmail-text-buffer)
846 (smtpmail-send-command process ".")
848 (if (or (null (car (setq response-code (smtpmail-read-response process))))
849 (not (integerp (car response-code)))
850 (>= (car response-code) 400))
855 ; (smtpmail-send-command process "QUIT")
856 ; (and (null (car (smtpmail-read-response process)))
860 (with-current-buffer (process-buffer process)
861 (smtpmail-send-command process "QUIT")
862 (smtpmail-read-response process)
864 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
865 ; (not (integerp (car response-code)))
866 ; (>= (car response-code) 400))
869 (delete-process process)
870 (unless smtpmail-debug-info
871 (kill-buffer process-buffer)))))))
874 (defun smtpmail-process-filter (process output)
875 (with-current-buffer (process-buffer process)
876 (goto-char (point-max))
879 (defun smtpmail-read-response (process)
880 (let ((case-fold-search nil)
881 (response-strings nil)
882 (response-continue t)
883 (return-value '(nil ()))
886 (while response-continue
887 (goto-char smtpmail-read-point)
888 (while (not (search-forward "\r\n" nil t))
889 (unless (memq (process-status process) '(open run))
891 (accept-process-output process)
892 (goto-char smtpmail-read-point))
894 (setq match-end (point))
895 (setq response-strings
896 (cons (buffer-substring smtpmail-read-point (- match-end 2))
899 (goto-char smtpmail-read-point)
900 (if (looking-at "[0-9]+ ")
901 (let ((begin (match-beginning 0))
903 (if smtpmail-debug-info
904 (message "%s" (car response-strings)))
906 (setq smtpmail-read-point match-end)
908 ;; ignore lines that start with "0"
909 (if (looking-at "0[0-9]+ ")
911 (setq response-continue nil)
913 (cons (string-to-number
914 (buffer-substring begin end))
915 (nreverse response-strings)))))
917 (if (looking-at "[0-9]+-")
918 (progn (if smtpmail-debug-info
919 (message "%s" (car response-strings)))
920 (setq smtpmail-read-point match-end)
921 (setq response-continue t))
923 (setq smtpmail-read-point match-end)
924 (setq response-continue nil)
926 (cons nil (nreverse response-strings)))))))
927 (setq smtpmail-read-point match-end))
931 (defun smtpmail-send-command (process command)
932 (goto-char (point-max))
933 (if (= (aref command 0) ?P)
934 (insert "PASS <omitted>\r\n")
935 (insert command "\r\n"))
936 (setq smtpmail-read-point (point))
937 (process-send-string process command)
938 (process-send-string process "\r\n"))
940 (defun smtpmail-send-data-1 (process data)
941 (goto-char (point-max))
943 (if (and (multibyte-string-p data)
944 smtpmail-code-conv-from)
945 (setq data (string-as-multibyte
946 (encode-coding-string data smtpmail-code-conv-from))))
948 (if smtpmail-debug-info
949 (insert data "\r\n"))
951 (setq smtpmail-read-point (point))
952 ;; Escape "." at start of a line
953 (if (eq (string-to-char data) ?.)
954 (process-send-string process "."))
955 (process-send-string process data)
956 (process-send-string process "\r\n")
959 (defun smtpmail-send-data (process buffer)
960 (let ((data-continue t) sending-data)
961 (with-current-buffer buffer
962 (goto-char (point-min)))
964 (with-current-buffer buffer
965 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
967 (setq data-continue (not (eobp))))
968 (smtpmail-send-data-1 process sending-data))))
970 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
971 "Get address list suitable for smtp RCPT TO: <address>."
973 (with-current-buffer smtpmail-address-buffer
976 ((case-fold-search t)
977 (simple-address-list "")
981 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
982 (goto-char (point-min))
983 ;; RESENT-* fields should stop processing of regular fields.
986 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
988 "^Resent-\\(to\\|cc\\|bcc\\):"
989 "^\\(To:\\|Cc:\\|Bcc:\\)")))
991 (while (re-search-forward addr-regexp header-end t)
993 (setq this-line (match-beginning 0))
995 ;; get any continuation lines
996 (while (and (looking-at "^[ \t]+") (< (point) header-end))
998 (setq this-line-end (point-marker))
999 (setq simple-address-list
1000 (concat simple-address-list " "
1001 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
1004 (insert " " simple-address-list "\n")
1005 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
1006 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
1007 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
1009 (goto-char (point-min))
1010 ;; tidyness in case hook is not robust when it looks at this
1011 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
1013 (goto-char (point-min))
1014 (let (recipient-address-list)
1015 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
1017 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
1018 recipient-address-list))
1020 (setq smtpmail-recipient-address-list recipient-address-list))
1028 (defun smtpmail-do-bcc (header-end)
1029 "Delete [Resent-]BCC: and their continuation lines from the header area.
1030 There may be multiple BCC: lines, and each may have arbitrarily
1031 many continuation lines."
1032 (let ((case-fold-search t))
1034 (goto-char (point-min))
1035 ;; iterate over all BCC: lines
1036 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
1037 (delete-region (match-beginning 0)
1038 (progn (forward-line 1) (point)))
1039 ;; get rid of any continuation lines
1040 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
1041 (replace-match ""))))))
1046 ;; arch-tag: a76992df-6d71-43b7-9e72-4bacc6c05466
1047 ;;; smtpmail.el ends here