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-starttls-program "starttls"
106 "The program to run in a subprocess to open an TLSv1 connection."
107 :group 'smtp-extensions)
109 (defcustom smtp-starttls-extra-args nil
110 "Extra arguments to `starttls-program'"
111 :group 'smtp-extensions)
113 (defcustom smtp-use-sasl nil
114 "If non-nil, use SMTP Authentication (RFC2554) if available."
116 :group 'smtp-extensions)
118 (defcustom smtp-sasl-user-name (user-login-name)
119 "Identification to be used for authorization."
121 :group 'smtp-extensions)
123 (defcustom smtp-sasl-properties nil
124 "Properties set to SASL client."
126 :group 'smtp-extensions)
128 (defcustom smtp-sasl-mechanisms nil
129 "List of authentication mechanisms."
130 :type '(repeat string)
131 :group 'smtp-extensions)
133 (defvar sasl-mechanisms)
136 (defvar smtp-open-connection-function #'open-network-stream
137 "*Function used for connecting to a SMTP server.
138 The function will be called with the same four arguments as
139 `open-network-stream' and should return a process object.
142 \(setq smtp-open-connection-function
143 #'(lambda (name buffer host service)
144 (let ((process-connection-type nil))
145 (start-process name buffer \"ssh\" \"-C\" host
146 \"nc\" host service))))
148 It connects to a SMTP server using \"ssh\" before actually connecting
149 to the SMTP port. Where the command \"nc\" is the netcat executable;
150 see http://www.atstake.com/research/tools/index.html#network_utilities
151 for details. In addition, you will have to modify the value for
152 `smtp-end-of-line' to \"\\n\" if you use \"telnet\" instead of \"nc\".")
154 (defvar smtp-read-point nil)
156 (defvar smtp-connection-alist nil)
158 (defvar smtp-submit-package-function #'smtp-submit-package)
160 (defvar smtp-end-of-line "\r\n"
161 "*String to use as end-of-line marker when talking to a SMTP server.
162 This is \"\\r\\n\" by default, but it may have to be \"\\n\" when using a non
163 native connection function. See also `smtp-open-connection-function'.")
166 ;;; A package contains a mail message, an envelope sender address,
167 ;;; and one or more envelope recipient addresses. In ESMTP model
168 ;;; the current sending package should be guaranteed to be accessible
169 ;;; anywhere from the hook methods (or SMTP commands).
172 (luna-define-class smtp-package ()
177 (luna-define-internal-accessors 'smtp-package))
179 (defun smtp-make-package (sender recipients buffer)
180 "Create a new package structure.
181 A package is a unit of SMTP message
182 SENDER specifies the package sender, a string.
183 RECIPIENTS is a list of recipients.
184 BUFFER may be a buffer or a buffer name which contains mail message."
185 (luna-make-entity 'smtp-package :sender sender :recipients recipients :buffer buffer))
187 (defun smtp-package-buffer-internal-size (package)
188 "Return the size of PACKAGE, an integer."
190 (set-buffer (smtp-package-buffer-internal package))
193 ;; Add one byte for each change-of-line
194 ;; because or CR-LF representation:
195 (count-lines (point-min) (point-max))
196 ;; For some reason, an empty line is
197 ;; added to the message. Maybe this
198 ;; is a bug, but it can't hurt to add
199 ;; those two bytes anyway:
201 (goto-char (point-min))
202 (while (re-search-forward "^\\." nil t)
203 (setq size (1+ size)))
206 ;;; @ SMTP connection
207 ;;; We should consider the function `open-network-stream' is a emulation
208 ;;; for another network stream. They are likely to be implemented with an
209 ;;; external program and the function `process-contact' returns the
210 ;;; process id instead of `(HOST SERVICE)' pair.
213 (luna-define-class smtp-connection ()
221 (luna-define-internal-accessors 'smtp-connection))
223 (defun smtp-make-connection (process server service)
224 "Create a new connection structure.
225 PROCESS is an internal subprocess-object. SERVER is name of the host
226 to connect to. SERVICE is name of the service desired."
227 (luna-make-entity 'smtp-connection :process process :server server :service service))
229 (luna-define-generic smtp-connection-opened (connection)
230 "Say whether the CONNECTION to server has been opened.")
232 (luna-define-generic smtp-close-connection (connection)
233 "Close the CONNECTION to server.")
235 (luna-define-method smtp-connection-opened ((connection smtp-connection))
236 (let ((process (smtp-connection-process-internal connection)))
237 (if (memq (process-status process) '(open run))
240 (luna-define-method smtp-close-connection ((connection smtp-connection))
241 (let ((process (smtp-connection-process-internal connection)))
242 (delete-process process)))
244 (defun smtp-make-fqdn ()
245 "Return user's fully qualified domain name."
248 (let ((system-name (system-name)))
251 (concat system-name "." smtp-local-domain))
252 ((string-match "[^.]\\.[^.]" system-name)
255 (error "Cannot generate valid FQDN"))))))
257 (defun smtp-find-connection (buffer)
258 "Find the connection delivering to BUFFER."
259 (let ((entry (assq buffer smtp-connection-alist))
262 (setq connection (nth 1 entry))
263 (if (smtp-connection-opened connection)
265 (setq smtp-connection-alist
266 (delq entry smtp-connection-alist))
270 (autoload 'starttls-open-stream "starttls")
271 (autoload 'starttls-negotiate "starttls"))
273 (defun smtp-open-connection (buffer server service)
274 "Open a SMTP connection for a service to a host.
275 Return a newly allocated connection-object.
276 BUFFER is the buffer to associate with the connection. SERVER is name
277 of the host to connect to. SERVICE is name of the service desired."
279 (binary-funcall smtp-open-connection-function
280 "SMTP" buffer server service))
283 (setq connection (smtp-make-connection process server service))
284 (set-process-filter process 'smtp-process-filter)
285 (setq smtp-connection-alist
286 (cons (list buffer connection)
287 smtp-connection-alist))
291 (autoload 'dig-invoke "dig")
292 (autoload 'dig-extract-rr "dig"))
294 (defun smtp-find-mx (domain &optional doerror)
296 ;; dig.el resolves only primally MX.
297 (cond ((setq server (smtp-dig domain "MX"))
298 (progn (string-match " \\([^ ]*\\)$" server)
299 (match-string 1 server)))
300 ((smtp-dig domain "A")
304 (error (format "SMTP cannot resolve %s" domain)))))))
306 (defun smtp-dig (domain type)
309 (setq dig-buf (dig-invoke domain type)))
311 (dig-extract-rr domain type)
312 (kill-buffer dig-buf))))
314 (defun smtp-find-server (recipients)
317 (mapcar (lambda (recipient)
319 (if (and (string-match "@\\([^\t\n ]*\\)" recipient)
322 (match-string 1 recipient))))
323 (cons server (list recipient))
324 (error (format "cannot find server for %s." recipient)))))
327 (while (setq rets (pop rec))
328 (if (setq ret (assoc (car rets) rec))
330 (append (cdr ret) (cdr rets)))
332 (append rlist (list rets)))))
336 (defun smtp-via-smtp (sender recipients buffer)
337 "Like `smtp-send-buffer', but sucks in any errors."
340 (smtp-send-buffer sender recipients buffer)
344 (make-obsolete 'smtp-via-smtp "It's old API.")
347 (defun smtp-send-buffer (sender recipients buffer)
349 SENDER is an envelope sender address.
350 RECIPIENTS is a list of envelope recipient addresses.
351 BUFFER may be a buffer or a buffer name which contains mail message."
352 (if smtp-send-by-myself
353 (smtp-send-buffer-by-myself sender recipients buffer)
355 (if (functionp smtp-server)
356 (funcall smtp-server sender recipients)
358 (error "`smtp-server' not defined"))))
360 (smtp-make-package sender recipients buffer))
361 (starttls-program smtp-starttls-program)
362 (starttls-extra-args smtp-starttls-extra-args)
363 (smtp-open-connection-function
364 (if smtp-use-starttls
365 #'starttls-open-stream
366 smtp-open-connection-function)))
370 (format "*trace of SMTP session to %s*" server)))
372 (buffer-disable-undo)
373 (unless (smtp-find-connection (current-buffer))
374 (smtp-open-connection (current-buffer) server smtp-service))
375 (make-local-variable 'smtp-read-point)
376 (setq smtp-read-point (point-min))
377 (funcall smtp-submit-package-function package)))))
379 (defun smtp-submit-package (package)
382 (smtp-primitive-greeting package)
384 (smtp-primitive-ehlo package)
386 (smtp-primitive-helo package)))
387 (if smtp-use-starttls
389 (smtp-connection-extensions-internal
390 (smtp-find-connection (current-buffer))))
392 (smtp-primitive-starttls package)
393 (smtp-primitive-ehlo package))
394 (unless smtp-use-starttls-ignore-error
395 (error "STARTTLS is not supported on this server"))))
397 (smtp-primitive-auth package))
398 (smtp-primitive-mailfrom package)
399 (smtp-primitive-rcptto package)
400 (smtp-primitive-data package))
401 (let ((connection (smtp-find-connection (current-buffer))))
402 (when (smtp-connection-opened connection)
403 (smtp-primitive-quit package)
404 (smtp-close-connection connection)))))
406 (defun smtp-send-buffer-by-myself (sender recipients buffer)
407 "Send a message by myself.
408 SENDER is an envelope sender address.
409 RECIPIENTS is a list of envelope recipient addresses.
410 BUFFER may be a buffer or a buffer name which contains mail message."
412 (smtp-find-server recipients))
413 (smtp-open-connection-function
414 (if smtp-use-starttls
415 #'starttls-open-stream
416 smtp-open-connection-function))
419 (setq server (caar servers))
420 (setq recipients (cdar servers))
421 (if (not (and server recipients))
422 ;; MAILER-DAEMON is required. :)
423 (error (format "Cannot send <%s>"
424 (mapconcat 'concat recipients ">,<"))))
426 (smtp-make-package sender recipients buffer))
430 (format "*trace of SMTP session to %s*" server)))
432 (buffer-disable-undo)
433 (unless (smtp-find-connection (current-buffer))
434 (smtp-open-connection (current-buffer) server smtp-service))
435 (make-local-variable 'smtp-read-point)
436 (setq smtp-read-point (point-min))
437 (let ((smtp-use-sasl nil)
438 (smtp-use-starttls-ignore-error t))
439 (funcall smtp-submit-package-function package)))
440 (setq servers (cdr servers)))))
442 ;;; @ hook methods for `smtp-submit-package'
445 (defun smtp-primitive-greeting (package)
447 (smtp-find-connection (current-buffer)))
449 (smtp-read-response connection)))
450 (if (/= (car response) 220)
451 (smtp-response-error response))))
453 (defun smtp-primitive-ehlo (package)
455 (smtp-find-connection (current-buffer)))
457 (smtp-send-command connection (format "EHLO %s" (smtp-make-fqdn)))
458 (setq response (smtp-read-response connection))
459 (if (/= (car response) 250)
460 (smtp-response-error response))
461 (smtp-connection-set-extensions-internal
465 (split-string extension)))
467 (car (read-from-string
468 (downcase (car extensions)))))
472 (defun smtp-primitive-helo (package)
474 (smtp-find-connection (current-buffer)))
476 (smtp-send-command connection (format "HELO %s" (smtp-make-fqdn)))
477 (setq response (smtp-read-response connection))
478 (if (/= (car response) 250)
479 (smtp-response-error response))))
481 (defun smtp-primitive-auth (package)
483 (smtp-find-connection (current-buffer)))
485 (cdr (assq 'auth (smtp-connection-extensions-internal connection))))
487 (or smtp-sasl-mechanisms sasl-mechanisms))
489 (sasl-find-mechanism mechanisms))
495 (error "No authentication mechanism available"))
496 (setq client (sasl-make-client mechanism smtp-sasl-user-name "smtp"
497 (smtp-connection-server-internal connection)))
498 (if smtp-sasl-properties
499 (sasl-client-set-properties client smtp-sasl-properties))
500 (setq name (sasl-mechanism-name mechanism)
501 ;; Retrieve the initial response
502 step (sasl-next-step client nil))
505 (if (sasl-step-data step)
506 (format "AUTH %s %s" name (base64-encode-string (sasl-step-data step) t))
507 (format "AUTH %s" name)))
510 (setq response (smtp-read-response connection))
511 (when (= (car response) 235)
512 ;; The authentication process is finished.
513 (setq step (sasl-next-step client step))
516 (smtp-response-error response)) ;Bogus server?
517 (if (/= (car response) 334)
518 (smtp-response-error response))
519 (sasl-step-set-data step (base64-decode-string (nth 1 response)))
520 (setq step (sasl-next-step client step))
523 (if (sasl-step-data step)
524 (base64-encode-string (sasl-step-data step) t)
526 ;;; (smtp-connection-set-encoder-internal
527 ;;; connection (sasl-client-encoder client))
528 ;;; (smtp-connection-set-decoder-internal
529 ;;; connection (sasl-client-decoder client))
532 (defun smtp-primitive-starttls (package)
534 (smtp-find-connection (current-buffer)))
536 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
537 (smtp-send-command connection "STARTTLS")
538 (setq response (smtp-read-response connection))
539 (if (/= (car response) 220)
540 (smtp-response-error response))
541 (starttls-negotiate (smtp-connection-process-internal connection))))
543 (defun smtp-primitive-mailfrom (package)
545 (smtp-find-connection (current-buffer)))
547 (smtp-connection-extensions-internal
550 (smtp-package-sender-internal package))
553 ;; SIZE --- Message Size Declaration (RFC1870)
554 (if (and smtp-use-size
555 (assq 'size extensions))
556 (setq extension (format "SIZE=%d" (smtp-package-buffer-internal-size package))))
557 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
558 (if (and smtp-use-8bitmime
559 (assq '8bitmime extensions))
560 (setq extension (concat extension " BODY=8BITMIME")))
564 (format "MAIL FROM:<%s> %s" sender extension)
565 (format "MAIL FROM:<%s>" sender)))
566 (setq response (smtp-read-response connection))
567 (if (/= (car response) 250)
568 (smtp-response-error response))))
570 (defun smtp-primitive-rcptto (package)
572 (smtp-find-connection (current-buffer)))
574 (smtp-package-recipients-internal package))
578 connection (format "RCPT TO:<%s>" (pop recipients)))
579 (setq response (smtp-read-response connection))
580 (unless (memq (car response) '(250 251))
581 (smtp-response-error response)))))
583 (defun smtp-primitive-data (package)
585 (smtp-find-connection (current-buffer)))
587 (smtp-send-command connection "DATA")
588 (setq response (smtp-read-response connection))
589 (if (/= (car response) 354)
590 (smtp-response-error response))
592 (set-buffer (smtp-package-buffer-internal package))
593 (goto-char (point-min))
596 connection (buffer-substring (point) (progn (end-of-line)(point))))
597 (beginning-of-line 2)))
598 (smtp-send-command connection ".")
599 (setq response (smtp-read-response connection))
600 (if (/= (car response) 250)
601 (smtp-response-error response))))
603 (defun smtp-primitive-quit (package)
605 (smtp-find-connection (current-buffer)))
607 (smtp-send-command connection "QUIT")
608 (setq response (smtp-read-response connection))
609 (if (/= (car response) 221)
610 (smtp-response-error response))))
612 ;;; @ low level process manipulating function
614 (defun smtp-process-filter (process output)
616 (set-buffer (process-buffer process))
617 (goto-char (point-max))
620 (put 'smtp-error 'error-message "SMTP error")
621 (put 'smtp-error 'error-conditions '(smtp-error error))
623 (put 'smtp-response-error 'error-message "SMTP response error")
624 (put 'smtp-response-error 'error-conditions '(smtp-response-error smtp-error error))
626 (defun smtp-response-error (response)
627 (signal 'smtp-response-error response))
629 (defun smtp-read-response (connection)
631 (smtp-connection-decoder-internal connection))
632 (response-continue t)
634 (while response-continue
635 (goto-char smtp-read-point)
636 (while (not (search-forward smtp-end-of-line nil t))
637 (accept-process-output (smtp-connection-process-internal connection))
638 (goto-char smtp-read-point))
640 (let ((string (buffer-substring smtp-read-point (- (point) 2))))
641 (delete-region smtp-read-point (point))
642 (insert (funcall decoder string) smtp-end-of-line)))
645 (list (buffer-substring
646 (+ 4 smtp-read-point)
649 (prog1 smtp-read-point
650 (setq smtp-read-point (point))))
651 (if (looking-at "[1-5][0-9][0-9] ")
652 (setq response (cons (read (point-marker)) response)
653 response-continue nil)))
656 (defun smtp-send-command (connection command)
659 (smtp-connection-process-internal connection))
661 (smtp-connection-encoder-internal connection)))
662 (set-buffer (process-buffer process))
663 (goto-char (point-max))
664 (setq command (concat command smtp-end-of-line))
666 (setq smtp-read-point (point))
668 (setq command (funcall encoder command)))
669 (process-send-string process command))))
671 (defun smtp-send-data (connection data)
673 (smtp-connection-process-internal connection))
675 (smtp-connection-encoder-internal connection)))
676 ;; Escape "." at start of a line.
677 (if (eq (string-to-char data) ?.)
678 (setq data (concat "." data smtp-end-of-line))
679 (setq data (concat data smtp-end-of-line)))
681 (setq data (funcall encoder data)))
682 (process-send-string process data)))
684 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
685 "Get address list suitable for smtp RCPT TO:<address>."
686 (let ((simple-address-list "")
690 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
694 (set-buffer smtp-address-buffer)
695 (setq case-fold-search t)
697 (insert (save-excursion
698 (set-buffer smtp-text-buffer)
699 (buffer-substring-no-properties header-start header-end)))
700 (goto-char (point-min))
701 ;; RESENT-* fields should stop processing of regular fields.
703 (if (re-search-forward "^RESENT-TO:" header-end t)
705 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
706 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
708 (while (re-search-forward addr-regexp header-end t)
710 (setq this-line (match-beginning 0))
712 ;; get any continuation lines.
713 (while (and (looking-at "^[ \t]+") (< (point) header-end))
715 (setq this-line-end (point-marker))
716 (setq simple-address-list
717 (concat simple-address-list " "
718 (mail-strip-quoted-names
719 (buffer-substring this-line this-line-end)))))
722 (insert-string simple-address-list)
725 (subst-char-in-region (point-min) (point-max) 10 ? t)
727 (subst-char-in-region (point-min) (point-max) ?, ? t)
729 (subst-char-in-region (point-min) (point-max) 9 ? t)
731 (goto-char (point-min))
732 ;; tidyness in case hook is not robust when it looks at this
733 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
735 (goto-char (point-min))
736 (let (recipient-address-list)
737 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
739 (setq recipient-address-list
740 (cons (buffer-substring (match-beginning 1) (match-end 1))
741 recipient-address-list)))
742 recipient-address-list))
743 (kill-buffer smtp-address-buffer))))
747 ;;; smtp.el ends here