1 ;;; smtp.el --- basic functions to send mail with SMTP server
3 ;; Copyright (C) 1995, 1996, 1998, 1999 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
40 "SMTP protocol for sending mail."
43 (defgroup smtp-extensions nil
44 "SMTP service extensions (RFC1869)."
47 (defcustom smtp-default-server nil
48 "Specify default SMTP server."
49 :type '(choice (const nil) string)
52 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
53 "The name of the host running SMTP server.
54 It can also be a function
55 called from `smtp-via-smtp' with arguments SENDER and RECIPIENTS."
56 :type '(choice (string :tag "Name")
57 (function :tag "Function"))
60 (defcustom smtp-service "smtp"
61 "SMTP service port number. \"smtp\" or 25."
62 :type '(choice (integer :tag "25" 25)
63 (string :tag "smtp" "smtp"))
66 (defcustom smtp-local-domain nil
67 "Local domain name without a host name.
68 If the function (system-name) returns the full internet address,
69 don't define this value."
70 :type '(choice (const nil) string)
73 (defcustom smtp-fqdn nil
74 "Fully qualified domain name used for Message-ID."
75 :type '(choice (const nil) string)
78 (defcustom smtp-use-8bitmime t
79 "If non-nil, use ESMTP 8BITMIME (RFC1652) if available."
81 :group 'smtp-extensions)
83 (defcustom smtp-use-size t
84 "If non-nil, use ESMTP SIZE (RFC1870) if available."
86 :group 'smtp-extensions)
88 (defcustom smtp-use-starttls nil
89 "If non-nil, use STARTTLS (RFC2595) if available."
91 :group 'smtp-extensions)
93 (defcustom smtp-use-sasl nil
94 "If non-nil, use SMTP Authentication (RFC2554) if available."
96 :group 'smtp-extensions)
98 (defcustom smtp-sasl-user-name (user-login-name)
99 "Identification to be used for authorization."
101 :group 'smtp-extensions)
103 (defcustom smtp-sasl-properties nil
104 "Properties set to SASL client."
106 :group 'smtp-extensions)
108 (defcustom smtp-sasl-mechanisms nil
109 "List of authentication mechanisms."
110 :type '(repeat string)
111 :group 'smtp-extensions)
113 (defvar sasl-mechanisms)
115 (autoload 'binary-open-network-stream "raw-io")
117 (defvar smtp-open-connection-function #'binary-open-network-stream)
119 (defvar smtp-read-point nil)
121 (defvar smtp-connection-alist nil)
123 (defvar smtp-submit-package-function #'smtp-submit-package)
126 ;;; A package contains a mail message, an envelope sender address,
127 ;;; and one or more envelope recipient addresses. In ESMTP model
128 ;;; the current sending package should be guaranteed to be accessible
129 ;;; anywhere from the hook methods (or SMTP commands).
132 (luna-define-class smtp-package ()
137 (luna-define-internal-accessors 'smtp-package))
139 (defun smtp-make-package (sender recipients buffer)
140 "Create a new package structure.
141 A package is a unit of SMTP message
142 SENDER specifies the package sender, a string.
143 RECIPIENTS is a list of recipients.
144 BUFFER may be a buffer or a buffer name which contains mail message."
145 (luna-make-entity 'smtp-package :sender sender :recipients recipients :buffer buffer))
147 (defun smtp-package-buffer-internal-size (package)
148 "Return the size of PACKAGE, an integer."
150 (set-buffer (smtp-package-buffer-internal package))
153 ;; Add one byte for each change-of-line
154 ;; because or CR-LF representation:
155 (count-lines (point-min) (point-max))
156 ;; For some reason, an empty line is
157 ;; added to the message. Maybe this
158 ;; is a bug, but it can't hurt to add
159 ;; those two bytes anyway:
161 (goto-char (point-min))
162 (while (re-search-forward "^\\." nil t)
163 (setq size (1+ size)))
166 ;;; @ SMTP connection
167 ;;; We should consider the function `open-network-stream' is a emulation
168 ;;; for another network stream. They are likely to be implemented with an
169 ;;; external program and the function `process-contact' returns the
170 ;;; process id instead of `(HOST SERVICE)' pair.
173 (luna-define-class smtp-connection ()
181 (luna-define-internal-accessors 'smtp-connection))
183 (defun smtp-make-connection (process server service)
184 "Create a new connection structure.
185 PROCESS is an internal subprocess-object. SERVER is name of the host
186 to connect to. SERVICE is name of the service desired."
187 (luna-make-entity 'smtp-connection :process process :server server :service service))
189 (luna-define-generic smtp-connection-opened (connection)
190 "Say whether the CONNECTION to server has been opened.")
192 (luna-define-generic smtp-close-connection (connection)
193 "Close the CONNECTION to server.")
195 (luna-define-method smtp-connection-opened ((connection smtp-connection))
196 (let ((process (smtp-connection-process-internal connection)))
197 (if (memq (process-status process) '(open run))
200 (luna-define-method smtp-close-connection ((connection smtp-connection))
201 (let ((process (smtp-connection-process-internal connection)))
202 (delete-process process)))
204 (defun smtp-make-fqdn ()
205 "Return user's fully qualified domain name."
208 (let ((system-name (system-name)))
211 (concat system-name "." smtp-local-domain))
212 ((string-match "[^.]\\.[^.]" system-name)
215 (error "Cannot generate valid FQDN"))))))
217 (defun smtp-find-connection (buffer)
218 "Find the connection delivering to BUFFER."
219 (let ((entry (assq buffer smtp-connection-alist))
222 (setq connection (nth 1 entry))
223 (if (smtp-connection-opened connection)
225 (setq smtp-connection-alist
226 (delq entry smtp-connection-alist))
230 (autoload 'starttls-open-stream "starttls")
231 (autoload 'starttls-negotiate "starttls"))
233 (defun smtp-open-connection (buffer server service)
234 "Open a SMTP connection for a service to a host.
235 Return a newly allocated connection-object.
236 BUFFER is the buffer to associate with the connection. SERVER is name
237 of the host to connect to. SERVICE is name of the service desired."
239 (funcall smtp-open-connection-function
240 "SMTP" buffer server service))
243 (setq connection (smtp-make-connection process server service))
244 (set-process-filter process 'smtp-process-filter)
245 (setq smtp-connection-alist
246 (cons (list buffer connection)
247 smtp-connection-alist))
251 (defun smtp-via-smtp (sender recipients buffer)
254 (smtp-send-buffer sender recipients buffer)
258 (make-obsolete 'smtp-via-smtp "It's old API.")
261 (defun smtp-send-buffer (sender recipients buffer)
263 (if (functionp smtp-server)
264 (funcall smtp-server sender recipients)
267 (smtp-make-package sender recipients buffer))
268 (smtp-open-connection-function
269 (if smtp-use-starttls
270 #'starttls-open-stream
271 smtp-open-connection-function)))
275 (format "*trace of SMTP session to %s*" server)))
277 (buffer-disable-undo)
278 (unless (smtp-find-connection (current-buffer))
279 (smtp-open-connection (current-buffer) server smtp-service))
280 (make-local-variable 'smtp-read-point)
281 (setq smtp-read-point (point-min))
282 (funcall smtp-submit-package-function package))))
284 (defun smtp-submit-package (package)
287 (smtp-primitive-greeting package)
289 (smtp-primitive-ehlo package)
291 (smtp-primitive-helo package)))
292 (if smtp-use-starttls
293 (smtp-primitive-starttls package))
295 (smtp-primitive-auth package))
296 (smtp-primitive-mailfrom package)
297 (smtp-primitive-rcptto package)
298 (smtp-primitive-data package))
299 (let ((connection (smtp-find-connection (current-buffer))))
300 (when (smtp-connection-opened connection)
301 (smtp-primitive-quit package)
302 (smtp-close-connection connection)))))
304 ;;; @ hook methods for `smtp-submit-package'
307 (defun smtp-primitive-greeting (package)
309 (smtp-find-connection (current-buffer)))
311 (smtp-read-response connection)))
312 (if (/= (car response) 220)
313 (smtp-response-error response))))
315 (defun smtp-primitive-ehlo (package)
317 (smtp-find-connection (current-buffer)))
319 (smtp-send-command connection (format "EHLO %s" (smtp-make-fqdn)))
320 (setq response (smtp-read-response connection))
321 (if (/= (car response) 250)
322 (smtp-response-error response))
323 (smtp-connection-set-extensions-internal
327 (split-string extension)))
329 (car (read-from-string
330 (downcase (car extensions)))))
334 (defun smtp-primitive-helo (package)
336 (smtp-find-connection (current-buffer)))
338 (smtp-send-command connection (format "HELO %s" (smtp-make-fqdn)))
339 (setq response (smtp-read-response connection))
340 (if (/= (car response) 250)
341 (smtp-response-error response))))
343 (defun smtp-primitive-auth (package)
345 (smtp-find-connection (current-buffer)))
347 (cdr (assq 'auth (smtp-connection-extensions-internal connection))))
349 (or smtp-sasl-mechanisms sasl-mechanisms))
351 (sasl-find-mechanism mechanisms))
357 (error "No authentication mechanism available"))
358 (setq client (sasl-make-client mechanism smtp-sasl-user-name "smtp"
359 (smtp-connection-server-internal connection)))
360 (if smtp-sasl-properties
361 (sasl-client-set-properties client smtp-sasl-properties))
362 (setq name (sasl-mechanism-name mechanism)
363 ;; Retrieve the initial response
364 step (sasl-next-step client nil))
367 (if (sasl-step-data step)
368 (format "AUTH %s %s" name (base64-encode-string (sasl-step-data step) t))
369 (format "AUTH %s" name)))
372 (setq response (smtp-read-response connection))
373 (when (= (car response) 235)
374 ;; The authentication process is finished.
375 (setq step (sasl-next-step client step))
378 (smtp-response-error response)) ;Bogus server?
379 (if (/= (car response) 334)
380 (smtp-response-error response))
381 (sasl-step-set-data step (base64-decode-string (nth 1 response)))
382 (setq step (sasl-next-step client step))
385 (if (sasl-step-data step)
386 (base64-encode-string (sasl-step-data step) t)
388 ;;; (smtp-connection-set-encoder-internal
389 ;;; connection (sasl-client-encoder client))
390 ;;; (smtp-connection-set-decoder-internal
391 ;;; connection (sasl-client-decoder client))
394 (defun smtp-primitive-starttls (package)
396 (smtp-find-connection (current-buffer)))
398 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
399 (smtp-send-command connection "STARTTLS")
400 (setq response (smtp-read-response connection))
401 (if (/= (car response) 220)
402 (smtp-response-error response))
403 (starttls-negotiate (smtp-connection-process-internal connection))))
405 (defun smtp-primitive-mailfrom (package)
407 (smtp-find-connection (current-buffer)))
409 (smtp-connection-extensions-internal
412 (smtp-package-sender-internal package))
415 ;; SIZE --- Message Size Declaration (RFC1870)
416 (if (and smtp-use-size
417 (assq 'size extensions))
418 (setq extension (format "SIZE=%d" (smtp-package-buffer-internal-size package))))
419 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
420 (if (and smtp-use-8bitmime
421 (assq '8bitmime extensions))
422 (setq extension (concat extension " BODY=8BITMIME")))
426 (format "MAIL FROM:<%s> %s" sender extension)
427 (format "MAIL FROM:<%s>" sender)))
428 (setq response (smtp-read-response connection))
429 (if (/= (car response) 250)
430 (smtp-response-error response))))
432 (defun smtp-primitive-rcptto (package)
434 (smtp-find-connection (current-buffer)))
436 (smtp-package-recipients-internal package))
440 connection (format "RCPT TO:<%s>" (pop recipients)))
441 (setq response (smtp-read-response connection))
442 (unless (memq (car response) '(250 251))
443 (smtp-response-error response)))))
445 (defun smtp-primitive-data (package)
447 (smtp-find-connection (current-buffer)))
449 (smtp-send-command connection "DATA")
450 (setq response (smtp-read-response connection))
451 (if (/= (car response) 354)
452 (smtp-response-error response))
454 (set-buffer (smtp-package-buffer-internal package))
455 (goto-char (point-min))
458 connection (buffer-substring (point) (progn (end-of-line)(point))))
459 (beginning-of-line 2)))
460 (smtp-send-command connection ".")
461 (setq response (smtp-read-response connection))
462 (if (/= (car response) 250)
463 (smtp-response-error response))))
465 (defun smtp-primitive-quit (package)
467 (smtp-find-connection (current-buffer)))
469 (smtp-send-command connection "QUIT")
470 (setq response (smtp-read-response connection))
471 (if (/= (car response) 221)
472 (smtp-response-error response))))
474 ;;; @ low level process manipulating function
476 (defun smtp-process-filter (process output)
478 (set-buffer (process-buffer process))
479 (goto-char (point-max))
482 (put 'smtp-error 'error-message "SMTP error")
483 (put 'smtp-error 'error-conditions '(smtp-error error))
485 (put 'smtp-response-error 'error-message "SMTP response error")
486 (put 'smtp-response-error 'error-conditions '(smtp-response-error smtp-error error))
488 (defun smtp-response-error (response)
489 (signal 'smtp-response-error response))
491 (defun smtp-read-response (connection)
493 (smtp-connection-decoder-internal connection))
494 (response-continue t)
496 (while response-continue
497 (goto-char smtp-read-point)
498 (while (not (search-forward "\r\n" nil t))
499 (accept-process-output (smtp-connection-process-internal connection))
500 (goto-char smtp-read-point))
502 (let ((string (buffer-substring smtp-read-point (- (point) 2))))
503 (delete-region smtp-read-point (point))
504 (insert (funcall decoder string) "\r\n")))
507 (list (buffer-substring
508 (+ 4 smtp-read-point)
511 (prog1 smtp-read-point
512 (setq smtp-read-point (point))))
513 (if (looking-at "[1-5][0-9][0-9] ")
514 (setq response (cons (read (point-marker)) response)
515 response-continue nil)))
518 (defun smtp-send-command (connection command)
521 (smtp-connection-process-internal connection))
523 (smtp-connection-encoder-internal connection)))
524 (set-buffer (process-buffer process))
525 (goto-char (point-max))
526 (setq command (concat command "\r\n"))
528 (setq smtp-read-point (point))
530 (setq command (funcall encoder command)))
531 (process-send-string process command))))
533 (defun smtp-send-data (connection data)
535 (smtp-connection-process-internal connection))
537 (smtp-connection-encoder-internal connection)))
538 ;; Escape "." at start of a line.
539 (if (eq (string-to-char data) ?.)
540 (setq data (concat "." data "\r\n"))
541 (setq data (concat data "\r\n")))
543 (setq data (funcall encoder data)))
544 (process-send-string process data)))
546 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
547 "Get address list suitable for smtp RCPT TO:<address>."
548 (let ((simple-address-list "")
552 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
556 (set-buffer smtp-address-buffer)
557 (setq case-fold-search t)
559 (insert (save-excursion
560 (set-buffer smtp-text-buffer)
561 (buffer-substring-no-properties header-start header-end)))
562 (goto-char (point-min))
563 ;; RESENT-* fields should stop processing of regular fields.
565 (if (re-search-forward "^RESENT-TO:" header-end t)
567 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
568 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
570 (while (re-search-forward addr-regexp header-end t)
572 (setq this-line (match-beginning 0))
574 ;; get any continuation lines.
575 (while (and (looking-at "^[ \t]+") (< (point) header-end))
577 (setq this-line-end (point-marker))
578 (setq simple-address-list
579 (concat simple-address-list " "
580 (mail-strip-quoted-names
581 (buffer-substring this-line this-line-end)))))
584 (insert-string simple-address-list)
587 (subst-char-in-region (point-min) (point-max) 10 ? t)
589 (subst-char-in-region (point-min) (point-max) ?, ? t)
591 (subst-char-in-region (point-min) (point-max) 9 ? t)
593 (goto-char (point-min))
594 ;; tidyness in case hook is not robust when it looks at this
595 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
597 (goto-char (point-min))
598 (let (recipient-address-list)
599 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
601 (setq recipient-address-list
602 (cons (buffer-substring (match-beginning 1) (match-end 1))
603 recipient-address-list)))
604 recipient-address-list))
605 (kill-buffer smtp-address-buffer))))
609 ;;; smtp.el ends here