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