]> code.delx.au - gnu-emacs/blob - lisp/mail/smtpmail.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / mail / smtpmail.el
1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
2
3 ;; Copyright (C) 1995, 1996, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
5
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>
13 ;; Keywords: mail
14
15 ;; This file is part of GNU Emacs.
16
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.
21
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.
26
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/>.
29
30 ;;; Commentary:
31
32 ;; Send Mail to smtp host from smtpmail temp buffer.
33
34 ;; Please add these lines in your .emacs(_emacs) or use customize.
35 ;;
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).
48
49 ;; To queue mail, set smtpmail-queue-mail to t and use
50 ;; smtpmail-send-queued-mail to send.
51
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
56 ;; support below.
57
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
61
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
66
67 ;;; Code:
68
69 (require 'sendmail)
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-user-or-password "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 "
126 :type '(choice (const nil) string)
127 :group 'smtpmail)
128
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
132 be verbose as well."
133 :type 'boolean
134 :group 'smtpmail)
135
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."
139 :type 'boolean
140 :group 'smtpmail)
141
142 (defcustom smtpmail-code-conv-from nil
143 "Coding system for encoding outgoing mail.
144 Used for the value of `sendmail-coding-system' when
145 `select-message-coding-system' is called. "
146 :type 'coding-system
147 :group 'smtpmail)
148
149 (defcustom smtpmail-queue-mail nil
150 "Non-nil means mail is queued; otherwise it is sent immediately.
151 If queued, it is stored in the directory `smtpmail-queue-dir'
152 and sent with `smtpmail-send-queued-mail'."
153 :type 'boolean
154 :group 'smtpmail)
155
156 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
157 "Directory where `smtpmail.el' stores queued mail."
158 :type 'directory
159 :group 'smtpmail)
160
161 (defcustom smtpmail-auth-credentials "~/.authinfo"
162 "Specify username and password for servers, directly or via .netrc file.
163 This variable can either be a filename pointing to a file in netrc(5)
164 format, or list of four-element lists that contain, in order,
165 `servername' (a string), `port' (an integer), `user' (a string) and
166 `password' (a string, or nil to query the user when needed). If you
167 need to enter a `realm' too, add it to the user string, so that it
168 looks like `user@realm'."
169 :type '(choice file
170 (repeat (list (string :tag "Server")
171 (integer :tag "Port")
172 (string :tag "Username")
173 (choice (const :tag "Query when needed" nil)
174 (string :tag "Password")))))
175 :version "22.1"
176 :group 'smtpmail)
177
178 (defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
179 "Specify STARTTLS keys and certificates for servers.
180 This is a list of four-element list with `servername' (a string),
181 `port' (an integer), `key' (a filename) and `certificate' (a
182 filename).
183 If you do not have a certificate/key pair, leave the `key' and
184 `certificate' fields as `nil'. A key/certificate pair is only
185 needed if you want to use X.509 client authenticated
186 connections."
187 :type '(repeat (list (string :tag "Server")
188 (integer :tag "Port")
189 (file :tag "Key")
190 (file :tag "Certificate")))
191 :version "21.1"
192 :group 'smtpmail)
193
194 (defcustom smtpmail-warn-about-unknown-extensions nil
195 "If set, print warnings about unknown SMTP extensions.
196 This is mainly useful for development purposes, to learn about
197 new SMTP extensions that might be useful to support."
198 :type 'boolean
199 :version "21.1"
200 :group 'smtpmail)
201
202 (defcustom smtpmail-queue-index-file "index"
203 "File name of queued mail index.
204 This is relative to `smtpmail-queue-dir'."
205 :type 'string
206 :group 'smtpmail)
207
208 ;; End of customizable variables.
209
210
211 (defvar smtpmail-address-buffer)
212 (defvar smtpmail-recipient-address-list)
213
214 (defvar smtpmail-queue-counter 0)
215
216 ;; Buffer-local variable.
217 (defvar smtpmail-read-point)
218
219 (defconst smtpmail-auth-supported '(cram-md5 plain login)
220 "List of supported SMTP AUTH mechanisms.")
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 (save-excursion
245 (set-buffer tembuf)
246 (erase-buffer)
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
250 ;; encoding.
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)
256 (insert ?\n))
257 ;; Change header-delimiter to be what sendmail expects.
258 (mail-sendmail-undelimit-header)
259 (setq delimline (point-marker))
260 ;; (sendmail-synch-aliases)
261 (if mail-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)
275 (replace-match "")
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))
279 (replace-match "")))
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^-~]"
294 fullname-end 1)
295 (progn
296 ;; Quote fullname, escaping specials.
297 (goto-char fullname-start)
298 (insert "\"")
299 (while (re-search-forward "[\"\\]"
300 fullname-end 1)
301 (replace-match "\\\\\\&" t))
302 (insert "\""))))
303 (insert " <" login ">\n"))
304 ((eq mail-from-style 'parens)
305 (insert "From: " login " (")
306 (let ((fullname-start (point)))
307 (insert fullname)
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 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
320 fullname-end 1)
321 (replace-match "\\1(\\3)" t)
322 (goto-char fullname-start))))
323 (insert ")\n"))
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
335 (let (charset)
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
342 (setq charset
343 (coding-system-get smtpmail-code-conv-from
344 'mime-charset))
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)
354 (newline))
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)))
362 (if mail-interactive
363 (with-current-buffer errbuf
364 (erase-buffer))))
365 ;;
366 ;;
367 ;;
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)
372
373 (smtpmail-do-bcc delimline)
374 ; Send or queue
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"))
381 (let* ((file-data
382 (expand-file-name
383 (format "%s_%i"
384 (format-time-string "%Y-%m-%d_%H:%M:%S")
385 (setq smtpmail-queue-counter
386 (1+ smtpmail-queue-counter)))
387 smtpmail-queue-dir))
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
396 (erase-buffer)
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)
401 (erase-buffer)
402 (insert (concat
403 "(setq smtpmail-recipient-address-list '"
404 (prin1-to-string smtpmail-recipient-address-list)
405 ")\n"))
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)
410 (point-max)
411 (expand-file-name smtpmail-queue-index-file
412 smtpmail-queue-dir)))
413 (kill-buffer buffer-scratch)
414 (kill-buffer buffer-data)
415 (kill-buffer buffer-elisp))))
416 (kill-buffer tembuf)
417 (if (bufferp errbuf)
418 (kill-buffer errbuf)))))
419
420 ;;;###autoload
421 (defun smtpmail-send-queued-mail ()
422 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
423 (interactive)
424 (with-temp-buffer
425 ;;; Get index, get first mail, send it, update index, get second
426 ;;; mail, send it, etc...
427 (let ((file-msg "")
428 (qfile (expand-file-name smtpmail-queue-index-file
429 smtpmail-queue-dir)))
430 (insert-file-contents qfile)
431 (goto-char (point-min))
432 (while (not (eobp))
433 (setq file-msg (buffer-substring (point) (line-end-position)))
434 (load file-msg)
435 ;; Insert the message literally: it is already encoded as per
436 ;; the MIME headers, and code conversions might guess the
437 ;; encoding wrongly.
438 (with-temp-buffer
439 (let ((coding-system-for-read 'no-conversion))
440 (insert-file-contents file-msg))
441 (let ((smtpmail-mail-address
442 (or (and mail-specify-envelope-from (mail-envelope-from))
443 user-mail-address)))
444 (if (not (null smtpmail-recipient-address-list))
445 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
446 (current-buffer)))
447 (error "Sending failed; SMTP protocol error"))
448 (error "Sending failed; no recipients"))))
449 (delete-file file-msg)
450 (delete-file (concat file-msg ".el"))
451 (delete-region (point-at-bol) (point-at-bol 2)))
452 (write-region (point-min) (point-max) qfile))))
453
454 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
455
456 (defun smtpmail-fqdn ()
457 (if smtpmail-local-domain
458 (concat (system-name) "." smtpmail-local-domain)
459 (system-name)))
460
461 (defsubst smtpmail-cred-server (cred)
462 (nth 0 cred))
463
464 (defsubst smtpmail-cred-port (cred)
465 (nth 1 cred))
466
467 (defsubst smtpmail-cred-key (cred)
468 (nth 2 cred))
469
470 (defsubst smtpmail-cred-user (cred)
471 (nth 2 cred))
472
473 (defsubst smtpmail-cred-cert (cred)
474 (nth 3 cred))
475
476 (defsubst smtpmail-cred-passwd (cred)
477 (nth 3 cred))
478
479 (defun smtpmail-find-credentials (cred server port)
480 (catch 'done
481 (let ((l cred) el)
482 (while (setq el (pop l))
483 (when (and (equal server (smtpmail-cred-server el))
484 (equal port (smtpmail-cred-port el)))
485 (throw 'done el))))))
486
487 (defun smtpmail-maybe-append-domain (recipient)
488 (if (or (not smtpmail-sendto-domain)
489 (string-match "@" recipient))
490 recipient
491 (concat recipient "@" smtpmail-sendto-domain)))
492
493 (defun smtpmail-intersection (list1 list2)
494 (let ((result nil))
495 (dolist (el2 list2)
496 (when (memq el2 list1)
497 (push el2 result)))
498 (nreverse result)))
499
500 (defvar starttls-extra-args)
501 (defvar starttls-extra-arguments)
502
503 (defun smtpmail-open-stream (process-buffer host port)
504 (let ((cred (smtpmail-find-credentials
505 smtpmail-starttls-credentials host port)))
506 (if (null (and cred (condition-case ()
507 (with-no-warnings
508 (require 'starttls)
509 (call-process (if starttls-use-gnutls
510 starttls-gnutls-program
511 starttls-program)))
512 (error nil))))
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 smtpmail-auth-supported mechs)))
543 (auth-user (auth-source-user-or-password
544 "login" host (or port "smtp")))
545 (auth-pass (auth-source-user-or-password
546 "password" host (or port "smtp")))
547 (cred (if (and auth-user auth-pass) ; try user-auth-* before netrc-*
548 (list host port auth-user auth-pass)
549 ;; else, if auth-source didn't return them...
550 (if (stringp smtpmail-auth-credentials)
551 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
552 (port-name (format "%s" (or port "smtp")))
553 (hostentry (netrc-machine netrc host port-name
554 port-name)))
555 (when hostentry
556 (list host port
557 (netrc-get hostentry "login")
558 (netrc-get hostentry "password"))))
559 ;; else, try smtpmail-find-credentials since
560 ;; smtpmail-auth-credentials is not a string
561 (smtpmail-find-credentials
562 smtpmail-auth-credentials host port))))
563 (prompt (when cred (format "SMTP password for %s:%s: "
564 (smtpmail-cred-server cred)
565 (smtpmail-cred-port cred))))
566 (passwd (when cred
567 (or (smtpmail-cred-passwd cred)
568 (password-read prompt prompt))))
569 ret)
570 (when (and cred mech)
571 (cond
572 ((eq mech 'cram-md5)
573 (smtpmail-send-command process (upcase (format "AUTH %s" mech)))
574 (if (or (null (car (setq ret (smtpmail-read-response process))))
575 (not (integerp (car ret)))
576 (>= (car ret) 400))
577 (throw 'done nil))
578 (when (eq (car ret) 334)
579 (let* ((challenge (substring (cadr ret) 4))
580 (decoded (base64-decode-string challenge))
581 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
582 (response (concat (smtpmail-cred-user cred) " " hash))
583 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
584 ;; SMTP auth fails because the SMTP server identifies
585 ;; only the first part of the string (delimited by
586 ;; new line characters) as a response from the
587 ;; client, and the rest as distinct commands.
588
589 ;; In my case, the response string is 80 characters
590 ;; long. Without the no-line-break option for
591 ;; base64-encode-sting, only the first 76 characters
592 ;; are taken as a response to the server, and the
593 ;; authentication fails.
594 (encoded (base64-encode-string response t)))
595 (smtpmail-send-command process (format "%s" encoded))
596 (if (or (null (car (setq ret (smtpmail-read-response process))))
597 (not (integerp (car ret)))
598 (>= (car ret) 400))
599 (throw 'done nil)))))
600 ((eq mech 'login)
601 (smtpmail-send-command process "AUTH LOGIN")
602 (if (or (null (car (setq ret (smtpmail-read-response process))))
603 (not (integerp (car ret)))
604 (>= (car ret) 400))
605 (throw 'done nil))
606 (smtpmail-send-command
607 process (base64-encode-string (smtpmail-cred-user cred) t))
608 (if (or (null (car (setq ret (smtpmail-read-response process))))
609 (not (integerp (car ret)))
610 (>= (car ret) 400))
611 (throw 'done nil))
612 (smtpmail-send-command process (base64-encode-string passwd t))
613 (if (or (null (car (setq ret (smtpmail-read-response process))))
614 (not (integerp (car ret)))
615 (>= (car ret) 400))
616 (throw 'done nil)))
617 ((eq mech 'plain)
618 ;; We used to send an empty initial request, and wait for an
619 ;; empty response, and then send the password, but this
620 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
621 ;; is not sent if the server did not advertise AUTH PLAIN in
622 ;; the EHLO response. See RFC 2554 for more info.
623 (smtpmail-send-command process
624 (concat "AUTH PLAIN "
625 (base64-encode-string
626 (concat "\0"
627 (smtpmail-cred-user cred)
628 "\0"
629 passwd) t)))
630 (if (or (null (car (setq ret (smtpmail-read-response process))))
631 (not (integerp (car ret)))
632 (not (equal (car ret) 235)))
633 (throw 'done nil)))
634
635 (t
636 (error "Mechanism %s not implemented" mech)))
637 ;; Remember the password.
638 (when (null (smtpmail-cred-passwd cred))
639 (password-cache-add prompt passwd)))))
640
641 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
642 (let ((process nil)
643 (host (or smtpmail-smtp-server
644 (error "`smtpmail-smtp-server' not defined")))
645 (port smtpmail-smtp-service)
646 ;; smtpmail-mail-address should be set to the appropriate
647 ;; buffer-local value by the caller, but in case not:
648 (envelope-from (or smtpmail-mail-address
649 (and mail-specify-envelope-from
650 (mail-envelope-from))
651 user-mail-address))
652 response-code
653 greeting
654 process-buffer
655 (supported-extensions '()))
656 (unwind-protect
657 (catch 'done
658 ;; get or create the trace buffer
659 (setq process-buffer
660 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
661
662 ;; clear the trace buffer of old output
663 (with-current-buffer process-buffer
664 (setq buffer-undo-list t)
665 (erase-buffer))
666
667 ;; open the connection to the server
668 (setq process (smtpmail-open-stream process-buffer host port))
669 (and (null process) (throw 'done nil))
670
671 ;; set the send-filter
672 (set-process-filter process 'smtpmail-process-filter)
673
674 (with-current-buffer process-buffer
675 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
676 (make-local-variable 'smtpmail-read-point)
677 (setq smtpmail-read-point (point-min))
678
679
680 (if (or (null (car (setq greeting (smtpmail-read-response process))))
681 (not (integerp (car greeting)))
682 (>= (car greeting) 400))
683 (throw 'done nil)
684 )
685
686 (let ((do-ehlo t)
687 (do-starttls t))
688 (while do-ehlo
689 ;; EHLO
690 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
691
692 (if (or (null (car (setq response-code
693 (smtpmail-read-response process))))
694 (not (integerp (car response-code)))
695 (>= (car response-code) 400))
696 (progn
697 ;; HELO
698 (smtpmail-send-command
699 process (format "HELO %s" (smtpmail-fqdn)))
700
701 (if (or (null (car (setq response-code
702 (smtpmail-read-response process))))
703 (not (integerp (car response-code)))
704 (>= (car response-code) 400))
705 (throw 'done nil)))
706 (dolist (line (cdr (cdr response-code)))
707 (let ((name
708 (with-case-table ascii-case-table
709 (mapcar (lambda (s) (intern (downcase s)))
710 (split-string (substring line 4) "[ ]")))))
711 (and (eq (length name) 1)
712 (setq name (car name)))
713 (and name
714 (cond ((memq (if (consp name) (car name) name)
715 '(verb xvrb 8bitmime onex xone
716 expn size dsn etrn
717 enhancedstatuscodes
718 help xusr
719 auth=login auth starttls))
720 (setq supported-extensions
721 (cons name supported-extensions)))
722 (smtpmail-warn-about-unknown-extensions
723 (message "Unknown extension %s" name)))))))
724
725 (if (and do-starttls
726 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
727 (member 'starttls supported-extensions)
728 (numberp (process-id process)))
729 (progn
730 (smtpmail-send-command process (format "STARTTLS"))
731 (if (or (null (car (setq response-code (smtpmail-read-response process))))
732 (not (integerp (car response-code)))
733 (>= (car response-code) 400))
734 (throw 'done nil))
735 (starttls-negotiate process)
736 (setq do-starttls nil))
737 (setq do-ehlo nil))))
738
739 (smtpmail-try-auth-methods process supported-extensions host port)
740
741 (if (or (member 'onex supported-extensions)
742 (member 'xone supported-extensions))
743 (progn
744 (smtpmail-send-command process (format "ONEX"))
745 (if (or (null (car (setq response-code (smtpmail-read-response process))))
746 (not (integerp (car response-code)))
747 (>= (car response-code) 400))
748 (throw 'done nil))))
749
750 (if (and smtpmail-debug-verb
751 (or (member 'verb supported-extensions)
752 (member 'xvrb supported-extensions)))
753 (progn
754 (smtpmail-send-command process (format "VERB"))
755 (if (or (null (car (setq response-code (smtpmail-read-response process))))
756 (not (integerp (car response-code)))
757 (>= (car response-code) 400))
758 (throw 'done nil))))
759
760 (if (member 'xusr supported-extensions)
761 (progn
762 (smtpmail-send-command process (format "XUSR"))
763 (if (or (null (car (setq response-code (smtpmail-read-response process))))
764 (not (integerp (car response-code)))
765 (>= (car response-code) 400))
766 (throw 'done nil))))
767
768 ;; MAIL FROM:<sender>
769 (let ((size-part
770 (if (or (member 'size supported-extensions)
771 (assoc 'size supported-extensions))
772 (format " SIZE=%d"
773 (with-current-buffer smtpmail-text-buffer
774 ;; size estimate:
775 (+ (- (point-max) (point-min))
776 ;; Add one byte for each change-of-line
777 ;; because of CR-LF representation:
778 (count-lines (point-min) (point-max)))))
779 ""))
780 (body-part
781 (if (member '8bitmime supported-extensions)
782 ;; FIXME:
783 ;; Code should be added here that transforms
784 ;; the contents of the message buffer into
785 ;; something the receiving SMTP can handle.
786 ;; For a receiver that supports 8BITMIME, this
787 ;; may mean converting BINARY to BASE64, or
788 ;; adding Content-Transfer-Encoding and the
789 ;; other MIME headers. The code should also
790 ;; return an indication of what encoding the
791 ;; message buffer is now, i.e. ASCII or
792 ;; 8BITMIME.
793 (if nil
794 " BODY=8BITMIME"
795 "")
796 "")))
797 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
798 (smtpmail-send-command process (format "MAIL FROM:<%s>%s%s"
799 envelope-from
800 size-part
801 body-part))
802
803 (if (or (null (car (setq response-code (smtpmail-read-response process))))
804 (not (integerp (car response-code)))
805 (>= (car response-code) 400))
806 (throw 'done nil)
807 ))
808
809 ;; RCPT TO:<recipient>
810 (let ((n 0))
811 (while (not (null (nth n recipient)))
812 (smtpmail-send-command process (format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient))))
813 (setq n (1+ n))
814
815 (setq response-code (smtpmail-read-response process))
816 (if (or (null (car response-code))
817 (not (integerp (car response-code)))
818 (>= (car response-code) 400))
819 (throw 'done nil)
820 )
821 ))
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
832 ;; Mail contents
833 (smtpmail-send-data process smtpmail-text-buffer)
834
835 ;;DATA end "."
836 (smtpmail-send-command process ".")
837
838 (if (or (null (car (setq response-code (smtpmail-read-response process))))
839 (not (integerp (car response-code)))
840 (>= (car response-code) 400))
841 (throw 'done nil)
842 )
843
844 ;;QUIT
845 ; (smtpmail-send-command process "QUIT")
846 ; (and (null (car (smtpmail-read-response process)))
847 ; (throw 'done nil))
848 t ))
849 (if process
850 (with-current-buffer (process-buffer process)
851 (smtpmail-send-command process "QUIT")
852 (smtpmail-read-response process)
853
854 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
855 ; (not (integerp (car response-code)))
856 ; (>= (car response-code) 400))
857 ; (throw 'done nil)
858 ; )
859 (delete-process process)
860 (unless smtpmail-debug-info
861 (kill-buffer process-buffer)))))))
862
863
864 (defun smtpmail-process-filter (process output)
865 (with-current-buffer (process-buffer process)
866 (goto-char (point-max))
867 (insert output)))
868
869 (defun smtpmail-read-response (process)
870 (let ((case-fold-search nil)
871 (response-strings nil)
872 (response-continue t)
873 (return-value '(nil ()))
874 match-end)
875 (catch 'done
876 (while response-continue
877 (goto-char smtpmail-read-point)
878 (while (not (search-forward "\r\n" nil t))
879 (unless (memq (process-status process) '(open run))
880 (throw 'done nil))
881 (accept-process-output process)
882 (goto-char smtpmail-read-point))
883
884 (setq match-end (point))
885 (setq response-strings
886 (cons (buffer-substring smtpmail-read-point (- match-end 2))
887 response-strings))
888
889 (goto-char smtpmail-read-point)
890 (if (looking-at "[0-9]+ ")
891 (let ((begin (match-beginning 0))
892 (end (match-end 0)))
893 (if smtpmail-debug-info
894 (message "%s" (car response-strings)))
895
896 (setq smtpmail-read-point match-end)
897
898 ;; ignore lines that start with "0"
899 (if (looking-at "0[0-9]+ ")
900 nil
901 (setq response-continue nil)
902 (setq return-value
903 (cons (string-to-number
904 (buffer-substring begin end))
905 (nreverse response-strings)))))
906
907 (if (looking-at "[0-9]+-")
908 (progn (if smtpmail-debug-info
909 (message "%s" (car response-strings)))
910 (setq smtpmail-read-point match-end)
911 (setq response-continue t))
912 (progn
913 (setq smtpmail-read-point match-end)
914 (setq response-continue nil)
915 (setq return-value
916 (cons nil (nreverse response-strings)))))))
917 (setq smtpmail-read-point match-end))
918 return-value))
919
920
921 (defun smtpmail-send-command (process command)
922 (goto-char (point-max))
923 (if (= (aref command 0) ?P)
924 (insert "PASS <omitted>\r\n")
925 (insert command "\r\n"))
926 (setq smtpmail-read-point (point))
927 (process-send-string process command)
928 (process-send-string process "\r\n"))
929
930 (defun smtpmail-send-data-1 (process data)
931 (goto-char (point-max))
932
933 (if (and (multibyte-string-p data)
934 smtpmail-code-conv-from)
935 (setq data (string-as-multibyte
936 (encode-coding-string data smtpmail-code-conv-from))))
937
938 (if smtpmail-debug-info
939 (insert data "\r\n"))
940
941 (setq smtpmail-read-point (point))
942 ;; Escape "." at start of a line
943 (if (eq (string-to-char data) ?.)
944 (process-send-string process "."))
945 (process-send-string process data)
946 (process-send-string process "\r\n")
947 )
948
949 (defun smtpmail-send-data (process buffer)
950 (let ((data-continue t) sending-data)
951 (with-current-buffer buffer
952 (goto-char (point-min)))
953 (while data-continue
954 (with-current-buffer buffer
955 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
956 (end-of-line 2)
957 (setq data-continue (not (eobp))))
958 (smtpmail-send-data-1 process sending-data))))
959
960 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
961 "Get address list suitable for smtp RCPT TO: <address>."
962 (unwind-protect
963 (with-current-buffer smtpmail-address-buffer
964 (erase-buffer)
965 (let
966 ((case-fold-search t)
967 (simple-address-list "")
968 this-line
969 this-line-end
970 addr-regexp)
971 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
972 (goto-char (point-min))
973 ;; RESENT-* fields should stop processing of regular fields.
974 (save-excursion
975 (setq addr-regexp
976 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
977 header-end t)
978 "^Resent-\\(to\\|cc\\|bcc\\):"
979 "^\\(To:\\|Cc:\\|Bcc:\\)")))
980
981 (while (re-search-forward addr-regexp header-end t)
982 (replace-match "")
983 (setq this-line (match-beginning 0))
984 (forward-line 1)
985 ;; get any continuation lines
986 (while (and (looking-at "^[ \t]+") (< (point) header-end))
987 (forward-line 1))
988 (setq this-line-end (point-marker))
989 (setq simple-address-list
990 (concat simple-address-list " "
991 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
992 )
993 (erase-buffer)
994 (insert " " simple-address-list "\n")
995 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
996 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
997 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
998
999 (goto-char (point-min))
1000 ;; tidyness in case hook is not robust when it looks at this
1001 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
1002
1003 (goto-char (point-min))
1004 (let (recipient-address-list)
1005 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
1006 (backward-char 1)
1007 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
1008 recipient-address-list))
1009 )
1010 (setq smtpmail-recipient-address-list recipient-address-list))
1011
1012 )
1013 )
1014 )
1015 )
1016
1017
1018 (defun smtpmail-do-bcc (header-end)
1019 "Delete [Resent-]BCC: and their continuation lines from the header area.
1020 There may be multiple BCC: lines, and each may have arbitrarily
1021 many continuation lines."
1022 (let ((case-fold-search t))
1023 (save-excursion
1024 (goto-char (point-min))
1025 ;; iterate over all BCC: lines
1026 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
1027 (delete-region (match-beginning 0)
1028 (progn (forward-line 1) (point)))
1029 ;; get rid of any continuation lines
1030 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
1031 (replace-match ""))))))
1032
1033
1034 (provide 'smtpmail)
1035
1036 ;; arch-tag: a76992df-6d71-43b7-9e72-4bacc6c05466
1037 ;;; smtpmail.el ends here