]> code.delx.au - gnu-emacs/blob - lisp/mail/smtpmail.el
New icons for debugger.
[gnu-emacs] / lisp / mail / smtpmail.el
1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
2
3 ;; Copyright (C) 1995, 1996, 2001, 2002, 2003 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 (delete-region (point-at-bol) (point-at-bol 2)))
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 (progn
461 (require 'starttls)
462 (call-process starttls-program))
463 (error nil))))
464 ;; The normal case.
465 (open-network-stream "SMTP" process-buffer host port)
466 (let* ((cred-key (smtpmail-cred-key cred))
467 (cred-cert (smtpmail-cred-cert cred))
468 (starttls-extra-args
469 (when (and (stringp cred-key) (stringp cred-cert)
470 (file-regular-p
471 (setq cred-key (expand-file-name cred-key)))
472 (file-regular-p
473 (setq cred-cert (expand-file-name cred-cert))))
474 (list "--key-file" cred-key "--cert-file" cred-cert))))
475 (starttls-open-stream "SMTP" process-buffer host port)))))
476
477 (defun smtpmail-try-auth-methods (process supported-extensions host port)
478 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
479 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
480 (cred (if (stringp smtpmail-auth-credentials)
481 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
482 (hostentry (netrc-machine
483 netrc host (format "%s" (or port "smtp"))
484 "smtp")))
485 (when hostentry
486 (list host port
487 (netrc-get hostentry "login")
488 (netrc-get hostentry "password"))))
489 (smtpmail-find-credentials
490 smtpmail-auth-credentials host port)))
491 (passwd (when cred
492 (or (smtpmail-cred-passwd cred)
493 (read-passwd
494 (format "SMTP password for %s:%s: "
495 (smtpmail-cred-server cred)
496 (smtpmail-cred-port cred))))))
497 ret)
498 (when cred
499 (cond
500 ((eq mech 'cram-md5)
501 (smtpmail-send-command process (format "AUTH %s" mech))
502 (if (or (null (car (setq ret (smtpmail-read-response process))))
503 (not (integerp (car ret)))
504 (>= (car ret) 400))
505 (throw 'done nil))
506 (when (eq (car ret) 334)
507 (let* ((challenge (substring (cadr ret) 4))
508 (decoded (base64-decode-string challenge))
509 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
510 (response (concat (smtpmail-cred-user cred) " " hash))
511 (encoded (base64-encode-string response)))
512 (smtpmail-send-command process (format "%s" encoded))
513 (if (or (null (car (setq ret (smtpmail-read-response process))))
514 (not (integerp (car ret)))
515 (>= (car ret) 400))
516 (throw 'done nil)))))
517 ((eq mech 'login)
518 (smtpmail-send-command process "AUTH LOGIN")
519 (if (or (null (car (setq ret (smtpmail-read-response process))))
520 (not (integerp (car ret)))
521 (>= (car ret) 400))
522 (throw 'done nil))
523 (smtpmail-send-command
524 process (base64-encode-string (smtpmail-cred-user cred)))
525 (if (or (null (car (setq ret (smtpmail-read-response process))))
526 (not (integerp (car ret)))
527 (>= (car ret) 400))
528 (throw 'done nil))
529 (smtpmail-send-command process (base64-encode-string passwd))
530 (if (or (null (car (setq ret (smtpmail-read-response process))))
531 (not (integerp (car ret)))
532 (>= (car ret) 400))
533 (throw 'done nil)))
534 (t
535 (error "Mechanism %s not implemented" mech)))
536 ;; Remember the password.
537 (when (and (not (stringp smtpmail-auth-credentials))
538 (null (smtpmail-cred-passwd cred)))
539 (setcar (cdr (cdr (cdr cred))) passwd)))))
540
541 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
542 (let ((process nil)
543 (host (or smtpmail-smtp-server
544 (error "`smtpmail-smtp-server' not defined")))
545 (port smtpmail-smtp-service)
546 (envelope-from (or (mail-envelope-from)
547 smtpmail-mail-address
548 user-mail-address))
549 response-code
550 greeting
551 process-buffer
552 (supported-extensions '()))
553 (unwind-protect
554 (catch 'done
555 ;; get or create the trace buffer
556 (setq process-buffer
557 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
558
559 ;; clear the trace buffer of old output
560 (with-current-buffer process-buffer
561 (erase-buffer))
562
563 ;; open the connection to the server
564 (setq process (smtpmail-open-stream process-buffer host port))
565 (and (null process) (throw 'done nil))
566
567 ;; set the send-filter
568 (set-process-filter process 'smtpmail-process-filter)
569
570 (with-current-buffer process-buffer
571 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
572 (make-local-variable 'smtpmail-read-point)
573 (setq smtpmail-read-point (point-min))
574
575
576 (if (or (null (car (setq greeting (smtpmail-read-response process))))
577 (not (integerp (car greeting)))
578 (>= (car greeting) 400))
579 (throw 'done nil)
580 )
581
582 (let ((do-ehlo t)
583 (do-starttls t))
584 (while do-ehlo
585 ;; EHLO
586 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
587
588 (if (or (null (car (setq response-code
589 (smtpmail-read-response process))))
590 (not (integerp (car response-code)))
591 (>= (car response-code) 400))
592 (progn
593 ;; HELO
594 (smtpmail-send-command
595 process (format "HELO %s" (smtpmail-fqdn)))
596
597 (if (or (null (car (setq response-code
598 (smtpmail-read-response process))))
599 (not (integerp (car response-code)))
600 (>= (car response-code) 400))
601 (throw 'done nil)))
602 (dolist (line (cdr (cdr response-code)))
603 (let ((name (mapcar (lambda (s) (intern (downcase s)))
604 (split-string (substring line 4) "[ ]"))))
605 (and (eq (length name) 1)
606 (setq name (car name)))
607 (and name
608 (cond ((memq (if (consp name) (car name) name)
609 '(verb xvrb 8bitmime onex xone
610 expn size dsn etrn
611 enhancedstatuscodes
612 help xusr
613 auth=login auth starttls))
614 (setq supported-extensions
615 (cons name supported-extensions)))
616 (smtpmail-warn-about-unknown-extensions
617 (message "Unknown extension %s" name)))))))
618
619 (if (and do-starttls
620 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
621 (member 'starttls supported-extensions)
622 (numberp (process-id process)))
623 (progn
624 (smtpmail-send-command process (format "STARTTLS"))
625 (if (or (null (car (setq response-code (smtpmail-read-response process))))
626 (not (integerp (car response-code)))
627 (>= (car response-code) 400))
628 (throw 'done nil))
629 (starttls-negotiate process)
630 (setq do-starttls nil))
631 (setq do-ehlo nil))))
632
633 (smtpmail-try-auth-methods process supported-extensions host port)
634
635 (if (or (member 'onex supported-extensions)
636 (member 'xone supported-extensions))
637 (progn
638 (smtpmail-send-command process (format "ONEX"))
639 (if (or (null (car (setq response-code (smtpmail-read-response process))))
640 (not (integerp (car response-code)))
641 (>= (car response-code) 400))
642 (throw 'done nil))))
643
644 (if (and smtpmail-debug-verb
645 (or (member 'verb supported-extensions)
646 (member 'xvrb supported-extensions)))
647 (progn
648 (smtpmail-send-command process (format "VERB"))
649 (if (or (null (car (setq response-code (smtpmail-read-response process))))
650 (not (integerp (car response-code)))
651 (>= (car response-code) 400))
652 (throw 'done nil))))
653
654 (if (member 'xusr supported-extensions)
655 (progn
656 (smtpmail-send-command process (format "XUSR"))
657 (if (or (null (car (setq response-code (smtpmail-read-response process))))
658 (not (integerp (car response-code)))
659 (>= (car response-code) 400))
660 (throw 'done nil))))
661
662 ;; MAIL FROM: <sender>
663 (let ((size-part
664 (if (or (member 'size supported-extensions)
665 (assoc 'size supported-extensions))
666 (format " SIZE=%d"
667 (with-current-buffer smtpmail-text-buffer
668 ;; size estimate:
669 (+ (- (point-max) (point-min))
670 ;; Add one byte for each change-of-line
671 ;; because or CR-LF representation:
672 (count-lines (point-min) (point-max))
673 ;; For some reason, an empty line is
674 ;; added to the message. Maybe this
675 ;; is a bug, but it can't hurt to add
676 ;; those two bytes anyway:
677 2)))
678 ""))
679 (body-part
680 (if (member '8bitmime supported-extensions)
681 ;; FIXME:
682 ;; Code should be added here that transforms
683 ;; the contents of the message buffer into
684 ;; something the receiving SMTP can handle.
685 ;; For a receiver that supports 8BITMIME, this
686 ;; may mean converting BINARY to BASE64, or
687 ;; adding Content-Transfer-Encoding and the
688 ;; other MIME headers. The code should also
689 ;; return an indication of what encoding the
690 ;; message buffer is now, i.e. ASCII or
691 ;; 8BITMIME.
692 (if nil
693 " BODY=8BITMIME"
694 "")
695 "")))
696 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
697 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
698 envelope-from
699 size-part
700 body-part))
701
702 (if (or (null (car (setq response-code (smtpmail-read-response process))))
703 (not (integerp (car response-code)))
704 (>= (car response-code) 400))
705 (throw 'done nil)
706 ))
707
708 ;; RCPT TO: <recipient>
709 (let ((n 0))
710 (while (not (null (nth n recipient)))
711 (smtpmail-send-command process (format "RCPT TO: <%s>" (smtpmail-maybe-append-domain (nth n recipient))))
712 (setq n (1+ n))
713
714 (setq response-code (smtpmail-read-response process))
715 (if (or (null (car response-code))
716 (not (integerp (car response-code)))
717 (>= (car response-code) 400))
718 (throw 'done nil)
719 )
720 ))
721
722 ;; DATA
723 (smtpmail-send-command process "DATA")
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 ;; Mail contents
732 (smtpmail-send-data process smtpmail-text-buffer)
733
734 ;;DATA end "."
735 (smtpmail-send-command process ".")
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 ;;QUIT
744 ; (smtpmail-send-command process "QUIT")
745 ; (and (null (car (smtpmail-read-response process)))
746 ; (throw 'done nil))
747 t ))
748 (if process
749 (with-current-buffer (process-buffer process)
750 (smtpmail-send-command process "QUIT")
751 (smtpmail-read-response process)
752
753 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
754 ; (not (integerp (car response-code)))
755 ; (>= (car response-code) 400))
756 ; (throw 'done nil)
757 ; )
758 (delete-process process)
759 (unless smtpmail-debug-info
760 (kill-buffer process-buffer)))))))
761
762
763 (defun smtpmail-process-filter (process output)
764 (with-current-buffer (process-buffer process)
765 (goto-char (point-max))
766 (insert output)))
767
768 (defun smtpmail-read-response (process)
769 (let ((case-fold-search nil)
770 (response-strings nil)
771 (response-continue t)
772 (return-value '(nil ()))
773 match-end)
774
775 (while response-continue
776 (goto-char smtpmail-read-point)
777 (while (not (search-forward "\r\n" nil t))
778 (accept-process-output process)
779 (goto-char smtpmail-read-point))
780
781 (setq match-end (point))
782 (setq response-strings
783 (cons (buffer-substring smtpmail-read-point (- match-end 2))
784 response-strings))
785
786 (goto-char smtpmail-read-point)
787 (if (looking-at "[0-9]+ ")
788 (let ((begin (match-beginning 0))
789 (end (match-end 0)))
790 (if smtpmail-debug-info
791 (message "%s" (car response-strings)))
792
793 (setq smtpmail-read-point match-end)
794
795 ;; ignore lines that start with "0"
796 (if (looking-at "0[0-9]+ ")
797 nil
798 (setq response-continue nil)
799 (setq return-value
800 (cons (string-to-int
801 (buffer-substring begin end))
802 (nreverse response-strings)))))
803
804 (if (looking-at "[0-9]+-")
805 (progn (if smtpmail-debug-info
806 (message "%s" (car response-strings)))
807 (setq smtpmail-read-point match-end)
808 (setq response-continue t))
809 (progn
810 (setq smtpmail-read-point match-end)
811 (setq response-continue nil)
812 (setq return-value
813 (cons nil (nreverse response-strings)))
814 )
815 )))
816 (setq smtpmail-read-point match-end)
817 return-value))
818
819
820 (defun smtpmail-send-command (process command)
821 (goto-char (point-max))
822 (if (= (aref command 0) ?P)
823 (insert "PASS <omitted>\r\n")
824 (insert command "\r\n"))
825 (setq smtpmail-read-point (point))
826 (process-send-string process command)
827 (process-send-string process "\r\n"))
828
829 (defun smtpmail-send-data-1 (process data)
830 (goto-char (point-max))
831
832 (if (and (multibyte-string-p data)
833 smtpmail-code-conv-from)
834 (setq data (string-as-multibyte
835 (encode-coding-string data smtpmail-code-conv-from))))
836
837 (if smtpmail-debug-info
838 (insert data "\r\n"))
839
840 (setq smtpmail-read-point (point))
841 ;; Escape "." at start of a line
842 (if (eq (string-to-char data) ?.)
843 (process-send-string process "."))
844 (process-send-string process data)
845 (process-send-string process "\r\n")
846 )
847
848 (defun smtpmail-send-data (process buffer)
849 (let
850 ((data-continue t)
851 (sending-data nil)
852 this-line
853 this-line-end)
854
855 (with-current-buffer buffer
856 (goto-char (point-min)))
857
858 (while data-continue
859 (with-current-buffer buffer
860 (beginning-of-line)
861 (setq this-line (point))
862 (end-of-line)
863 (setq this-line-end (point))
864 (setq sending-data nil)
865 (setq sending-data (buffer-substring this-line this-line-end))
866 (if (/= (forward-line 1) 0)
867 (setq data-continue nil)))
868
869 (smtpmail-send-data-1 process sending-data)
870 )
871 )
872 )
873
874
875 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
876 "Get address list suitable for smtp RCPT TO: <address>."
877 (unwind-protect
878 (with-current-buffer smtpmail-address-buffer
879 (erase-buffer)
880 (let
881 ((case-fold-search t)
882 (simple-address-list "")
883 this-line
884 this-line-end
885 addr-regexp)
886 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
887 (goto-char (point-min))
888 ;; RESENT-* fields should stop processing of regular fields.
889 (save-excursion
890 (setq addr-regexp
891 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
892 header-end t)
893 "^Resent-\\(to\\|cc\\|bcc\\):"
894 "^\\(To:\\|Cc:\\|Bcc:\\)")))
895
896 (while (re-search-forward addr-regexp header-end t)
897 (replace-match "")
898 (setq this-line (match-beginning 0))
899 (forward-line 1)
900 ;; get any continuation lines
901 (while (and (looking-at "^[ \t]+") (< (point) header-end))
902 (forward-line 1))
903 (setq this-line-end (point-marker))
904 (setq simple-address-list
905 (concat simple-address-list " "
906 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
907 )
908 (erase-buffer)
909 (insert " " simple-address-list "\n")
910 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
911 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
912 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
913
914 (goto-char (point-min))
915 ;; tidyness in case hook is not robust when it looks at this
916 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
917
918 (goto-char (point-min))
919 (let (recipient-address-list)
920 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
921 (backward-char 1)
922 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
923 recipient-address-list))
924 )
925 (setq smtpmail-recipient-address-list recipient-address-list))
926
927 )
928 )
929 )
930 )
931
932
933 (defun smtpmail-do-bcc (header-end)
934 "Delete [Resent-]BCC: and their continuation lines from the header area.
935 There may be multiple BCC: lines, and each may have arbitrarily
936 many continuation lines."
937 (let ((case-fold-search t))
938 (save-excursion
939 (goto-char (point-min))
940 ;; iterate over all BCC: lines
941 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
942 (delete-region (match-beginning 0)
943 (progn (forward-line 1) (point)))
944 ;; get rid of any continuation lines
945 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
946 (replace-match ""))))))
947
948
949 (provide 'smtpmail)
950
951 ;;; smtpmail.el ends here