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.
36 (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 (defvar smtp-open-connection-function #'open-network-stream)
117 (defvar smtp-read-point nil)
119 (defvar smtp-connection-alist nil)
121 (defvar smtp-submit-package-function #'smtp-submit-package)
123 ;;; @ SMTP package structure
124 ;;; A package contains a mail message, an envelope sender address,
125 ;;; and one or more envelope recipient addresses. In ESMTP model
126 ;;; the current sending package should be guaranteed to be accessible
127 ;;; anywhere from the hook methods (or SMTP commands).
129 (defmacro smtp-package-sender (package)
130 "Return the sender of PACKAGE, a string."
133 (defmacro smtp-package-recipients (package)
134 "Return the recipients of PACKAGE, a list of strings."
137 (defmacro smtp-package-buffer (package)
138 "Return the data of PACKAGE, a buffer."
141 (defmacro smtp-make-package (sender recipients buffer)
142 "Create a new package structure.
143 A package is a unit of SMTP message
144 SENDER specifies the package sender, a string.
145 RECIPIENTS is a list of recipients.
146 BUFFER may be a buffer or a buffer name which contains mail message."
147 `(vector ,sender ,recipients ,buffer))
149 (defun smtp-package-buffer-size (package)
150 "Return the size of PACKAGE, an integer."
152 (set-buffer (smtp-package-buffer package))
155 ;; Add one byte for each change-of-line
156 ;; because or CR-LF representation:
157 (count-lines (point-min) (point-max))
158 ;; For some reason, an empty line is
159 ;; added to the message. Maybe this
160 ;; is a bug, but it can't hurt to add
161 ;; those two bytes anyway:
163 (goto-char (point-min))
164 (while (re-search-forward "^\\." nil t)
165 (setq size (1+ size)))
168 ;;; @ SMTP connection structure
169 ;;; We should consider the function `open-network-stream' is a emulation
170 ;;; for another network stream. They are likely to be implemented with an
171 ;;; external program and the function `process-contact' returns the
172 ;;; process id instead of `(HOST SERVICE)' pair.
174 (defmacro smtp-connection-process (connection)
175 "Return the subprocess-object of CONNECTION."
176 `(aref ,connection 0))
178 (defmacro smtp-connection-server (connection)
179 "Return the server of CONNECTION, a string."
180 `(aref ,connection 1))
182 (defmacro smtp-connection-service (connection)
183 "Return the service of CONNECTION, a string or an integer."
184 `(aref ,connection 2))
186 (defmacro smtp-connection-extensions (connection)
187 "Return the SMTP extensions of CONNECTION, a list of strings."
188 `(aref ,connection 3))
190 (defmacro smtp-connection-set-extensions (connection extensions)
191 "Set the SMTP extensions of CONNECTION.
192 EXTENSIONS is a list of cons cells of the form \(EXTENSION . PARAMETERS).
193 Where EXTENSION is a symbol and PARAMETERS is a list of strings."
194 `(aset ,connection 3 ,extensions))
196 (defmacro smtp-make-connection (process server service)
197 "Create a new connection structure.
198 PROCESS is an internal subprocess-object. SERVER is name of the host
199 to connect to. SERVICE is name of the service desired."
200 `(vector ,process ,server ,service nil))
202 (defun smtp-connection-opened (connection)
203 "Say whether the CONNECTION to server has been opened."
204 (let ((process (smtp-connection-process connection)))
205 (if (memq (process-status process) '(open run))
208 (defun smtp-close-connection (connection)
209 "Close the CONNECTION to server."
210 (let ((process (smtp-connection-process connection)))
211 (delete-process process)))
213 (defun smtp-make-fqdn ()
214 "Return user's fully qualified domain name."
217 (let ((system-name (system-name)))
220 (concat system-name "." smtp-local-domain))
221 ((string-match "[^.]\\.[^.]" system-name)
224 (error "Cannot generate valid FQDN"))))))
226 (defun smtp-find-connection (buffer)
227 "Find the connection delivering to BUFFER."
228 (let ((entry (assq buffer smtp-connection-alist))
231 (setq connection (nth 1 entry))
232 (if (smtp-connection-opened connection)
234 (setq smtp-connection-alist
235 (delq entry smtp-connection-alist))
239 (autoload 'starttls-open-stream "starttls")
240 (autoload 'starttls-negotiate "starttls"))
242 (defun smtp-open-connection (buffer server service)
243 "Open a SMTP connection for a service to a host.
244 Return a newly allocated connection-object.
245 BUFFER is the buffer to associate with the connection. SERVER is name
246 of the host to connect to. SERVICE is name of the service desired."
249 (funcall smtp-open-connection-function
250 "SMTP" buffer server service)))
253 (setq connection (smtp-make-connection process server service))
254 (set-process-filter process 'smtp-process-filter)
255 (setq smtp-connection-alist
256 (cons (list buffer connection)
257 smtp-connection-alist))
261 (defun smtp-via-smtp (sender recipients buffer)
264 (smtp-send-buffer sender recipients buffer)
268 (make-obsolete 'smtp-via-smtp "It's old API.")
271 (defun smtp-send-buffer (sender recipients buffer)
273 (if (functionp smtp-server)
274 (funcall smtp-server sender recipients)
277 (smtp-make-package sender recipients buffer))
278 (smtp-open-connection-function
279 (if smtp-use-starttls
280 #'starttls-open-stream
281 smtp-open-connection-function)))
285 (format "*trace of SMTP session to %s*" server)))
287 (buffer-disable-undo)
288 (unless (smtp-find-connection (current-buffer))
289 (smtp-open-connection (current-buffer) server smtp-service))
290 (make-local-variable 'smtp-read-point)
291 (setq smtp-read-point (point-min))
292 (funcall smtp-submit-package-function package))))
294 (defun smtp-submit-package (package)
297 (smtp-primitive-greeting package)
299 (smtp-primitive-ehlo package)
301 (smtp-primitive-helo package)))
302 (if smtp-use-starttls
303 (smtp-primitive-starttls package))
305 (smtp-primitive-auth package))
306 (smtp-primitive-mailfrom package)
307 (smtp-primitive-rcptto package)
308 (smtp-primitive-data package))
309 (let ((connection (smtp-find-connection (current-buffer))))
310 (when (smtp-connection-opened connection)
312 (smtp-primitive-quit package)
313 (smtp-close-connection connection)))))
315 ;;; @ hook methods for `smtp-submit-package'
318 (defun smtp-primitive-greeting (package)
320 (smtp-find-connection (current-buffer)))
323 (smtp-connection-process connection))))
324 (if (/= (car response) 220)
325 (smtp-response-error response))))
327 (defun smtp-primitive-ehlo (package)
329 (smtp-find-connection (current-buffer)))
331 (smtp-connection-process connection))
333 (smtp-send-command process (format "EHLO %s" (smtp-make-fqdn)))
334 (setq response (smtp-read-response process))
335 (if (/= (car response) 250)
336 (smtp-response-error response))
337 (smtp-connection-set-extensions
341 (split-string extension)))
343 (car (read-from-string
344 (downcase (car extensions)))))
348 (defun smtp-primitive-helo (package)
350 (smtp-find-connection (current-buffer)))
352 (smtp-connection-process connection))
354 (smtp-send-command process (format "HELO %s" (smtp-make-fqdn)))
355 (setq response (smtp-read-response process))
356 (if (/= (car response) 250)
357 (smtp-response-error response))))
359 (defun smtp-primitive-auth (package)
361 (smtp-find-connection (current-buffer)))
363 (smtp-connection-process connection))
365 (cdr (assq 'auth (smtp-connection-extensions connection))))
367 (or smtp-sasl-mechanisms sasl-mechanisms))
369 (sasl-find-mechanism mechanisms))
375 (error "No authentication mechanism available"))
376 (setq client (sasl-make-client mechanism smtp-sasl-user-name "smtp"
377 (smtp-connection-server connection)))
378 (if smtp-sasl-properties
379 (sasl-client-set-properties client smtp-sasl-properties))
380 (setq name (sasl-mechanism-name mechanism)
381 ;; Retrieve the initial response
382 step (sasl-next-step client nil))
385 (if (sasl-step-data step)
386 (format "AUTH %s %s" name (base64-encode-string (sasl-step-data step) t))
387 (format "AUTH %s" name)))
390 (setq response (smtp-read-response process))
391 (when (= (car response) 235)
392 ;; The authentication process is finished.
393 (setq step (sasl-next-step client step))
396 (smtp-response-error response)) ;Bogus server?
397 (if (/= (car response) 334)
398 (smtp-response-error response))
399 (sasl-step-set-data step (base64-decode-string (nth 1 response)))
400 (setq step (sasl-next-step client step))
402 process (if (sasl-step-data step)
403 (base64-encode-string (sasl-step-data step) t)
406 (defun smtp-primitive-starttls (package)
408 (smtp-find-connection (current-buffer)))
410 (smtp-connection-process connection))
412 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
413 (smtp-send-command process "STARTTLS")
414 (setq response (smtp-read-response process))
415 (if (/= (car response) 220)
416 (smtp-response-error response))
417 (starttls-negotiate process)))
419 (defun smtp-primitive-mailfrom (package)
421 (smtp-find-connection (current-buffer)))
423 (smtp-connection-process connection))
425 (smtp-connection-extensions
428 (smtp-package-sender package))
431 ;; SIZE --- Message Size Declaration (RFC1870)
432 (if (and smtp-use-size
433 (assq 'size extensions))
434 (setq extension (format "SIZE=%d" (smtp-package-buffer-size package))))
435 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
436 (if (and smtp-use-8bitmime
437 (assq '8bitmime extensions))
438 (setq extension (concat extension " BODY=8BITMIME")))
442 (format "MAIL FROM:<%s> %s" sender extension)
443 (format "MAIL FROM:<%s>" sender)))
444 (setq response (smtp-read-response process))
445 (if (/= (car response) 250)
446 (smtp-response-error response))))
448 (defun smtp-primitive-rcptto (package)
450 (smtp-find-connection (current-buffer)))
452 (smtp-connection-process connection))
454 (smtp-package-recipients package))
458 process (format "RCPT TO:<%s>" (pop recipients)))
459 (setq response (smtp-read-response process))
460 (unless (memq (car response) '(250 251))
461 (smtp-response-error response)))))
463 (defun smtp-primitive-data (package)
465 (smtp-find-connection (current-buffer)))
467 (smtp-connection-process connection))
469 (smtp-send-command process "DATA")
470 (setq response (smtp-read-response process))
471 (if (/= (car response) 354)
472 (smtp-response-error response))
474 (set-buffer (smtp-package-buffer package))
475 (goto-char (point-min))
478 process (buffer-substring (point) (progn (end-of-line)(point))))
480 (smtp-send-command process ".")
481 (setq response (smtp-read-response process))
482 (if (/= (car response) 250)
483 (smtp-response-error response))))
485 (defun smtp-primitive-quit (package)
487 (smtp-find-connection (current-buffer)))
489 (smtp-connection-process connection))
491 (smtp-send-command process "QUIT")
492 (setq response (smtp-read-response process))
493 (if (/= (car response) 221)
494 (smtp-response-error response))))
496 ;;; @ low level process manipulating function
498 (defun smtp-process-filter (process output)
500 (set-buffer (process-buffer process))
501 (goto-char (point-max))
504 (put 'smtp-error 'error-message "SMTP error")
505 (put 'smtp-error 'error-conditions '(smtp-error error))
507 (put 'smtp-response-error 'error-message "SMTP response error")
508 (put 'smtp-response-error 'error-conditions '(smtp-response-error smtp-error error))
510 (defun smtp-response-error (response)
511 (signal 'smtp-response-error response))
513 (defun smtp-read-response (process)
514 (let (case-fold-search
515 (response-continue t)
517 (while response-continue
518 (goto-char smtp-read-point)
519 (while (not (search-forward "\r\n" nil t))
520 (accept-process-output process)
521 (goto-char smtp-read-point))
524 (list (buffer-substring
525 (+ 4 smtp-read-point)
528 (prog1 smtp-read-point
529 (setq smtp-read-point (point))))
530 (if (looking-at "[1-5][0-9][0-9] ")
531 (setq response (cons (read (point-marker)) response)
532 response-continue nil)))
535 (defun smtp-send-command (process command)
537 (set-buffer (process-buffer process))
538 (goto-char (point-max))
539 (insert command "\r\n")
540 (setq smtp-read-point (point))
541 (process-send-string process command)
542 (process-send-string process "\r\n")))
544 (defun smtp-send-data (process data)
546 (set-buffer (process-buffer process))
547 (goto-char (point-max))
548 (setq smtp-read-point (point))
549 ;; Escape "." at start of a line.
550 (if (eq (string-to-char data) ?.)
551 (process-send-string process "."))
552 (process-send-string process data)
553 (process-send-string process "\r\n")))
555 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
556 "Get address list suitable for smtp RCPT TO:<address>."
557 (let ((simple-address-list "")
561 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
565 (set-buffer smtp-address-buffer)
566 (setq case-fold-search t)
568 (insert (save-excursion
569 (set-buffer smtp-text-buffer)
570 (buffer-substring-no-properties header-start header-end)))
571 (goto-char (point-min))
572 ;; RESENT-* fields should stop processing of regular fields.
574 (if (re-search-forward "^RESENT-TO:" header-end t)
576 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
577 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
579 (while (re-search-forward addr-regexp header-end t)
581 (setq this-line (match-beginning 0))
583 ;; get any continuation lines.
584 (while (and (looking-at "^[ \t]+") (< (point) header-end))
586 (setq this-line-end (point-marker))
587 (setq simple-address-list
588 (concat simple-address-list " "
589 (mail-strip-quoted-names
590 (buffer-substring this-line this-line-end)))))
593 (insert-string simple-address-list)
596 (subst-char-in-region (point-min) (point-max) 10 ? t)
598 (subst-char-in-region (point-min) (point-max) ?, ? t)
600 (subst-char-in-region (point-min) (point-max) 9 ? t)
602 (goto-char (point-min))
603 ;; tidyness in case hook is not robust when it looks at this
604 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
606 (goto-char (point-min))
607 (let (recipient-address-list)
608 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
610 (setq recipient-address-list
611 (cons (buffer-substring (match-beginning 1) (match-end 1))
612 recipient-address-list)))
613 recipient-address-list))
614 (kill-buffer smtp-address-buffer))))
618 ;;; smtp.el ends here