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)
130 (defvar smtp-read-point nil)
132 (defvar smtp-connection-alist nil)
134 (defvar smtp-submit-package-function #'smtp-submit-package)
137 ;;; A package contains a mail message, an envelope sender address,
138 ;;; and one or more envelope recipient addresses. In ESMTP model
139 ;;; the current sending package should be guaranteed to be accessible
140 ;;; anywhere from the hook methods (or SMTP commands).
143 (luna-define-class smtp-package ()
148 (luna-define-internal-accessors 'smtp-package))
150 (defun smtp-make-package (sender recipients buffer)
151 "Create a new package structure.
152 A package is a unit of SMTP message
153 SENDER specifies the package sender, a string.
154 RECIPIENTS is a list of recipients.
155 BUFFER may be a buffer or a buffer name which contains mail message."
156 (luna-make-entity 'smtp-package :sender sender :recipients recipients :buffer buffer))
158 (defun smtp-package-buffer-internal-size (package)
159 "Return the size of PACKAGE, an integer."
161 (set-buffer (smtp-package-buffer-internal package))
164 ;; Add one byte for each change-of-line
165 ;; because or CR-LF representation:
166 (count-lines (point-min) (point-max))
167 ;; For some reason, an empty line is
168 ;; added to the message. Maybe this
169 ;; is a bug, but it can't hurt to add
170 ;; those two bytes anyway:
172 (goto-char (point-min))
173 (while (re-search-forward "^\\." nil t)
174 (setq size (1+ size)))
177 ;;; @ SMTP connection
178 ;;; We should consider the function `open-network-stream' is a emulation
179 ;;; for another network stream. They are likely to be implemented with an
180 ;;; external program and the function `process-contact' returns the
181 ;;; process id instead of `(HOST SERVICE)' pair.
184 (luna-define-class smtp-connection ()
192 (luna-define-internal-accessors 'smtp-connection))
194 (defun smtp-make-connection (process server service)
195 "Create a new connection structure.
196 PROCESS is an internal subprocess-object. SERVER is name of the host
197 to connect to. SERVICE is name of the service desired."
198 (luna-make-entity 'smtp-connection :process process :server server :service service))
200 (luna-define-generic smtp-connection-opened (connection)
201 "Say whether the CONNECTION to server has been opened.")
203 (luna-define-generic smtp-close-connection (connection)
204 "Close the CONNECTION to server.")
206 (luna-define-method smtp-connection-opened ((connection smtp-connection))
207 (let ((process (smtp-connection-process-internal connection)))
208 (if (memq (process-status process) '(open run))
211 (luna-define-method smtp-close-connection ((connection smtp-connection))
212 (let ((process (smtp-connection-process-internal connection)))
213 (delete-process process)))
215 (defun smtp-make-fqdn ()
216 "Return user's fully qualified domain name."
219 (let ((system-name (system-name)))
222 (concat system-name "." smtp-local-domain))
223 ((string-match "[^.]\\.[^.]" system-name)
226 (error "Cannot generate valid FQDN"))))))
228 (defun smtp-find-connection (buffer)
229 "Find the connection delivering to BUFFER."
230 (let ((entry (assq buffer smtp-connection-alist))
233 (setq connection (nth 1 entry))
234 (if (smtp-connection-opened connection)
236 (setq smtp-connection-alist
237 (delq entry smtp-connection-alist))
241 (autoload 'starttls-open-stream "starttls")
242 (autoload 'starttls-negotiate "starttls"))
244 (defun smtp-open-connection (buffer server service)
245 "Open a SMTP connection for a service to a host.
246 Return a newly allocated connection-object.
247 BUFFER is the buffer to associate with the connection. SERVER is name
248 of the host to connect to. SERVICE is name of the service desired."
250 (binary-funcall smtp-open-connection-function
251 "SMTP" buffer server service))
254 (setq connection (smtp-make-connection process server service))
255 (set-process-filter process 'smtp-process-filter)
256 (setq smtp-connection-alist
257 (cons (list buffer connection)
258 smtp-connection-alist))
262 (autoload 'dig-invoke "dig")
263 (autoload 'dig-extract-rr "dig"))
265 (defun smtp-find-mx (domain &optional doerror)
267 ;; dig.el resolves only primally MX.
268 (cond ((setq server (smtp-dig domain "MX"))
269 (progn (string-match " \\([^ ]*\\)$" server)
270 (match-string 1 server)))
271 ((smtp-dig domain "A")
275 (error (format "SMTP cannot resolve %s" domain)))))))
277 (defun smtp-dig (domain type)
280 (setq dig-buf (dig-invoke domain type)))
282 (dig-extract-rr domain type)
283 (kill-buffer dig-buf))))
285 (defun smtp-find-server (recipients)
288 (mapcar (lambda (recipient)
290 (if (and (string-match "@\\([^\t\n ]*\\)" recipient)
293 (match-string 1 recipient))))
294 (cons server (list recipient))
295 (error (format "cannot find server for %s." recipient)))))
298 (while (setq rets (pop rec))
299 (if (setq ret (assoc (car rets) rec))
301 (append (cdr ret) (cdr rets)))
303 (append rlist (list rets)))))
307 (defun smtp-via-smtp (sender recipients buffer)
308 "Like `smtp-send-buffer', but sucks in any errors."
311 (smtp-send-buffer sender recipients buffer)
315 (make-obsolete 'smtp-via-smtp "It's old API.")
318 (defun smtp-send-buffer (sender recipients buffer)
320 SENDER is an envelope sender address.
321 RECIPIENTS is a list of envelope recipient addresses.
322 BUFFER may be a buffer or a buffer name which contains mail message."
323 (if smtp-send-by-myself
324 (smtp-send-buffer-by-myself sender recipients buffer)
326 (if (functionp smtp-server)
327 (funcall smtp-server sender recipients)
330 (smtp-make-package sender recipients buffer))
331 (smtp-open-connection-function
332 (if smtp-use-starttls
333 #'starttls-open-stream
334 smtp-open-connection-function)))
338 (format "*trace of SMTP session to %s*" server)))
340 (buffer-disable-undo)
341 (unless (smtp-find-connection (current-buffer))
342 (smtp-open-connection (current-buffer) server smtp-service))
343 (make-local-variable 'smtp-read-point)
344 (setq smtp-read-point (point-min))
345 (funcall smtp-submit-package-function package)))))
347 (defun smtp-submit-package (package)
350 (smtp-primitive-greeting package)
352 (smtp-primitive-ehlo package)
354 (smtp-primitive-helo package)))
355 (if smtp-use-starttls
357 (smtp-connection-extensions-internal
358 (smtp-find-connection (current-buffer))))
360 (smtp-primitive-starttls package)
361 (smtp-primitive-ehlo package))
362 (unless smtp-use-starttls-ignore-error
363 (error "STARTTLS is not supported on this server"))))
365 (smtp-primitive-auth package))
366 (smtp-primitive-mailfrom package)
367 (smtp-primitive-rcptto package)
368 (smtp-primitive-data package))
369 (let ((connection (smtp-find-connection (current-buffer))))
370 (when (smtp-connection-opened connection)
371 (smtp-primitive-quit package)
372 (smtp-close-connection connection)))))
374 (defun smtp-send-buffer-by-myself (sender recipients buffer)
375 "Send a message by myself.
376 SENDER is an envelope sender address.
377 RECIPIENTS is a list of envelope recipient addresses.
378 BUFFER may be a buffer or a buffer name which contains mail message."
380 (smtp-find-server recipients))
381 (smtp-open-connection-function
382 (if smtp-use-starttls
383 #'starttls-open-stream
384 smtp-open-connection-function))
387 (setq server (caar servers))
388 (setq recipients (cdar servers))
389 (if (not (and server recipients))
390 ;; MAILER-DAEMON is required. :)
391 (error (format "Cannot send <%s>"
392 (mapconcat 'concat recipients ">,<"))))
394 (smtp-make-package sender recipients buffer))
398 (format "*trace of SMTP session to %s*" server)))
400 (buffer-disable-undo)
401 (unless (smtp-find-connection (current-buffer))
402 (smtp-open-connection (current-buffer) server smtp-service))
403 (make-local-variable 'smtp-read-point)
404 (setq smtp-read-point (point-min))
405 (let ((smtp-use-sasl nil)
406 (smtp-use-starttls-ignore-error t))
407 (funcall smtp-submit-package-function package)))
408 (setq servers (cdr servers)))))
410 ;;; @ hook methods for `smtp-submit-package'
413 (defun smtp-primitive-greeting (package)
415 (smtp-find-connection (current-buffer)))
417 (smtp-read-response connection)))
418 (if (/= (car response) 220)
419 (smtp-response-error response))))
421 (defun smtp-primitive-ehlo (package)
423 (smtp-find-connection (current-buffer)))
425 (smtp-send-command connection (format "EHLO %s" (smtp-make-fqdn)))
426 (setq response (smtp-read-response connection))
427 (if (/= (car response) 250)
428 (smtp-response-error response))
429 (smtp-connection-set-extensions-internal
433 (split-string extension)))
435 (car (read-from-string
436 (downcase (car extensions)))))
440 (defun smtp-primitive-helo (package)
442 (smtp-find-connection (current-buffer)))
444 (smtp-send-command connection (format "HELO %s" (smtp-make-fqdn)))
445 (setq response (smtp-read-response connection))
446 (if (/= (car response) 250)
447 (smtp-response-error response))))
449 (defun smtp-primitive-auth (package)
451 (smtp-find-connection (current-buffer)))
453 (cdr (assq 'auth (smtp-connection-extensions-internal connection))))
455 (or smtp-sasl-mechanisms sasl-mechanisms))
457 (sasl-find-mechanism mechanisms))
463 (error "No authentication mechanism available"))
464 (setq client (sasl-make-client mechanism smtp-sasl-user-name "smtp"
465 (smtp-connection-server-internal connection)))
466 (if smtp-sasl-properties
467 (sasl-client-set-properties client smtp-sasl-properties))
468 (setq name (sasl-mechanism-name mechanism)
469 ;; Retrieve the initial response
470 step (sasl-next-step client nil))
473 (if (sasl-step-data step)
474 (format "AUTH %s %s" name (base64-encode-string (sasl-step-data step) t))
475 (format "AUTH %s" name)))
478 (setq response (smtp-read-response connection))
479 (when (= (car response) 235)
480 ;; The authentication process is finished.
481 (setq step (sasl-next-step client step))
484 (smtp-response-error response)) ;Bogus server?
485 (if (/= (car response) 334)
486 (smtp-response-error response))
487 (sasl-step-set-data step (base64-decode-string (nth 1 response)))
488 (setq step (sasl-next-step client step))
491 (if (sasl-step-data step)
492 (base64-encode-string (sasl-step-data step) t)
494 ;;; (smtp-connection-set-encoder-internal
495 ;;; connection (sasl-client-encoder client))
496 ;;; (smtp-connection-set-decoder-internal
497 ;;; connection (sasl-client-decoder client))
500 (defun smtp-primitive-starttls (package)
502 (smtp-find-connection (current-buffer)))
504 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
505 (smtp-send-command connection "STARTTLS")
506 (setq response (smtp-read-response connection))
507 (if (/= (car response) 220)
508 (smtp-response-error response))
509 (starttls-negotiate (smtp-connection-process-internal connection))))
511 (defun smtp-primitive-mailfrom (package)
513 (smtp-find-connection (current-buffer)))
515 (smtp-connection-extensions-internal
518 (smtp-package-sender-internal package))
521 ;; SIZE --- Message Size Declaration (RFC1870)
522 (if (and smtp-use-size
523 (assq 'size extensions))
524 (setq extension (format "SIZE=%d" (smtp-package-buffer-internal-size package))))
525 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
526 (if (and smtp-use-8bitmime
527 (assq '8bitmime extensions))
528 (setq extension (concat extension " BODY=8BITMIME")))
532 (format "MAIL FROM:<%s> %s" sender extension)
533 (format "MAIL FROM:<%s>" sender)))
534 (setq response (smtp-read-response connection))
535 (if (/= (car response) 250)
536 (smtp-response-error response))))
538 (defun smtp-primitive-rcptto (package)
540 (smtp-find-connection (current-buffer)))
542 (smtp-package-recipients-internal package))
546 connection (format "RCPT TO:<%s>" (pop recipients)))
547 (setq response (smtp-read-response connection))
548 (unless (memq (car response) '(250 251))
549 (smtp-response-error response)))))
551 (defun smtp-primitive-data (package)
553 (smtp-find-connection (current-buffer)))
555 (smtp-send-command connection "DATA")
556 (setq response (smtp-read-response connection))
557 (if (/= (car response) 354)
558 (smtp-response-error response))
560 (set-buffer (smtp-package-buffer-internal package))
561 (goto-char (point-min))
564 connection (buffer-substring (point) (progn (end-of-line)(point))))
565 (beginning-of-line 2)))
566 (smtp-send-command connection ".")
567 (setq response (smtp-read-response connection))
568 (if (/= (car response) 250)
569 (smtp-response-error response))))
571 (defun smtp-primitive-quit (package)
573 (smtp-find-connection (current-buffer)))
575 (smtp-send-command connection "QUIT")
576 (setq response (smtp-read-response connection))
577 (if (/= (car response) 221)
578 (smtp-response-error response))))
580 ;;; @ low level process manipulating function
582 (defun smtp-process-filter (process output)
584 (set-buffer (process-buffer process))
585 (goto-char (point-max))
588 (put 'smtp-error 'error-message "SMTP error")
589 (put 'smtp-error 'error-conditions '(smtp-error error))
591 (put 'smtp-response-error 'error-message "SMTP response error")
592 (put 'smtp-response-error 'error-conditions '(smtp-response-error smtp-error error))
594 (defun smtp-response-error (response)
595 (signal 'smtp-response-error response))
597 (defun smtp-read-response (connection)
599 (smtp-connection-decoder-internal connection))
600 (response-continue t)
602 (while response-continue
603 (goto-char smtp-read-point)
604 (while (not (search-forward "\r\n" nil t))
605 (accept-process-output (smtp-connection-process-internal connection))
606 (goto-char smtp-read-point))
608 (let ((string (buffer-substring smtp-read-point (- (point) 2))))
609 (delete-region smtp-read-point (point))
610 (insert (funcall decoder string) "\r\n")))
613 (list (buffer-substring
614 (+ 4 smtp-read-point)
617 (prog1 smtp-read-point
618 (setq smtp-read-point (point))))
619 (if (looking-at "[1-5][0-9][0-9] ")
620 (setq response (cons (read (point-marker)) response)
621 response-continue nil)))
624 (defun smtp-send-command (connection command)
627 (smtp-connection-process-internal connection))
629 (smtp-connection-encoder-internal connection)))
630 (set-buffer (process-buffer process))
631 (goto-char (point-max))
632 (setq command (concat command "\r\n"))
634 (setq smtp-read-point (point))
636 (setq command (funcall encoder command)))
637 (process-send-string process command))))
639 (defun smtp-send-data (connection data)
641 (smtp-connection-process-internal connection))
643 (smtp-connection-encoder-internal connection)))
644 ;; Escape "." at start of a line.
645 (if (eq (string-to-char data) ?.)
646 (setq data (concat "." data "\r\n"))
647 (setq data (concat data "\r\n")))
649 (setq data (funcall encoder data)))
650 (process-send-string process data)))
652 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
653 "Get address list suitable for smtp RCPT TO:<address>."
654 (let ((simple-address-list "")
658 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
662 (set-buffer smtp-address-buffer)
663 (setq case-fold-search t)
665 (insert (save-excursion
666 (set-buffer smtp-text-buffer)
667 (buffer-substring-no-properties header-start header-end)))
668 (goto-char (point-min))
669 ;; RESENT-* fields should stop processing of regular fields.
671 (if (re-search-forward "^RESENT-TO:" header-end t)
673 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
674 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
676 (while (re-search-forward addr-regexp header-end t)
678 (setq this-line (match-beginning 0))
680 ;; get any continuation lines.
681 (while (and (looking-at "^[ \t]+") (< (point) header-end))
683 (setq this-line-end (point-marker))
684 (setq simple-address-list
685 (concat simple-address-list " "
686 (mail-strip-quoted-names
687 (buffer-substring this-line this-line-end)))))
690 (insert-string simple-address-list)
693 (subst-char-in-region (point-min) (point-max) 10 ? t)
695 (subst-char-in-region (point-min) (point-max) ?, ? t)
697 (subst-char-in-region (point-min) (point-max) 9 ? t)
699 (goto-char (point-min))
700 ;; tidyness in case hook is not robust when it looks at this
701 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
703 (goto-char (point-min))
704 (let (recipient-address-list)
705 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
707 (setq recipient-address-list
708 (cons (buffer-substring (match-beginning 1) (match-end 1))
709 recipient-address-list)))
710 recipient-address-list))
711 (kill-buffer smtp-address-buffer))))
715 ;;; smtp.el ends here