Trailing whitepace deleted.
[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 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 of `user-mail-address' in 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 (smtpmail-mail-address user-mail-address)
227 (smtpmail-code-conv-from
228 (if enable-multibyte-characters
229 (let ((sendmail-coding-system smtpmail-code-conv-from))
230 (select-message-coding-system)))))
231 (unwind-protect
232 (save-excursion
233 (set-buffer tembuf)
234 (erase-buffer)
235 (insert-buffer-substring mailbuf)
236 (goto-char (point-max))
237 ;; require one newline at the end.
238 (or (= (preceding-char) ?\n)
239 (insert ?\n))
240 ;; Change header-delimiter to be what sendmail expects.
241 (mail-sendmail-undelimit-header)
242 (setq delimline (point-marker))
243 ;; (sendmail-synch-aliases)
244 (if mail-aliases
245 (expand-mail-aliases (point-min) delimline))
246 (goto-char (point-min))
247 ;; ignore any blank lines in the header
248 (while (and (re-search-forward "\n\n\n*" delimline t)
249 (< (point) delimline))
250 (replace-match "\n"))
251 (let ((case-fold-search t))
252 ;; We used to process Resent-... headers here,
253 ;; but it was not done properly, and the job
254 ;; is done correctly in smtpmail-deduce-address-list.
255 ;; Don't send out a blank subject line
256 (goto-char (point-min))
257 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
258 (replace-match "")
259 ;; This one matches a Subject just before the header delimiter.
260 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
261 (= (match-end 0) delimline))
262 (replace-match "")))
263 ;; Put the "From:" field in unless for some odd reason
264 ;; they put one in themselves.
265 (goto-char (point-min))
266 (if (not (re-search-forward "^From:" delimline t))
267 (let* ((login smtpmail-mail-address)
268 (fullname (user-full-name)))
269 (cond ((eq mail-from-style 'angles)
270 (insert "From: " fullname)
271 (let ((fullname-start (+ (point-min) 6))
272 (fullname-end (point-marker)))
273 (goto-char fullname-start)
274 ;; Look for a character that cannot appear unquoted
275 ;; according to RFC 822.
276 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
277 fullname-end 1)
278 (progn
279 ;; Quote fullname, escaping specials.
280 (goto-char fullname-start)
281 (insert "\"")
282 (while (re-search-forward "[\"\\]"
283 fullname-end 1)
284 (replace-match "\\\\\\&" t))
285 (insert "\""))))
286 (insert " <" login ">\n"))
287 ((eq mail-from-style 'parens)
288 (insert "From: " login " (")
289 (let ((fullname-start (point)))
290 (insert fullname)
291 (let ((fullname-end (point-marker)))
292 (goto-char fullname-start)
293 ;; RFC 822 says \ and nonmatching parentheses
294 ;; must be escaped in comments.
295 ;; Escape every instance of ()\ ...
296 (while (re-search-forward "[()\\]" fullname-end 1)
297 (replace-match "\\\\\\&" t))
298 ;; ... then undo escaping of matching parentheses,
299 ;; including matching nested parentheses.
300 (goto-char fullname-start)
301 (while (re-search-forward
302 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
303 fullname-end 1)
304 (replace-match "\\1(\\3)" t)
305 (goto-char fullname-start))))
306 (insert ")\n"))
307 ((null mail-from-style)
308 (insert "From: " login "\n")))))
309 ;; Insert a `Message-Id:' field if there isn't one yet.
310 (goto-char (point-min))
311 (unless (re-search-forward "^Message-Id:" delimline t)
312 (insert "Message-Id: " (message-make-message-id) "\n"))
313 ;; Insert a `Date:' field if there isn't one yet.
314 (goto-char (point-min))
315 (unless (re-search-forward "^Date:" delimline t)
316 (insert "Date: " (message-make-date) "\n"))
317 ;; Insert an extra newline if we need it to work around
318 ;; Sun's bug that swallows newlines.
319 (goto-char (1+ delimline))
320 (if (eval mail-mailer-swallows-blank-line)
321 (newline))
322 ;; Find and handle any FCC fields.
323 (goto-char (point-min))
324 (if (re-search-forward "^FCC:" delimline t)
325 (mail-do-fcc delimline))
326 (if mail-interactive
327 (with-current-buffer errbuf
328 (erase-buffer))))
329 ;;
330 ;;
331 ;;
332 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
333 (setq smtpmail-recipient-address-list
334 (smtpmail-deduce-address-list tembuf (point-min) delimline))
335 (kill-buffer smtpmail-address-buffer)
336
337 (smtpmail-do-bcc delimline)
338 ; Send or queue
339 (if (not smtpmail-queue-mail)
340 (if (not (null smtpmail-recipient-address-list))
341 (if (not (smtpmail-via-smtp
342 smtpmail-recipient-address-list tembuf))
343 (error "Sending failed; SMTP protocol error"))
344 (error "Sending failed; no recipients"))
345 (let* ((file-data
346 (expand-file-name
347 (format "%s_%i"
348 (format-time-string "%Y-%m-%d_%H:%M:%S")
349 (setq smtpmail-queue-counter
350 (1+ smtpmail-queue-counter)))
351 smtpmail-queue-dir))
352 (file-data (convert-standard-filename file-data))
353 (file-elisp (concat file-data ".el"))
354 (buffer-data (create-file-buffer file-data))
355 (buffer-elisp (create-file-buffer file-elisp))
356 (buffer-scratch "*queue-mail*"))
357 (with-current-buffer buffer-data
358 (erase-buffer)
359 (insert-buffer tembuf)
360 (write-file file-data)
361 (set-buffer buffer-elisp)
362 (erase-buffer)
363 (insert (concat
364 "(setq smtpmail-recipient-address-list '"
365 (prin1-to-string smtpmail-recipient-address-list)
366 ")\n"))
367 (write-file file-elisp)
368 (set-buffer (generate-new-buffer buffer-scratch))
369 (insert (concat file-data "\n"))
370 (append-to-file (point-min)
371 (point-max)
372 smtpmail-queue-index)
373 )
374 (kill-buffer buffer-scratch)
375 (kill-buffer buffer-data)
376 (kill-buffer buffer-elisp))))
377 (kill-buffer tembuf)
378 (if (bufferp errbuf)
379 (kill-buffer errbuf)))))
380
381 ;;;###autoload
382 (defun smtpmail-send-queued-mail ()
383 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
384 (interactive)
385 (with-temp-buffer
386 ;;; Get index, get first mail, send it, update index, get second
387 ;;; mail, send it, etc...
388 (let ((file-msg ""))
389 (insert-file-contents smtpmail-queue-index)
390 (beginning-of-buffer)
391 (while (not (eobp))
392 (setq file-msg (buffer-substring (point) (line-end-position)))
393 (load file-msg)
394 ;; Insert the message literally: it is already encoded as per
395 ;; the MIME headers, and code conversions might guess the
396 ;; encoding wrongly.
397 (with-temp-buffer
398 (let ((coding-system-for-read 'no-conversion))
399 (insert-file-contents file-msg))
400 (if (not (null smtpmail-recipient-address-list))
401 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
402 (current-buffer)))
403 (error "Sending failed; SMTP protocol error"))
404 (error "Sending failed; no recipients")))
405 (delete-file file-msg)
406 (delete-file (concat file-msg ".el"))
407 (kill-line 1))
408 (write-region (point-min) (point-max) smtpmail-queue-index))))
409
410 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
411
412 (defun smtpmail-fqdn ()
413 (if smtpmail-local-domain
414 (concat (system-name) "." smtpmail-local-domain)
415 (system-name)))
416
417 (defsubst smtpmail-cred-server (cred)
418 (nth 0 cred))
419
420 (defsubst smtpmail-cred-port (cred)
421 (nth 1 cred))
422
423 (defsubst smtpmail-cred-key (cred)
424 (nth 2 cred))
425
426 (defsubst smtpmail-cred-user (cred)
427 (nth 2 cred))
428
429 (defsubst smtpmail-cred-cert (cred)
430 (nth 3 cred))
431
432 (defsubst smtpmail-cred-passwd (cred)
433 (nth 3 cred))
434
435 (defun smtpmail-find-credentials (cred server port)
436 (catch 'done
437 (let ((l cred) el)
438 (while (setq el (pop l))
439 (when (and (equal server (smtpmail-cred-server el))
440 (equal port (smtpmail-cred-port el)))
441 (throw 'done el))))))
442
443 (defun smtpmail-maybe-append-domain (recipient)
444 (if (or (not smtpmail-sendto-domain)
445 (string-match "@" recipient))
446 recipient
447 (concat recipient "@" smtpmail-sendto-domain)))
448
449 (defun smtpmail-intersection (list1 list2)
450 (let ((result nil))
451 (dolist (el2 list2)
452 (when (memq el2 list1)
453 (push el2 result)))
454 (nreverse result)))
455
456 (defun smtpmail-open-stream (process-buffer host port)
457 (let ((cred (smtpmail-find-credentials
458 smtpmail-starttls-credentials host port)))
459 (if (null (and cred (condition-case ()
460 (call-process "starttls")
461 (error nil))))
462 ;; The normal case.
463 (open-network-stream "SMTP" process-buffer host port)
464 (let* ((cred-key (smtpmail-cred-key cred))
465 (cred-cert (smtpmail-cred-cert cred))
466 (starttls-extra-args
467 (when (and (stringp cred-key) (stringp cred-cert)
468 (file-regular-p
469 (setq cred-key (expand-file-name cred-key)))
470 (file-regular-p
471 (setq cred-cert (expand-file-name cred-cert))))
472 (list "--key-file" cred-key "--cert-file" cred-cert))))
473 (starttls-open-stream "SMTP" process-buffer host port)))))
474
475 (defun smtpmail-try-auth-methods (process supported-extensions host port)
476 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
477 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
478 (cred (if (stringp smtpmail-auth-credentials)
479 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
480 (hostentry (netrc-machine
481 netrc host (format "%s" (or port "smtp"))
482 "smtp")))
483 (when hostentry
484 (list host port
485 (netrc-get hostentry "login")
486 (netrc-get hostentry "password"))))
487 (smtpmail-find-credentials
488 smtpmail-auth-credentials host port)))
489 (passwd (when cred
490 (or (smtpmail-cred-passwd cred)
491 (read-passwd
492 (format "SMTP password for %s:%s: "
493 (smtpmail-cred-server cred)
494 (smtpmail-cred-port cred))))))
495 ret)
496 (when cred
497 (cond
498 ((eq mech 'cram-md5)
499 (smtpmail-send-command process (format "AUTH %s" mech))
500 (if (or (null (car (setq ret (smtpmail-read-response process))))
501 (not (integerp (car ret)))
502 (>= (car ret) 400))
503 (throw 'done nil))
504 (when (eq (car ret) 334)
505 (let* ((challenge (substring (cadr ret) 4))
506 (decoded (base64-decode-string challenge))
507 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
508 (response (concat (smtpmail-cred-user cred) " " hash))
509 (encoded (base64-encode-string response)))
510 (smtpmail-send-command process (format "%s" encoded))
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 ((eq mech 'login)
516 (smtpmail-send-command process "AUTH LOGIN")
517 (if (or (null (car (setq ret (smtpmail-read-response process))))
518 (not (integerp (car ret)))
519 (>= (car ret) 400))
520 (throw 'done nil))
521 (smtpmail-send-command
522 process (base64-encode-string (smtpmail-cred-user cred)))
523 (if (or (null (car (setq ret (smtpmail-read-response process))))
524 (not (integerp (car ret)))
525 (>= (car ret) 400))
526 (throw 'done nil))
527 (smtpmail-send-command process (base64-encode-string passwd))
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 (t
533 (error "Mechanism %s not implemented" mech)))
534 ;; Remember the password.
535 (when (and (not (stringp smtpmail-auth-credentials))
536 (null (smtpmail-cred-passwd cred)))
537 (setcar (cdr (cdr (cdr cred))) passwd)))))
538
539 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
540 (let ((process nil)
541 (host (or smtpmail-smtp-server
542 (error "`smtpmail-smtp-server' not defined")))
543 (port smtpmail-smtp-service)
544 (envelope-from (or (mail-envelope-from)
545 smtpmail-mail-address
546 user-mail-address))
547 response-code
548 greeting
549 process-buffer
550 (supported-extensions '()))
551 (unwind-protect
552 (catch 'done
553 ;; get or create the trace buffer
554 (setq process-buffer
555 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
556
557 ;; clear the trace buffer of old output
558 (with-current-buffer process-buffer
559 (erase-buffer))
560
561 ;; open the connection to the server
562 (setq process (smtpmail-open-stream process-buffer host port))
563 (and (null process) (throw 'done nil))
564
565 ;; set the send-filter
566 (set-process-filter process 'smtpmail-process-filter)
567
568 (with-current-buffer process-buffer
569 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
570 (make-local-variable 'smtpmail-read-point)
571 (setq smtpmail-read-point (point-min))
572
573
574 (if (or (null (car (setq greeting (smtpmail-read-response process))))
575 (not (integerp (car greeting)))
576 (>= (car greeting) 400))
577 (throw 'done nil)
578 )
579
580 (let ((do-ehlo t)
581 (do-starttls t))
582 (while do-ehlo
583 ;; EHLO
584 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
585
586 (if (or (null (car (setq response-code
587 (smtpmail-read-response process))))
588 (not (integerp (car response-code)))
589 (>= (car response-code) 400))
590 (progn
591 ;; HELO
592 (smtpmail-send-command
593 process (format "HELO %s" (smtpmail-fqdn)))
594
595 (if (or (null (car (setq response-code
596 (smtpmail-read-response process))))
597 (not (integerp (car response-code)))
598 (>= (car response-code) 400))
599 (throw 'done nil)))
600 (dolist (line (cdr (cdr response-code)))
601 (let ((name (mapcar (lambda (s) (intern (downcase s)))
602 (split-string (substring line 4) "[ ]"))))
603 (and (eq (length name) 1)
604 (setq name (car name)))
605 (and name
606 (cond ((memq (if (consp name) (car name) name)
607 '(verb xvrb 8bitmime onex xone
608 expn size dsn etrn
609 enhancedstatuscodes
610 help xusr
611 auth=login auth starttls))
612 (setq supported-extensions
613 (cons name supported-extensions)))
614 (smtpmail-warn-about-unknown-extensions
615 (message "Unknown extension %s" name)))))))
616
617 (if (and do-starttls
618 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
619 (member 'starttls supported-extensions)
620 (numberp (process-id process)))
621 (progn
622 (smtpmail-send-command process (format "STARTTLS"))
623 (if (or (null (car (setq response-code (smtpmail-read-response process))))
624 (not (integerp (car response-code)))
625 (>= (car response-code) 400))
626 (throw 'done nil))
627 (starttls-negotiate process)
628 (setq do-starttls nil))
629 (setq do-ehlo nil))))
630
631 (smtpmail-try-auth-methods process supported-extensions host port)
632
633 (if (or (member 'onex supported-extensions)
634 (member 'xone supported-extensions))
635 (progn
636 (smtpmail-send-command process (format "ONEX"))
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
642 (if (and smtpmail-debug-verb
643 (or (member 'verb supported-extensions)
644 (member 'xvrb supported-extensions)))
645 (progn
646 (smtpmail-send-command process (format "VERB"))
647 (if (or (null (car (setq response-code (smtpmail-read-response process))))
648 (not (integerp (car response-code)))
649 (>= (car response-code) 400))
650 (throw 'done nil))))
651
652 (if (member 'xusr supported-extensions)
653 (progn
654 (smtpmail-send-command process (format "XUSR"))
655 (if (or (null (car (setq response-code (smtpmail-read-response process))))
656 (not (integerp (car response-code)))
657 (>= (car response-code) 400))
658 (throw 'done nil))))
659
660 ;; MAIL FROM: <sender>
661 (let ((size-part
662 (if (or (member 'size supported-extensions)
663 (assoc 'size supported-extensions))
664 (format " SIZE=%d"
665 (with-current-buffer smtpmail-text-buffer
666 ;; size estimate:
667 (+ (- (point-max) (point-min))
668 ;; Add one byte for each change-of-line
669 ;; because or CR-LF representation:
670 (count-lines (point-min) (point-max))
671 ;; For some reason, an empty line is
672 ;; added to the message. Maybe this
673 ;; is a bug, but it can't hurt to add
674 ;; those two bytes anyway:
675 2)))
676 ""))
677 (body-part
678 (if (member '8bitmime supported-extensions)
679 ;; FIXME:
680 ;; Code should be added here that transforms
681 ;; the contents of the message buffer into
682 ;; something the receiving SMTP can handle.
683 ;; For a receiver that supports 8BITMIME, this
684 ;; may mean converting BINARY to BASE64, or
685 ;; adding Content-Transfer-Encoding and the
686 ;; other MIME headers. The code should also
687 ;; return an indication of what encoding the
688 ;; message buffer is now, i.e. ASCII or
689 ;; 8BITMIME.
690 (if nil
691 " BODY=8BITMIME"
692 "")
693 "")))
694 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
695 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
696 envelope-from
697 size-part
698 body-part))
699
700 (if (or (null (car (setq response-code (smtpmail-read-response process))))
701 (not (integerp (car response-code)))
702 (>= (car response-code) 400))
703 (throw 'done nil)
704 ))
705
706 ;; RCPT TO: <recipient>
707 (let ((n 0))
708 (while (not (null (nth n recipient)))
709 (smtpmail-send-command process (format "RCPT TO: <%s>" (smtpmail-maybe-append-domain (nth n recipient))))
710 (setq n (1+ n))
711
712 (setq response-code (smtpmail-read-response process))
713 (if (or (null (car response-code))
714 (not (integerp (car response-code)))
715 (>= (car response-code) 400))
716 (throw 'done nil)
717 )
718 ))
719
720 ;; DATA
721 (smtpmail-send-command process "DATA")
722
723 (if (or (null (car (setq response-code (smtpmail-read-response process))))
724 (not (integerp (car response-code)))
725 (>= (car response-code) 400))
726 (throw 'done nil)
727 )
728
729 ;; Mail contents
730 (smtpmail-send-data process smtpmail-text-buffer)
731
732 ;;DATA end "."
733 (smtpmail-send-command process ".")
734
735 (if (or (null (car (setq response-code (smtpmail-read-response process))))
736 (not (integerp (car response-code)))
737 (>= (car response-code) 400))
738 (throw 'done nil)
739 )
740
741 ;;QUIT
742 ; (smtpmail-send-command process "QUIT")
743 ; (and (null (car (smtpmail-read-response process)))
744 ; (throw 'done nil))
745 t ))
746 (if process
747 (with-current-buffer (process-buffer process)
748 (smtpmail-send-command process "QUIT")
749 (smtpmail-read-response process)
750
751 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
752 ; (not (integerp (car response-code)))
753 ; (>= (car response-code) 400))
754 ; (throw 'done nil)
755 ; )
756 (delete-process process)
757 (unless smtpmail-debug-info
758 (kill-buffer process-buffer)))))))
759
760
761 (defun smtpmail-process-filter (process output)
762 (with-current-buffer (process-buffer process)
763 (goto-char (point-max))
764 (insert output)))
765
766 (defun smtpmail-read-response (process)
767 (let ((case-fold-search nil)
768 (response-strings nil)
769 (response-continue t)
770 (return-value '(nil ()))
771 match-end)
772
773 (while response-continue
774 (goto-char smtpmail-read-point)
775 (while (not (search-forward "\r\n" nil t))
776 (accept-process-output process)
777 (goto-char smtpmail-read-point))
778
779 (setq match-end (point))
780 (setq response-strings
781 (cons (buffer-substring smtpmail-read-point (- match-end 2))
782 response-strings))
783
784 (goto-char smtpmail-read-point)
785 (if (looking-at "[0-9]+ ")
786 (let ((begin (match-beginning 0))
787 (end (match-end 0)))
788 (if smtpmail-debug-info
789 (message "%s" (car response-strings)))
790
791 (setq smtpmail-read-point match-end)
792
793 ;; ignore lines that start with "0"
794 (if (looking-at "0[0-9]+ ")
795 nil
796 (setq response-continue nil)
797 (setq return-value
798 (cons (string-to-int
799 (buffer-substring begin end))
800 (nreverse response-strings)))))
801
802 (if (looking-at "[0-9]+-")
803 (progn (if smtpmail-debug-info
804 (message "%s" (car response-strings)))
805 (setq smtpmail-read-point match-end)
806 (setq response-continue t))
807 (progn
808 (setq smtpmail-read-point match-end)
809 (setq response-continue nil)
810 (setq return-value
811 (cons nil (nreverse response-strings)))
812 )
813 )))
814 (setq smtpmail-read-point match-end)
815 return-value))
816
817
818 (defun smtpmail-send-command (process command)
819 (goto-char (point-max))
820 (if (= (aref command 0) ?P)
821 (insert "PASS <omitted>\r\n")
822 (insert command "\r\n"))
823 (setq smtpmail-read-point (point))
824 (process-send-string process command)
825 (process-send-string process "\r\n"))
826
827 (defun smtpmail-send-data-1 (process data)
828 (goto-char (point-max))
829
830 (if (and (multibyte-string-p data)
831 smtpmail-code-conv-from)
832 (setq data (string-as-multibyte
833 (encode-coding-string data smtpmail-code-conv-from))))
834
835 (if smtpmail-debug-info
836 (insert data "\r\n"))
837
838 (setq smtpmail-read-point (point))
839 ;; Escape "." at start of a line
840 (if (eq (string-to-char data) ?.)
841 (process-send-string process "."))
842 (process-send-string process data)
843 (process-send-string process "\r\n")
844 )
845
846 (defun smtpmail-send-data (process buffer)
847 (let
848 ((data-continue t)
849 (sending-data nil)
850 this-line
851 this-line-end)
852
853 (with-current-buffer buffer
854 (goto-char (point-min)))
855
856 (while data-continue
857 (with-current-buffer buffer
858 (beginning-of-line)
859 (setq this-line (point))
860 (end-of-line)
861 (setq this-line-end (point))
862 (setq sending-data nil)
863 (setq sending-data (buffer-substring this-line this-line-end))
864 (if (/= (forward-line 1) 0)
865 (setq data-continue nil)))
866
867 (smtpmail-send-data-1 process sending-data)
868 )
869 )
870 )
871
872
873 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
874 "Get address list suitable for smtp RCPT TO: <address>."
875 (unwind-protect
876 (with-current-buffer smtpmail-address-buffer
877 (erase-buffer)
878 (let
879 ((case-fold-search t)
880 (simple-address-list "")
881 this-line
882 this-line-end
883 addr-regexp)
884 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
885 (goto-char (point-min))
886 ;; RESENT-* fields should stop processing of regular fields.
887 (save-excursion
888 (setq addr-regexp
889 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
890 header-end t)
891 "^Resent-\\(to\\|cc\\|bcc\\):"
892 "^\\(To:\\|Cc:\\|Bcc:\\)")))
893
894 (while (re-search-forward addr-regexp header-end t)
895 (replace-match "")
896 (setq this-line (match-beginning 0))
897 (forward-line 1)
898 ;; get any continuation lines
899 (while (and (looking-at "^[ \t]+") (< (point) header-end))
900 (forward-line 1))
901 (setq this-line-end (point-marker))
902 (setq simple-address-list
903 (concat simple-address-list " "
904 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
905 )
906 (erase-buffer)
907 (insert " " simple-address-list "\n")
908 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
909 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
910 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
911
912 (goto-char (point-min))
913 ;; tidyness in case hook is not robust when it looks at this
914 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
915
916 (goto-char (point-min))
917 (let (recipient-address-list)
918 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
919 (backward-char 1)
920 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
921 recipient-address-list))
922 )
923 (setq smtpmail-recipient-address-list recipient-address-list))
924
925 )
926 )
927 )
928 )
929
930
931 (defun smtpmail-do-bcc (header-end)
932 "Delete [Resent-]BCC: and their continuation lines from the header area.
933 There may be multiple BCC: lines, and each may have arbitrarily
934 many continuation lines."
935 (let ((case-fold-search t))
936 (save-excursion
937 (goto-char (point-min))
938 ;; iterate over all BCC: lines
939 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
940 (delete-region (match-beginning 0)
941 (progn (forward-line 1) (point)))
942 ;; get rid of any continuation lines
943 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
944 (replace-match ""))))))
945
946
947 (provide 'smtpmail)
948
949 ;;; smtpmail.el ends here