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