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