1 ;;; smtp.el --- basic functions to send mail with SMTP server
3 ;; Copyright (C) 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
6 ;; Simon Leinen <simon@switch.ch> (ESMTP support)
7 ;; Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
8 ;; Daiki Ueno <ueno@unixuser.org>
9 ;; Keywords: SMTP, mail
11 ;; This file is part of FLIM (Faithful Library about Internet Message).
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
35 (require 'mail-utils) ; mail-strip-quoted-names
38 (require 'mel) ; binary-funcall
41 "SMTP protocol for sending mail."
44 (defgroup smtp-extensions nil
45 "SMTP service extensions (RFC1869)."
48 (defcustom smtp-default-server nil
49 "Specify default SMTP server."
50 :type '(choice (const nil) string)
53 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
54 "The name of the host running SMTP server.
55 It can also be a function
56 called from `smtp-via-smtp' with arguments SENDER and RECIPIENTS."
57 :type '(choice (string :tag "Name")
58 (function :tag "Function"))
61 (defcustom smtp-send-by-myself nil
62 "If non-nil, smtp.el send a mail by myself without smtp-server.
63 This option requires \"dig.el\"."
67 (defcustom smtp-service "smtp"
68 "SMTP service port number. \"smtp\" or 25."
69 :type '(choice (integer :tag "25" 25)
70 (string :tag "smtp" "smtp"))
73 (defcustom smtp-local-domain nil
74 "Local domain name without a host name.
75 If the function (system-name) returns the full internet address,
76 don't define this value."
77 :type '(choice (const nil) string)
80 (defcustom smtp-fqdn nil
81 "Fully qualified domain name used for Message-ID."
82 :type '(choice (const nil) string)
85 (defcustom smtp-use-8bitmime t
86 "If non-nil, use ESMTP 8BITMIME (RFC1652) if available."
88 :group 'smtp-extensions)
90 (defcustom smtp-use-size t
91 "If non-nil, use ESMTP SIZE (RFC1870) if available."
93 :group 'smtp-extensions)
95 (defcustom smtp-use-starttls nil
96 "If non-nil, use STARTTLS (RFC2595) if available."
98 :group 'smtp-extensions)
100 (defcustom smtp-use-starttls-ignore-error nil
101 "If non-nil, do not use STARTTLS if STARTTLS is not available."
103 :group 'smtp-extensions)
105 (defcustom smtp-use-sasl nil
106 "If non-nil, use SMTP Authentication (RFC2554) if available."
108 :group 'smtp-extensions)
110 (defcustom smtp-sasl-user-name (user-login-name)
111 "Identification to be used for authorization."
113 :group 'smtp-extensions)
115 (defcustom smtp-sasl-properties nil
116 "Properties set to SASL client."
118 :group 'smtp-extensions)
120 (defcustom smtp-sasl-mechanisms nil
121 "List of authentication mechanisms."
122 :type '(repeat string)
123 :group 'smtp-extensions)
125 (defvar sasl-mechanisms)
128 (defvar smtp-open-connection-function #'open-network-stream
129 "*Function used for connecting to a SMTP server.
130 The function will be called with the same four arguments as
131 `open-network-stream' and should return a process object.
134 \(setq smtp-open-connection-function
135 #'(lambda (name buffer host service)
136 (let ((process-connection-type nil))
137 (start-process name buffer \"ssh\" \"-C\" host
138 \"nc\" host service))))
140 It connects to a SMTP server using \"ssh\" before actually connecting
141 to the SMTP port. Where the command \"nc\" is the netcat executable;
142 see http://www.atstake.com/research/tools/index.html#network_utilities
143 for details. In addition, you will have to modify the value for
144 `smtp-end-of-line' to \"\\n\" if you use \"telnet\" instead of \"nc\".")
146 (defvar smtp-read-point nil)
148 (defvar smtp-connection-alist nil)
150 (defvar smtp-submit-package-function #'smtp-submit-package)
152 (defvar smtp-end-of-line "\r\n"
153 "*String to use as end-of-line marker when talking to a SMTP server.
154 This is \"\\r\\n\" by default, but it may have to be \"\\n\" when using a non
155 native connection function. See also `smtp-open-connection-function'.")
158 ;;; A package contains a mail message, an envelope sender address,
159 ;;; and one or more envelope recipient addresses. In ESMTP model
160 ;;; the current sending package should be guaranteed to be accessible
161 ;;; anywhere from the hook methods (or SMTP commands).
164 (luna-define-class smtp-package ()
169 (luna-define-internal-accessors 'smtp-package))
171 (defun smtp-make-package (sender recipients buffer)
172 "Create a new package structure.
173 A package is a unit of SMTP message
174 SENDER specifies the package sender, a string.
175 RECIPIENTS is a list of recipients.
176 BUFFER may be a buffer or a buffer name which contains mail message."
177 (luna-make-entity 'smtp-package :sender sender :recipients recipients :buffer buffer))
179 (defun smtp-package-buffer-internal-size (package)
180 "Return the size of PACKAGE, an integer."
182 (set-buffer (smtp-package-buffer-internal package))
185 ;; Add one byte for each change-of-line
186 ;; because or CR-LF representation:
187 (count-lines (point-min) (point-max))
188 ;; For some reason, an empty line is
189 ;; added to the message. Maybe this
190 ;; is a bug, but it can't hurt to add
191 ;; those two bytes anyway:
193 (goto-char (point-min))
194 (while (re-search-forward "^\\." nil t)
195 (setq size (1+ size)))
198 ;;; @ SMTP connection
199 ;;; We should consider the function `open-network-stream' is a emulation
200 ;;; for another network stream. They are likely to be implemented with an
201 ;;; external program and the function `process-contact' returns the
202 ;;; process id instead of `(HOST SERVICE)' pair.
205 (luna-define-class smtp-connection ()
213 (luna-define-internal-accessors 'smtp-connection))
215 (defun smtp-make-connection (process server service)
216 "Create a new connection structure.
217 PROCESS is an internal subprocess-object. SERVER is name of the host
218 to connect to. SERVICE is name of the service desired."
219 (luna-make-entity 'smtp-connection :process process :server server :service service))
221 (luna-define-generic smtp-connection-opened (connection)
222 "Say whether the CONNECTION to server has been opened.")
224 (luna-define-generic smtp-close-connection (connection)
225 "Close the CONNECTION to server.")
227 (luna-define-method smtp-connection-opened ((connection smtp-connection))
228 (let ((process (smtp-connection-process-internal connection)))
229 (if (memq (process-status process) '(open run))
232 (luna-define-method smtp-close-connection ((connection smtp-connection))
233 (let ((process (smtp-connection-process-internal connection)))
234 (delete-process process)))
236 (defun smtp-make-fqdn ()
237 "Return user's fully qualified domain name."
240 (let ((system-name (system-name)))
243 (concat system-name "." smtp-local-domain))
244 ((string-match "[^.]\\.[^.]" system-name)
247 (error "Cannot generate valid FQDN"))))))
249 (defun smtp-find-connection (buffer)
250 "Find the connection delivering to BUFFER."
251 (let ((entry (assq buffer smtp-connection-alist))
254 (setq connection (nth 1 entry))
255 (if (smtp-connection-opened connection)
257 (setq smtp-connection-alist
258 (delq entry smtp-connection-alist))
262 (autoload 'starttls-open-stream "starttls")
263 (autoload 'starttls-negotiate "starttls"))
265 (defun smtp-open-connection (buffer server service)
266 "Open a SMTP connection for a service to a host.
267 Return a newly allocated connection-object.
268 BUFFER is the buffer to associate with the connection. SERVER is name
269 of the host to connect to. SERVICE is name of the service desired."
271 (binary-funcall smtp-open-connection-function
272 "SMTP" buffer server service))
275 (setq connection (smtp-make-connection process server service))
276 (set-process-filter process 'smtp-process-filter)
277 (setq smtp-connection-alist
278 (cons (list buffer connection)
279 smtp-connection-alist))
283 (autoload 'dig-invoke "dig")
284 (autoload 'dig-extract-rr "dig"))
286 (defun smtp-find-mx (domain &optional doerror)
288 ;; dig.el resolves only primally MX.
289 (cond ((setq server (smtp-dig domain "MX"))
290 (progn (string-match " \\([^ ]*\\)$" server)
291 (match-string 1 server)))
292 ((smtp-dig domain "A")
296 (error (format "SMTP cannot resolve %s" domain)))))))
298 (defun smtp-dig (domain type)
301 (setq dig-buf (dig-invoke domain type)))
303 (dig-extract-rr domain type)
304 (kill-buffer dig-buf))))
306 (defun smtp-find-server (recipients)
309 (mapcar (lambda (recipient)
311 (if (and (string-match "@\\([^\t\n ]*\\)" recipient)
314 (match-string 1 recipient))))
315 (cons server (list recipient))
316 (error (format "cannot find server for %s." recipient)))))
319 (while (setq rets (pop rec))
320 (if (setq ret (assoc (car rets) rec))
322 (append (cdr ret) (cdr rets)))
324 (append rlist (list rets)))))
328 (defun smtp-via-smtp (sender recipients buffer)
329 "Like `smtp-send-buffer', but sucks in any errors."
332 (smtp-send-buffer sender recipients buffer)
336 (make-obsolete 'smtp-via-smtp "It's old API.")
339 (defun smtp-send-buffer (sender recipients buffer)
341 SENDER is an envelope sender address.
342 RECIPIENTS is a list of envelope recipient addresses.
343 BUFFER may be a buffer or a buffer name which contains mail message."
344 (if smtp-send-by-myself
345 (smtp-send-buffer-by-myself sender recipients buffer)
347 (if (functionp smtp-server)
348 (funcall smtp-server sender recipients)
350 (error "`smtp-server' not defined"))))
352 (smtp-make-package sender recipients buffer))
353 (smtp-open-connection-function
354 (if smtp-use-starttls
355 #'starttls-open-stream
356 smtp-open-connection-function)))
360 (format "*trace of SMTP session to %s*" server)))
362 (buffer-disable-undo)
363 (unless (smtp-find-connection (current-buffer))
364 (smtp-open-connection (current-buffer) server smtp-service))
365 (make-local-variable 'smtp-read-point)
366 (setq smtp-read-point (point-min))
367 (funcall smtp-submit-package-function package)))))
369 (defun smtp-submit-package (package)
372 (smtp-primitive-greeting package)
374 (smtp-primitive-ehlo package)
376 (smtp-primitive-helo package)))
377 (if smtp-use-starttls
379 (smtp-connection-extensions-internal
380 (smtp-find-connection (current-buffer))))
382 (smtp-primitive-starttls package)
383 (smtp-primitive-ehlo package))
384 (unless smtp-use-starttls-ignore-error
385 (error "STARTTLS is not supported on this server"))))
387 (smtp-primitive-auth package))
388 (smtp-primitive-mailfrom package)
389 (smtp-primitive-rcptto package)
390 (smtp-primitive-data package))
391 (let ((connection (smtp-find-connection (current-buffer))))
392 (when (smtp-connection-opened connection)
393 (smtp-primitive-quit package)
394 (smtp-close-connection connection)))))
396 (defun smtp-send-buffer-by-myself (sender recipients buffer)
397 "Send a message by myself.
398 SENDER is an envelope sender address.
399 RECIPIENTS is a list of envelope recipient addresses.
400 BUFFER may be a buffer or a buffer name which contains mail message."
402 (smtp-find-server recipients))
403 (smtp-open-connection-function
404 (if smtp-use-starttls
405 #'starttls-open-stream
406 smtp-open-connection-function))
409 (setq server (caar servers))
410 (setq recipients (cdar servers))
411 (if (not (and server recipients))
412 ;; MAILER-DAEMON is required. :)
413 (error (format "Cannot send <%s>"
414 (mapconcat 'concat recipients ">,<"))))
416 (smtp-make-package sender recipients buffer))
420 (format "*trace of SMTP session to %s*" server)))
422 (buffer-disable-undo)
423 (unless (smtp-find-connection (current-buffer))
424 (smtp-open-connection (current-buffer) server smtp-service))
425 (make-local-variable 'smtp-read-point)
426 (setq smtp-read-point (point-min))
427 (let ((smtp-use-sasl nil)
428 (smtp-use-starttls-ignore-error t))
429 (funcall smtp-submit-package-function package)))
430 (setq servers (cdr servers)))))
432 ;;; @ hook methods for `smtp-submit-package'
435 (defun smtp-primitive-greeting (package)
437 (smtp-find-connection (current-buffer)))
439 (smtp-read-response connection)))
440 (if (/= (car response) 220)
441 (smtp-response-error response))))
443 (defun smtp-primitive-ehlo (package)
445 (smtp-find-connection (current-buffer)))
447 (smtp-send-command connection (format "EHLO %s" (smtp-make-fqdn)))
448 (setq response (smtp-read-response connection))
449 (if (/= (car response) 250)
450 (smtp-response-error response))
451 (smtp-connection-set-extensions-internal
455 (split-string extension)))
457 (car (read-from-string
458 (downcase (car extensions)))))
462 (defun smtp-primitive-helo (package)
464 (smtp-find-connection (current-buffer)))
466 (smtp-send-command connection (format "HELO %s" (smtp-make-fqdn)))
467 (setq response (smtp-read-response connection))
468 (if (/= (car response) 250)
469 (smtp-response-error response))))
471 (defun smtp-primitive-auth (package)
473 (smtp-find-connection (current-buffer)))
475 (cdr (assq 'auth (smtp-connection-extensions-internal connection))))
477 (or smtp-sasl-mechanisms sasl-mechanisms))
479 (sasl-find-mechanism mechanisms))
485 (error "No authentication mechanism available"))
486 (setq client (sasl-make-client mechanism smtp-sasl-user-name "smtp"
487 (smtp-connection-server-internal connection)))
488 (if smtp-sasl-properties
489 (sasl-client-set-properties client smtp-sasl-properties))
490 (setq name (sasl-mechanism-name mechanism)
491 ;; Retrieve the initial response
492 step (sasl-next-step client nil))
495 (if (sasl-step-data step)
496 (format "AUTH %s %s" name (base64-encode-string (sasl-step-data step) t))
497 (format "AUTH %s" name)))
500 (setq response (smtp-read-response connection))
501 (when (= (car response) 235)
502 ;; The authentication process is finished.
503 (setq step (sasl-next-step client step))
506 (smtp-response-error response)) ;Bogus server?
507 (if (/= (car response) 334)
508 (smtp-response-error response))
509 (sasl-step-set-data step (base64-decode-string (nth 1 response)))
510 (setq step (sasl-next-step client step))
513 (if (sasl-step-data step)
514 (base64-encode-string (sasl-step-data step) t)
516 ;;; (smtp-connection-set-encoder-internal
517 ;;; connection (sasl-client-encoder client))
518 ;;; (smtp-connection-set-decoder-internal
519 ;;; connection (sasl-client-decoder client))
522 (defun smtp-primitive-starttls (package)
524 (smtp-find-connection (current-buffer)))
526 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
527 (smtp-send-command connection "STARTTLS")
528 (setq response (smtp-read-response connection))
529 (if (/= (car response) 220)
530 (smtp-response-error response))
531 (starttls-negotiate (smtp-connection-process-internal connection))))
533 (defun smtp-primitive-mailfrom (package)
535 (smtp-find-connection (current-buffer)))
537 (smtp-connection-extensions-internal
540 (smtp-package-sender-internal package))
543 ;; SIZE --- Message Size Declaration (RFC1870)
544 (if (and smtp-use-size
545 (assq 'size extensions))
546 (setq extension (format "SIZE=%d" (smtp-package-buffer-internal-size package))))
547 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
548 (if (and smtp-use-8bitmime
549 (assq '8bitmime extensions))
550 (setq extension (concat extension " BODY=8BITMIME")))
554 (format "MAIL FROM:<%s> %s" sender extension)
555 (format "MAIL FROM:<%s>" sender)))
556 (setq response (smtp-read-response connection))
557 (if (/= (car response) 250)
558 (smtp-response-error response))))
560 (defun smtp-primitive-rcptto (package)
562 (smtp-find-connection (current-buffer)))
564 (smtp-package-recipients-internal package))
568 connection (format "RCPT TO:<%s>" (pop recipients)))
569 (setq response (smtp-read-response connection))
570 (unless (memq (car response) '(250 251))
571 (smtp-response-error response)))))
573 (defun smtp-primitive-data (package)
575 (smtp-find-connection (current-buffer)))
577 (smtp-send-command connection "DATA")
578 (setq response (smtp-read-response connection))
579 (if (/= (car response) 354)
580 (smtp-response-error response))
582 (set-buffer (smtp-package-buffer-internal package))
583 (goto-char (point-min))
586 connection (buffer-substring (point) (progn (end-of-line)(point))))
587 (beginning-of-line 2)))
588 (smtp-send-command connection ".")
589 (setq response (smtp-read-response connection))
590 (if (/= (car response) 250)
591 (smtp-response-error response))))
593 (defun smtp-primitive-quit (package)
595 (smtp-find-connection (current-buffer)))
597 (smtp-send-command connection "QUIT")
598 (setq response (smtp-read-response connection))
599 (if (/= (car response) 221)
600 (smtp-response-error response))))
602 ;;; @ low level process manipulating function
604 (defun smtp-process-filter (process output)
606 (set-buffer (process-buffer process))
607 (goto-char (point-max))
610 (put 'smtp-error 'error-message "SMTP error")
611 (put 'smtp-error 'error-conditions '(smtp-error error))
613 (put 'smtp-response-error 'error-message "SMTP response error")
614 (put 'smtp-response-error 'error-conditions '(smtp-response-error smtp-error error))
616 (defun smtp-response-error (response)
617 (signal 'smtp-response-error response))
619 (defun smtp-read-response (connection)
621 (smtp-connection-decoder-internal connection))
622 (response-continue t)
624 (while response-continue
625 (goto-char smtp-read-point)
626 (while (not (search-forward smtp-end-of-line nil t))
627 (accept-process-output (smtp-connection-process-internal connection))
628 (goto-char smtp-read-point))
630 (let ((string (buffer-substring smtp-read-point (- (point) 2))))
631 (delete-region smtp-read-point (point))
632 (insert (funcall decoder string) smtp-end-of-line)))
635 (list (buffer-substring
636 (+ 4 smtp-read-point)
639 (prog1 smtp-read-point
640 (setq smtp-read-point (point))))
641 (if (looking-at "[1-5][0-9][0-9] ")
642 (setq response (cons (read (point-marker)) response)
643 response-continue nil)))
646 (defun smtp-send-command (connection command)
649 (smtp-connection-process-internal connection))
651 (smtp-connection-encoder-internal connection)))
652 (set-buffer (process-buffer process))
653 (goto-char (point-max))
654 (setq command (concat command smtp-end-of-line))
656 (setq smtp-read-point (point))
658 (setq command (funcall encoder command)))
659 (process-send-string process command))))
661 (defun smtp-send-data (connection data)
663 (smtp-connection-process-internal connection))
665 (smtp-connection-encoder-internal connection)))
666 ;; Escape "." at start of a line.
667 (if (eq (string-to-char data) ?.)
668 (setq data (concat "." data smtp-end-of-line))
669 (setq data (concat data smtp-end-of-line)))
671 (setq data (funcall encoder data)))
672 (process-send-string process data)))
674 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
675 "Get address list suitable for smtp RCPT TO:<address>."
676 (let ((simple-address-list "")
680 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
684 (set-buffer smtp-address-buffer)
685 (setq case-fold-search t)
687 (insert (save-excursion
688 (set-buffer smtp-text-buffer)
689 (buffer-substring-no-properties header-start header-end)))
690 (goto-char (point-min))
691 ;; RESENT-* fields should stop processing of regular fields.
693 (if (re-search-forward "^RESENT-TO:" header-end t)
695 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
696 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
698 (while (re-search-forward addr-regexp header-end t)
700 (setq this-line (match-beginning 0))
702 ;; get any continuation lines.
703 (while (and (looking-at "^[ \t]+") (< (point) header-end))
705 (setq this-line-end (point-marker))
706 (setq simple-address-list
707 (concat simple-address-list " "
708 (mail-strip-quoted-names
709 (buffer-substring this-line this-line-end)))))
712 (insert-string simple-address-list)
715 (subst-char-in-region (point-min) (point-max) 10 ? t)
717 (subst-char-in-region (point-min) (point-max) ?, ? t)
719 (subst-char-in-region (point-min) (point-max) 9 ? t)
721 (goto-char (point-min))
722 ;; tidyness in case hook is not robust when it looks at this
723 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
725 (goto-char (point-min))
726 (let (recipient-address-list)
727 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
729 (setq recipient-address-list
730 (cons (buffer-substring (match-beginning 1) (match-end 1))
731 recipient-address-list)))
732 recipient-address-list))
733 (kill-buffer smtp-address-buffer))))
737 ;;; smtp.el ends here