Merge from emacs-23; up to 2010-06-11T21:26:13Z!lekktu@gmail.com.
[bpt/emacs.git] / lisp / mail / smtpmail.el
1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
2
3 ;; Copyright (C) 1995-1996, 2001-2011 Free Software Foundation, Inc.
4
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>
12 ;; Keywords: mail
13
14 ;; This file is part of GNU Emacs.
15
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 3 of the License, or
19 ;; (at your option) any later version.
20
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.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28
29 ;;; Commentary:
30
31 ;; Send Mail to smtp host from smtpmail temp buffer.
32
33 ;; Please add these lines in your .emacs(_emacs) or use customize.
34 ;;
35 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
36 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
37 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
38 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
39 ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
40 ;;(setq smtpmail-debug-info t) ; only to debug problems
41 ;;(setq smtpmail-auth-credentials ; or use ~/.authinfo
42 ;; '(("YOUR SMTP HOST" 25 "username" "password")))
43 ;;(setq smtpmail-starttls-credentials
44 ;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
45 ;; Where the 25 equals the value of `smtpmail-smtp-service', it can be an
46 ;; integer or a string, just as long as they match (eq).
47
48 ;; To queue mail, set `smtpmail-queue-mail' to t and use
49 ;; `smtpmail-send-queued-mail' to send.
50
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
55 ;; support below.
56
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
60
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
65
66 ;;; Code:
67
68 (require 'sendmail)
69 (autoload 'starttls-any-program-available "starttls")
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")
79 (autoload 'password-read "password-cache")
80 (autoload 'auth-source-search "auth-source")
81
82 ;;;
83 (defgroup smtpmail nil
84 "SMTP protocol for sending mail."
85 :group 'mail)
86
87
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)
92 :group 'smtpmail)
93
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)
98 :group 'smtpmail)
99
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"))
104 :group 'smtpmail)
105
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)
111 :group 'smtpmail)
112
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.)
118
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:
123 RCPT TO: <someone>
124 501 <someone>: recipient address must contain a domain."
125 :type '(choice (const nil) string)
126 :group 'smtpmail)
127
128 (defcustom smtpmail-debug-info nil
129 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
130 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
131 be verbose as well."
132 :type 'boolean
133 :group 'smtpmail)
134
135 (defcustom smtpmail-debug-verb nil
136 "Whether this library sends the SMTP VERB command or not.
137 The commands enables verbose information from the SMTP server."
138 :type 'boolean
139 :group 'smtpmail)
140
141 (defcustom smtpmail-code-conv-from nil
142 "Coding system for encoding outgoing mail.
143 Used for the value of `sendmail-coding-system' when
144 `select-message-coding-system' is called. "
145 :type 'coding-system
146 :group 'smtpmail)
147
148 (defcustom smtpmail-queue-mail nil
149 "Non-nil means mail is queued; otherwise it is sent immediately.
150 If queued, it is stored in the directory `smtpmail-queue-dir'
151 and sent with `smtpmail-send-queued-mail'."
152 :type 'boolean
153 :group 'smtpmail)
154
155 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
156 "Directory where `smtpmail.el' stores queued mail."
157 :type 'directory
158 :group 'smtpmail)
159
160 (defcustom smtpmail-auth-credentials "~/.authinfo"
161 "Specify username and password for servers, directly or via .netrc file.
162 This variable can either be a filename pointing to a file in netrc(5)
163 format, or list of four-element lists that contain, in order,
164 `servername' (a string), `port' (an integer), `user' (a string) and
165 `password' (a string, or nil to query the user when needed). If you
166 need to enter a `realm' too, add it to the user string, so that it
167 looks like `user@realm'."
168 :type '(choice file
169 (repeat (list (string :tag "Server")
170 (integer :tag "Port")
171 (string :tag "Username")
172 (choice (const :tag "Query when needed" nil)
173 (string :tag "Password")))))
174 :version "22.1"
175 :group 'smtpmail)
176
177 (defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
178 "Specify STARTTLS keys and certificates for servers.
179 This is a list of four-element list with `servername' (a string),
180 `port' (an integer), `key' (a filename) and `certificate' (a
181 filename).
182 If you do not have a certificate/key pair, leave the `key' and
183 `certificate' fields as `nil'. A key/certificate pair is only
184 needed if you want to use X.509 client authenticated
185 connections."
186 :type '(repeat (list (string :tag "Server")
187 (integer :tag "Port")
188 (file :tag "Key")
189 (file :tag "Certificate")))
190 :version "21.1"
191 :group 'smtpmail)
192
193 (defcustom smtpmail-warn-about-unknown-extensions nil
194 "If set, print warnings about unknown SMTP extensions.
195 This is mainly useful for development purposes, to learn about
196 new SMTP extensions that might be useful to support."
197 :type 'boolean
198 :version "21.1"
199 :group 'smtpmail)
200
201 (defcustom smtpmail-queue-index-file "index"
202 "File name of queued mail index.
203 This is relative to `smtpmail-queue-dir'."
204 :type 'string
205 :group 'smtpmail)
206
207 ;; End of customizable variables.
208
209
210 (defvar smtpmail-address-buffer)
211 (defvar smtpmail-recipient-address-list)
212
213 (defvar smtpmail-queue-counter 0)
214
215 ;; Buffer-local variable.
216 (defvar smtpmail-read-point)
217
218 (defconst smtpmail-auth-supported '(cram-md5 plain login)
219 "List of supported SMTP AUTH mechanisms.
220 The list is in preference order.")
221
222 (defvar smtpmail-mail-address nil
223 "Value to use for envelope-from address for mail from ambient buffer.")
224
225 ;;;###autoload
226 (defun smtpmail-send-it ()
227 (let ((errbuf (if mail-interactive
228 (generate-new-buffer " smtpmail errors")
229 0))
230 (tembuf (generate-new-buffer " smtpmail temp"))
231 (case-fold-search nil)
232 delimline
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))
238 user-mail-address))
239 (smtpmail-code-conv-from
240 (if enable-multibyte-characters
241 (let ((sendmail-coding-system smtpmail-code-conv-from))
242 (select-message-coding-system)))))
243 (unwind-protect
244 (with-current-buffer tembuf
245 (erase-buffer)
246 ;; Use the same `buffer-file-coding-system' as in the mail
247 ;; buffer, otherwise any `write-region' invocations (e.g., in
248 ;; mail-do-fcc below) will annoy with asking for a suitable
249 ;; encoding.
250 (set-buffer-file-coding-system smtpmail-code-conv-from nil t)
251 (insert-buffer-substring mailbuf)
252 (goto-char (point-max))
253 ;; require one newline at the end.
254 (or (= (preceding-char) ?\n)
255 (insert ?\n))
256 ;; Change header-delimiter to be what sendmail expects.
257 (mail-sendmail-undelimit-header)
258 (setq delimline (point-marker))
259 ;; (sendmail-synch-aliases)
260 (if mail-aliases
261 (expand-mail-aliases (point-min) delimline))
262 (goto-char (point-min))
263 ;; ignore any blank lines in the header
264 (while (and (re-search-forward "\n\n\n*" delimline t)
265 (< (point) delimline))
266 (replace-match "\n"))
267 (let ((case-fold-search t))
268 ;; We used to process Resent-... headers here,
269 ;; but it was not done properly, and the job
270 ;; is done correctly in `smtpmail-deduce-address-list'.
271 ;; Don't send out a blank subject line
272 (goto-char (point-min))
273 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
274 (replace-match "")
275 ;; This one matches a Subject just before the header delimiter.
276 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
277 (= (match-end 0) delimline))
278 (replace-match "")))
279 ;; Put the "From:" field in unless for some odd reason
280 ;; they put one in themselves.
281 (goto-char (point-min))
282 (if (not (re-search-forward "^From:" delimline t))
283 (let* ((login smtpmail-mail-address)
284 (fullname (user-full-name)))
285 (cond ((eq mail-from-style 'angles)
286 (insert "From: " fullname)
287 (let ((fullname-start (+ (point-min) 6))
288 (fullname-end (point-marker)))
289 (goto-char fullname-start)
290 ;; Look for a character that cannot appear unquoted
291 ;; according to RFC 822.
292 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
293 fullname-end 1)
294 (progn
295 ;; Quote fullname, escaping specials.
296 (goto-char fullname-start)
297 (insert "\"")
298 (while (re-search-forward "[\"\\]"
299 fullname-end 1)
300 (replace-match "\\\\\\&" t))
301 (insert "\""))))
302 (insert " <" login ">\n"))
303 ((eq mail-from-style 'parens)
304 (insert "From: " login " (")
305 (let ((fullname-start (point)))
306 (insert fullname)
307 (let ((fullname-end (point-marker)))
308 (goto-char fullname-start)
309 ;; RFC 822 says \ and nonmatching parentheses
310 ;; must be escaped in comments.
311 ;; Escape every instance of ()\ ...
312 (while (re-search-forward "[()\\]" fullname-end 1)
313 (replace-match "\\\\\\&" t))
314 ;; ... then undo escaping of matching parentheses,
315 ;; including matching nested parentheses.
316 (goto-char fullname-start)
317 (while (re-search-forward
318 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
319 fullname-end 1)
320 (replace-match "\\1(\\3)" t)
321 (goto-char fullname-start))))
322 (insert ")\n"))
323 ((null mail-from-style)
324 (insert "From: " login "\n")))))
325 ;; Insert a `Message-Id:' field if there isn't one yet.
326 (goto-char (point-min))
327 (unless (re-search-forward "^Message-Id:" delimline t)
328 (insert "Message-Id: " (message-make-message-id) "\n"))
329 ;; Insert a `Date:' field if there isn't one yet.
330 (goto-char (point-min))
331 (unless (re-search-forward "^Date:" delimline t)
332 (insert "Date: " (message-make-date) "\n"))
333 ;; Possibly add a MIME header for the current coding system
334 (let (charset)
335 (goto-char (point-min))
336 (and (eq mail-send-nonascii 'mime)
337 (not (re-search-forward "^MIME-version:" delimline t))
338 (progn (skip-chars-forward "\0-\177")
339 (/= (point) (point-max)))
340 smtpmail-code-conv-from
341 (setq charset
342 (coding-system-get smtpmail-code-conv-from
343 'mime-charset))
344 (goto-char delimline)
345 (insert "MIME-version: 1.0\n"
346 "Content-type: text/plain; charset="
347 (symbol-name charset)
348 "\nContent-Transfer-Encoding: 8bit\n")))
349 ;; Insert an extra newline if we need it to work around
350 ;; Sun's bug that swallows newlines.
351 (goto-char (1+ delimline))
352 (if (eval mail-mailer-swallows-blank-line)
353 (newline))
354 ;; Find and handle any FCC fields.
355 (goto-char (point-min))
356 (if (re-search-forward "^FCC:" delimline t)
357 ;; Force `mail-do-fcc' to use the encoding of the mail
358 ;; buffer to encode outgoing messages on FCC files.
359 (let ((coding-system-for-write smtpmail-code-conv-from))
360 (mail-do-fcc delimline)))
361 (if mail-interactive
362 (with-current-buffer errbuf
363 (erase-buffer))))
364 ;; Encode the header according to RFC2047.
365 (mail-encode-header (point-min) delimline)
366 ;;
367 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
368 (setq smtpmail-recipient-address-list
369 (smtpmail-deduce-address-list tembuf (point-min) delimline))
370 (kill-buffer smtpmail-address-buffer)
371
372 (smtpmail-do-bcc delimline)
373 ;; Send or queue
374 (if (not smtpmail-queue-mail)
375 (if (not (null smtpmail-recipient-address-list))
376 (if (not (smtpmail-via-smtp
377 smtpmail-recipient-address-list tembuf))
378 (error "Sending failed; SMTP protocol error"))
379 (error "Sending failed; no recipients"))
380 (let* ((file-data
381 (expand-file-name
382 (format "%s_%i"
383 (format-time-string "%Y-%m-%d_%H:%M:%S")
384 (setq smtpmail-queue-counter
385 (1+ smtpmail-queue-counter)))
386 smtpmail-queue-dir))
387 (file-data (convert-standard-filename file-data))
388 (file-elisp (concat file-data ".el"))
389 (buffer-data (create-file-buffer file-data))
390 (buffer-elisp (create-file-buffer file-elisp))
391 (buffer-scratch "*queue-mail*"))
392 (unless (file-exists-p smtpmail-queue-dir)
393 (make-directory smtpmail-queue-dir t))
394 (with-current-buffer buffer-data
395 (erase-buffer)
396 (set-buffer-file-coding-system
397 ;; We will be reading the file with no-conversion in
398 ;; smtpmail-send-queued-mail below, so write it out
399 ;; with Unix EOLs.
400 (coding-system-change-eol-conversion
401 (or smtpmail-code-conv-from 'undecided)
402 'unix)
403 nil t)
404 (insert-buffer-substring tembuf)
405 (write-file file-data)
406 (set-buffer buffer-elisp)
407 (erase-buffer)
408 (insert (concat
409 "(setq smtpmail-recipient-address-list '"
410 (prin1-to-string smtpmail-recipient-address-list)
411 ")\n"))
412 (write-file file-elisp)
413 (set-buffer (generate-new-buffer buffer-scratch))
414 (insert (concat file-data "\n"))
415 (append-to-file (point-min)
416 (point-max)
417 (expand-file-name smtpmail-queue-index-file
418 smtpmail-queue-dir)))
419 (kill-buffer buffer-scratch)
420 (kill-buffer buffer-data)
421 (kill-buffer buffer-elisp))))
422 (kill-buffer tembuf)
423 (if (bufferp errbuf)
424 (kill-buffer errbuf)))))
425
426 ;;;###autoload
427 (defun smtpmail-send-queued-mail ()
428 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
429 (interactive)
430 (with-temp-buffer
431 ;; Get index, get first mail, send it, update index, get second
432 ;; mail, send it, etc...
433 (let ((file-msg "")
434 (qfile (expand-file-name smtpmail-queue-index-file
435 smtpmail-queue-dir)))
436 (insert-file-contents qfile)
437 (goto-char (point-min))
438 (while (not (eobp))
439 (setq file-msg (buffer-substring (point) (line-end-position)))
440 (load file-msg)
441 ;; Insert the message literally: it is already encoded as per
442 ;; the MIME headers, and code conversions might guess the
443 ;; encoding wrongly.
444 (with-temp-buffer
445 (let ((coding-system-for-read 'no-conversion))
446 (insert-file-contents file-msg))
447 (let ((smtpmail-mail-address
448 (or (and mail-specify-envelope-from (mail-envelope-from))
449 user-mail-address)))
450 (if (not (null smtpmail-recipient-address-list))
451 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
452 (current-buffer)))
453 (error "Sending failed; SMTP protocol error"))
454 (error "Sending failed; no recipients"))))
455 (delete-file file-msg)
456 (delete-file (concat file-msg ".el"))
457 (delete-region (point-at-bol) (point-at-bol 2)))
458 (write-region (point-min) (point-max) qfile))))
459
460 ;; (defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
461
462 (defun smtpmail-fqdn ()
463 (if smtpmail-local-domain
464 (concat (system-name) "." smtpmail-local-domain)
465 (system-name)))
466
467 (defsubst smtpmail-cred-server (cred)
468 (nth 0 cred))
469
470 (defsubst smtpmail-cred-port (cred)
471 (nth 1 cred))
472
473 (defsubst smtpmail-cred-key (cred)
474 (nth 2 cred))
475
476 (defsubst smtpmail-cred-user (cred)
477 (nth 2 cred))
478
479 (defsubst smtpmail-cred-cert (cred)
480 (nth 3 cred))
481
482 (defsubst smtpmail-cred-passwd (cred)
483 (nth 3 cred))
484
485 (defun smtpmail-find-credentials (cred server port)
486 (catch 'done
487 (let ((l cred) el)
488 (while (setq el (pop l))
489 (when (and (equal server (smtpmail-cred-server el))
490 (equal port (smtpmail-cred-port el)))
491 (throw 'done el))))))
492
493 (defun smtpmail-maybe-append-domain (recipient)
494 (if (or (not smtpmail-sendto-domain)
495 (string-match "@" recipient))
496 recipient
497 (concat recipient "@" smtpmail-sendto-domain)))
498
499 (defun smtpmail-intersection (list1 list2)
500 (let ((result nil))
501 (dolist (el2 list2)
502 (when (memq el2 list1)
503 (push el2 result)))
504 (nreverse result)))
505
506 (defvar starttls-extra-args)
507 (defvar starttls-extra-arguments)
508
509 (defun smtpmail-open-stream (process-buffer host port)
510 (let ((cred (smtpmail-find-credentials
511 smtpmail-starttls-credentials host port)))
512 (if (null (and cred (starttls-any-program-available)))
513 ;; The normal case.
514 (open-network-stream "SMTP" process-buffer host port)
515 (let* ((cred-key (smtpmail-cred-key cred))
516 (cred-cert (smtpmail-cred-cert cred))
517 (starttls-extra-args
518 (append
519 starttls-extra-args
520 (when (and (stringp cred-key) (stringp cred-cert)
521 (file-regular-p
522 (setq cred-key (expand-file-name cred-key)))
523 (file-regular-p
524 (setq cred-cert (expand-file-name cred-cert))))
525 (list "--key-file" cred-key "--cert-file" cred-cert))))
526 (starttls-extra-arguments
527 (append
528 starttls-extra-arguments
529 (when (and (stringp cred-key) (stringp cred-cert)
530 (file-regular-p
531 (setq cred-key (expand-file-name cred-key)))
532 (file-regular-p
533 (setq cred-cert (expand-file-name cred-cert))))
534 (list "--x509keyfile" cred-key "--x509certfile" cred-cert)))))
535 (starttls-open-stream "SMTP" process-buffer host port)))))
536
537 ;; `password-read' autoloads password-cache.
538 (declare-function password-cache-add "password-cache" (key password))
539
540 (defun smtpmail-try-auth-methods (process supported-extensions host port)
541 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
542 (mech (car (smtpmail-intersection mechs smtpmail-auth-supported)))
543 (auth-info (auth-source-search :max 1
544 :host host
545 :port (or port "smtp")))
546 (auth-user (plist-get (nth 0 auth-info) :user))
547 (auth-pass (plist-get (nth 0 auth-info) :secret))
548 (auth-pass (if (functionp auth-pass)
549 (funcall auth-pass)
550 auth-pass))
551 (cred (if (and auth-user auth-pass) ; try user-auth-* before netrc-*
552 (list host port auth-user auth-pass)
553 ;; else, if auth-source didn't return them...
554 (if (stringp smtpmail-auth-credentials)
555 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
556 (port-name (format "%s" (or port "smtp")))
557 (hostentry (netrc-machine netrc host port-name
558 port-name)))
559 (when hostentry
560 (list host port
561 (netrc-get hostentry "login")
562 (netrc-get hostentry "password"))))
563 ;; else, try `smtpmail-find-credentials' since
564 ;; `smtpmail-auth-credentials' is not a string
565 (smtpmail-find-credentials
566 smtpmail-auth-credentials host port))))
567 (prompt (when cred (format "SMTP password for %s:%s: "
568 (smtpmail-cred-server cred)
569 (smtpmail-cred-port cred))))
570 (passwd (when cred
571 (or (smtpmail-cred-passwd cred)
572 (password-read prompt prompt))))
573 ret)
574 (when (and cred mech)
575 (cond
576 ((eq mech 'cram-md5)
577 (smtpmail-send-command process (upcase (format "AUTH %s" mech)))
578 (if (or (null (car (setq ret (smtpmail-read-response process))))
579 (not (integerp (car ret)))
580 (>= (car ret) 400))
581 (throw 'done nil))
582 (when (eq (car ret) 334)
583 (let* ((challenge (substring (cadr ret) 4))
584 (decoded (base64-decode-string challenge))
585 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
586 (response (concat (smtpmail-cred-user cred) " " hash))
587 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
588 ;; SMTP auth fails because the SMTP server identifies
589 ;; only the first part of the string (delimited by
590 ;; new line characters) as a response from the
591 ;; client, and the rest as distinct commands.
592
593 ;; In my case, the response string is 80 characters
594 ;; long. Without the no-line-break option for
595 ;; `base64-encode-string', only the first 76 characters
596 ;; are taken as a response to the server, and the
597 ;; authentication fails.
598 (encoded (base64-encode-string response t)))
599 (smtpmail-send-command process (format "%s" encoded))
600 (if (or (null (car (setq ret (smtpmail-read-response process))))
601 (not (integerp (car ret)))
602 (>= (car ret) 400))
603 (throw 'done nil)))))
604 ((eq mech 'login)
605 (smtpmail-send-command process "AUTH LOGIN")
606 (if (or (null (car (setq ret (smtpmail-read-response process))))
607 (not (integerp (car ret)))
608 (>= (car ret) 400))
609 (throw 'done nil))
610 (smtpmail-send-command
611 process (base64-encode-string (smtpmail-cred-user cred) t))
612 (if (or (null (car (setq ret (smtpmail-read-response process))))
613 (not (integerp (car ret)))
614 (>= (car ret) 400))
615 (throw 'done nil))
616 (smtpmail-send-command process (base64-encode-string passwd t))
617 (if (or (null (car (setq ret (smtpmail-read-response process))))
618 (not (integerp (car ret)))
619 (>= (car ret) 400))
620 (throw 'done nil)))
621 ((eq mech 'plain)
622 ;; We used to send an empty initial request, and wait for an
623 ;; empty response, and then send the password, but this
624 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
625 ;; is not sent if the server did not advertise AUTH PLAIN in
626 ;; the EHLO response. See RFC 2554 for more info.
627 (smtpmail-send-command process
628 (concat "AUTH PLAIN "
629 (base64-encode-string
630 (concat "\0"
631 (smtpmail-cred-user cred)
632 "\0"
633 passwd) t)))
634 (if (or (null (car (setq ret (smtpmail-read-response process))))
635 (not (integerp (car ret)))
636 (not (equal (car ret) 235)))
637 (throw 'done nil)))
638
639 (t
640 (error "Mechanism %s not implemented" mech)))
641 ;; Remember the password.
642 (when (null (smtpmail-cred-passwd cred))
643 (password-cache-add prompt passwd)))))
644
645 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
646 (let ((process nil)
647 (host (or smtpmail-smtp-server
648 (error "`smtpmail-smtp-server' not defined")))
649 (port smtpmail-smtp-service)
650 ;; `smtpmail-mail-address' should be set to the appropriate
651 ;; buffer-local value by the caller, but in case not:
652 (envelope-from (or smtpmail-mail-address
653 (and mail-specify-envelope-from
654 (mail-envelope-from))
655 user-mail-address))
656 response-code
657 greeting
658 process-buffer
659 (supported-extensions '()))
660 (unwind-protect
661 (catch 'done
662 ;; get or create the trace buffer
663 (setq process-buffer
664 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
665
666 ;; clear the trace buffer of old output
667 (with-current-buffer process-buffer
668 (setq buffer-undo-list t)
669 (erase-buffer))
670
671 ;; open the connection to the server
672 (setq process (smtpmail-open-stream process-buffer host port))
673 (and (null process) (throw 'done nil))
674
675 ;; set the send-filter
676 (set-process-filter process 'smtpmail-process-filter)
677
678 (with-current-buffer process-buffer
679 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
680 (make-local-variable 'smtpmail-read-point)
681 (setq smtpmail-read-point (point-min))
682
683
684 (if (or (null (car (setq greeting (smtpmail-read-response process))))
685 (not (integerp (car greeting)))
686 (>= (car greeting) 400))
687 (throw 'done nil))
688
689 (let ((do-ehlo t)
690 (do-starttls t))
691 (while do-ehlo
692 ;; EHLO
693 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
694
695 (if (or (null (car (setq response-code
696 (smtpmail-read-response process))))
697 (not (integerp (car response-code)))
698 (>= (car response-code) 400))
699 (progn
700 ;; HELO
701 (smtpmail-send-command
702 process (format "HELO %s" (smtpmail-fqdn)))
703
704 (if (or (null (car (setq response-code
705 (smtpmail-read-response process))))
706 (not (integerp (car response-code)))
707 (>= (car response-code) 400))
708 (throw 'done nil)))
709 (dolist (line (cdr (cdr response-code)))
710 (let ((name
711 (with-case-table ascii-case-table
712 (mapcar (lambda (s) (intern (downcase s)))
713 (split-string (substring line 4) "[ ]")))))
714 (and (eq (length name) 1)
715 (setq name (car name)))
716 (and name
717 (cond ((memq (if (consp name) (car name) name)
718 '(verb xvrb 8bitmime onex xone
719 expn size dsn etrn
720 enhancedstatuscodes
721 help xusr
722 auth=login auth starttls))
723 (setq supported-extensions
724 (cons name supported-extensions)))
725 (smtpmail-warn-about-unknown-extensions
726 (message "Unknown extension %s" name)))))))
727
728 (if (and do-starttls
729 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
730 (member 'starttls supported-extensions)
731 (numberp (process-id process)))
732 (progn
733 (smtpmail-send-command process (format "STARTTLS"))
734 (if (or (null (car (setq response-code (smtpmail-read-response process))))
735 (not (integerp (car response-code)))
736 (>= (car response-code) 400))
737 (throw 'done nil))
738 (starttls-negotiate process)
739 (setq do-starttls nil))
740 (setq do-ehlo nil))))
741
742 (smtpmail-try-auth-methods process supported-extensions host port)
743
744 (if (or (member 'onex supported-extensions)
745 (member 'xone supported-extensions))
746 (progn
747 (smtpmail-send-command process (format "ONEX"))
748 (if (or (null (car (setq response-code (smtpmail-read-response process))))
749 (not (integerp (car response-code)))
750 (>= (car response-code) 400))
751 (throw 'done nil))))
752
753 (if (and smtpmail-debug-verb
754 (or (member 'verb supported-extensions)
755 (member 'xvrb supported-extensions)))
756 (progn
757 (smtpmail-send-command process (format "VERB"))
758 (if (or (null (car (setq response-code (smtpmail-read-response process))))
759 (not (integerp (car response-code)))
760 (>= (car response-code) 400))
761 (throw 'done nil))))
762
763 (if (member 'xusr supported-extensions)
764 (progn
765 (smtpmail-send-command process (format "XUSR"))
766 (if (or (null (car (setq response-code (smtpmail-read-response process))))
767 (not (integerp (car response-code)))
768 (>= (car response-code) 400))
769 (throw 'done nil))))
770
771 ;; MAIL FROM:<sender>
772 (let ((size-part
773 (if (or (member 'size supported-extensions)
774 (assoc 'size supported-extensions))
775 (format " SIZE=%d"
776 (with-current-buffer smtpmail-text-buffer
777 ;; size estimate:
778 (+ (- (point-max) (point-min))
779 ;; Add one byte for each change-of-line
780 ;; because of CR-LF representation:
781 (count-lines (point-min) (point-max)))))
782 ""))
783 (body-part
784 (if (member '8bitmime supported-extensions)
785 ;; FIXME:
786 ;; Code should be added here that transforms
787 ;; the contents of the message buffer into
788 ;; something the receiving SMTP can handle.
789 ;; For a receiver that supports 8BITMIME, this
790 ;; may mean converting BINARY to BASE64, or
791 ;; adding Content-Transfer-Encoding and the
792 ;; other MIME headers. The code should also
793 ;; return an indication of what encoding the
794 ;; message buffer is now, i.e. ASCII or
795 ;; 8BITMIME.
796 (if nil
797 " BODY=8BITMIME"
798 "")
799 "")))
800 ;; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
801 (smtpmail-send-command process (format "MAIL FROM:<%s>%s%s"
802 envelope-from
803 size-part
804 body-part))
805
806 (if (or (null (car (setq response-code (smtpmail-read-response process))))
807 (not (integerp (car response-code)))
808 (>= (car response-code) 400))
809 (throw 'done nil)))
810
811 ;; RCPT TO:<recipient>
812 (let ((n 0))
813 (while (not (null (nth n recipient)))
814 (smtpmail-send-command process (format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient))))
815 (setq n (1+ n))
816
817 (setq response-code (smtpmail-read-response process))
818 (if (or (null (car response-code))
819 (not (integerp (car response-code)))
820 (>= (car response-code) 400))
821 (throw 'done nil))))
822
823 ;; DATA
824 (smtpmail-send-command process "DATA")
825
826 (if (or (null (car (setq response-code (smtpmail-read-response process))))
827 (not (integerp (car response-code)))
828 (>= (car response-code) 400))
829 (throw 'done nil))
830
831 ;; Mail contents
832 (smtpmail-send-data process smtpmail-text-buffer)
833
834 ;; DATA end "."
835 (smtpmail-send-command process ".")
836
837 (if (or (null (car (setq response-code (smtpmail-read-response process))))
838 (not (integerp (car response-code)))
839 (>= (car response-code) 400))
840 (throw 'done nil))
841
842 ;; QUIT
843 ;; (smtpmail-send-command process "QUIT")
844 ;; (and (null (car (smtpmail-read-response process)))
845 ;; (throw 'done nil))
846 t))
847 (if process
848 (with-current-buffer (process-buffer process)
849 (smtpmail-send-command process "QUIT")
850 (smtpmail-read-response process)
851
852 ;; (if (or (null (car (setq response-code (smtpmail-read-response process))))
853 ;; (not (integerp (car response-code)))
854 ;; (>= (car response-code) 400))
855 ;; (throw 'done nil))
856 (delete-process process)
857 (unless smtpmail-debug-info
858 (kill-buffer process-buffer)))))))
859
860
861 (defun smtpmail-process-filter (process output)
862 (with-current-buffer (process-buffer process)
863 (goto-char (point-max))
864 (insert output)))
865
866 (defun smtpmail-read-response (process)
867 (let ((case-fold-search nil)
868 (response-strings nil)
869 (response-continue t)
870 (return-value '(nil ()))
871 match-end)
872 (catch 'done
873 (while response-continue
874 (goto-char smtpmail-read-point)
875 (while (not (search-forward "\r\n" nil t))
876 (unless (memq (process-status process) '(open run))
877 (throw 'done nil))
878 (accept-process-output process)
879 (goto-char smtpmail-read-point))
880
881 (setq match-end (point))
882 (setq response-strings
883 (cons (buffer-substring smtpmail-read-point (- match-end 2))
884 response-strings))
885
886 (goto-char smtpmail-read-point)
887 (if (looking-at "[0-9]+ ")
888 (let ((begin (match-beginning 0))
889 (end (match-end 0)))
890 (if smtpmail-debug-info
891 (message "%s" (car response-strings)))
892
893 (setq smtpmail-read-point match-end)
894
895 ;; ignore lines that start with "0"
896 (if (looking-at "0[0-9]+ ")
897 nil
898 (setq response-continue nil)
899 (setq return-value
900 (cons (string-to-number
901 (buffer-substring begin end))
902 (nreverse response-strings)))))
903
904 (if (looking-at "[0-9]+-")
905 (progn (if smtpmail-debug-info
906 (message "%s" (car response-strings)))
907 (setq smtpmail-read-point match-end)
908 (setq response-continue t))
909 (progn
910 (setq smtpmail-read-point match-end)
911 (setq response-continue nil)
912 (setq return-value
913 (cons nil (nreverse response-strings)))))))
914 (setq smtpmail-read-point match-end))
915 return-value))
916
917
918 (defun smtpmail-send-command (process command)
919 (goto-char (point-max))
920 (if (= (aref command 0) ?P)
921 (insert "PASS <omitted>\r\n")
922 (insert command "\r\n"))
923 (setq smtpmail-read-point (point))
924 (process-send-string process command)
925 (process-send-string process "\r\n"))
926
927 (defun smtpmail-send-data-1 (process data)
928 (goto-char (point-max))
929
930 (if (and (multibyte-string-p data)
931 smtpmail-code-conv-from)
932 (setq data (string-as-multibyte
933 (encode-coding-string data smtpmail-code-conv-from))))
934
935 (if smtpmail-debug-info
936 (insert data "\r\n"))
937
938 (setq smtpmail-read-point (point))
939 ;; Escape "." at start of a line
940 (if (eq (string-to-char data) ?.)
941 (process-send-string process "."))
942 (process-send-string process data)
943 (process-send-string process "\r\n"))
944
945 (defun smtpmail-send-data (process buffer)
946 (let ((data-continue t) sending-data)
947 (with-current-buffer buffer
948 (goto-char (point-min)))
949 (while data-continue
950 (with-current-buffer buffer
951 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
952 (end-of-line 2)
953 (setq data-continue (not (eobp))))
954 (smtpmail-send-data-1 process sending-data))))
955
956 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
957 "Get address list suitable for smtp RCPT TO: <address>."
958 (unwind-protect
959 (with-current-buffer smtpmail-address-buffer
960 (erase-buffer)
961 (let ((case-fold-search t)
962 (simple-address-list "")
963 this-line
964 this-line-end
965 addr-regexp)
966 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
967 (goto-char (point-min))
968 ;; RESENT-* fields should stop processing of regular fields.
969 (save-excursion
970 (setq addr-regexp
971 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
972 header-end t)
973 "^Resent-\\(to\\|cc\\|bcc\\):"
974 "^\\(To:\\|Cc:\\|Bcc:\\)")))
975
976 (while (re-search-forward addr-regexp header-end t)
977 (replace-match "")
978 (setq this-line (match-beginning 0))
979 (forward-line 1)
980 ;; get any continuation lines
981 (while (and (looking-at "^[ \t]+") (< (point) header-end))
982 (forward-line 1))
983 (setq this-line-end (point-marker))
984 (setq simple-address-list
985 (concat simple-address-list " "
986 (mail-strip-quoted-names (buffer-substring this-line this-line-end)))))
987 (erase-buffer)
988 (insert " " simple-address-list "\n")
989 (subst-char-in-region (point-min) (point-max) 10 ? t) ; newline --> blank
990 (subst-char-in-region (point-min) (point-max) ?, ? t) ; comma --> blank
991 (subst-char-in-region (point-min) (point-max) 9 ? t) ; tab --> blank
992
993 (goto-char (point-min))
994 ;; tidyness in case hook is not robust when it looks at this
995 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
996
997 (goto-char (point-min))
998 (let (recipient-address-list)
999 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
1000 (backward-char 1)
1001 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
1002 recipient-address-list)))
1003 (setq smtpmail-recipient-address-list recipient-address-list))))))
1004
1005 (defun smtpmail-do-bcc (header-end)
1006 "Delete [Resent-]BCC: and their continuation lines from the header area.
1007 There may be multiple BCC: lines, and each may have arbitrarily
1008 many continuation lines."
1009 (let ((case-fold-search t))
1010 (save-excursion
1011 (goto-char (point-min))
1012 ;; iterate over all BCC: lines
1013 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
1014 (delete-region (match-beginning 0)
1015 (progn (forward-line 1) (point)))
1016 ;; get rid of any continuation lines
1017 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
1018 (replace-match ""))))))
1019
1020 (provide 'smtpmail)
1021
1022 ;;; smtpmail.el ends here