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)
311 (smtp-primitive-quit package)
312 (smtp-close-connection connection)))))
314 ;;; @ hook methods for `smtp-submit-package'
317 (defun smtp-primitive-greeting (package)
319 (smtp-find-connection (current-buffer)))
322 (smtp-connection-process connection))))
323 (if (/= (car response) 220)
324 (smtp-response-error response))))
326 (defun smtp-primitive-ehlo (package)
328 (smtp-find-connection (current-buffer)))
330 (smtp-connection-process connection))
332 (smtp-send-command process (format "EHLO %s" (smtp-make-fqdn)))
333 (setq response (smtp-read-response process))
334 (if (/= (car response) 250)
335 (smtp-response-error response))
336 (smtp-connection-set-extensions
340 (split-string extension)))
342 (car (read-from-string
343 (downcase (car extensions)))))
347 (defun smtp-primitive-helo (package)
349 (smtp-find-connection (current-buffer)))
351 (smtp-connection-process connection))
353 (smtp-send-command process (format "HELO %s" (smtp-make-fqdn)))
354 (setq response (smtp-read-response process))
355 (if (/= (car response) 250)
356 (smtp-response-error response))))
358 (defun smtp-primitive-auth (package)
360 (smtp-find-connection (current-buffer)))
362 (smtp-connection-process connection))
364 (cdr (assq 'auth (smtp-connection-extensions connection))))
366 (or smtp-sasl-mechanisms sasl-mechanisms))
368 (sasl-find-mechanism mechanisms))
374 (error "No authentication mechanism available"))
375 (setq client (sasl-make-client mechanism smtp-sasl-user-name "smtp"
376 (smtp-connection-server connection)))
377 (if smtp-sasl-properties
378 (sasl-client-set-properties client smtp-sasl-properties))
379 (setq name (sasl-mechanism-name mechanism)
380 ;; Retrieve the initial response
381 step (sasl-next-step client nil))
384 (if (sasl-step-data step)
385 (format "AUTH %s %s" name (base64-encode-string (sasl-step-data step) t))
386 (format "AUTH %s" name)))
389 (setq response (smtp-read-response process))
390 (when (= (car response) 235)
391 ;; The authentication process is finished.
392 (setq step (sasl-next-step client step))
395 (smtp-response-error response)) ;Bogus server?
396 (if (/= (car response) 334)
397 (smtp-response-error response))
398 (sasl-step-set-data step (base64-decode-string (nth 1 response)))
399 (setq step (sasl-next-step client step))
401 process (if (sasl-step-data step)
402 (base64-encode-string (sasl-step-data step) t)
405 (defun smtp-primitive-starttls (package)
407 (smtp-find-connection (current-buffer)))
409 (smtp-connection-process connection))
411 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
412 (smtp-send-command process "STARTTLS")
413 (setq response (smtp-read-response process))
414 (if (/= (car response) 220)
415 (smtp-response-error response))
416 (starttls-negotiate process)))
418 (defun smtp-primitive-mailfrom (package)
420 (smtp-find-connection (current-buffer)))
422 (smtp-connection-process connection))
424 (smtp-connection-extensions
427 (smtp-package-sender package))
430 ;; SIZE --- Message Size Declaration (RFC1870)
431 (if (and smtp-use-size
432 (assq 'size extensions))
433 (setq extension (format "SIZE=%d" (smtp-package-buffer-size package))))
434 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
435 (if (and smtp-use-8bitmime
436 (assq '8bitmime extensions))
437 (setq extension (concat extension " BODY=8BITMIME")))
441 (format "MAIL FROM:<%s> %s" sender extension)
442 (format "MAIL FROM:<%s>" sender)))
443 (setq response (smtp-read-response process))
444 (if (/= (car response) 250)
445 (smtp-response-error response))))
447 (defun smtp-primitive-rcptto (package)
449 (smtp-find-connection (current-buffer)))
451 (smtp-connection-process connection))
453 (smtp-package-recipients package))
457 process (format "RCPT TO:<%s>" (pop recipients)))
458 (setq response (smtp-read-response process))
459 (unless (memq (car response) '(250 251))
460 (smtp-response-error response)))))
462 (defun smtp-primitive-data (package)
464 (smtp-find-connection (current-buffer)))
466 (smtp-connection-process connection))
468 (smtp-send-command process "DATA")
469 (setq response (smtp-read-response process))
470 (if (/= (car response) 354)
471 (smtp-response-error response))
473 (set-buffer (smtp-package-buffer package))
474 (goto-char (point-min))
477 process (buffer-substring (point) (progn (end-of-line)(point))))
478 (beginning-of-line 2)))
479 (smtp-send-command process ".")
480 (setq response (smtp-read-response process))
481 (if (/= (car response) 250)
482 (smtp-response-error response))))
484 (defun smtp-primitive-quit (package)
486 (smtp-find-connection (current-buffer)))
488 (smtp-connection-process connection))
490 (smtp-send-command process "QUIT")
491 (setq response (smtp-read-response process))
492 (if (/= (car response) 221)
493 (smtp-response-error response))))
495 ;;; @ low level process manipulating function
497 (defun smtp-process-filter (process output)
499 (set-buffer (process-buffer process))
500 (goto-char (point-max))
503 (put 'smtp-error 'error-message "SMTP error")
504 (put 'smtp-error 'error-conditions '(smtp-error error))
506 (put 'smtp-response-error 'error-message "SMTP response error")
507 (put 'smtp-response-error 'error-conditions '(smtp-response-error smtp-error error))
509 (defun smtp-response-error (response)
510 (signal 'smtp-response-error response))
512 (defun smtp-read-response (process)
513 (let ((response-continue t)
515 (while response-continue
516 (goto-char smtp-read-point)
517 (while (not (search-forward "\r\n" nil t))
518 (accept-process-output process)
519 (goto-char smtp-read-point))
522 (list (buffer-substring
523 (+ 4 smtp-read-point)
526 (prog1 smtp-read-point
527 (setq smtp-read-point (point))))
528 (if (looking-at "[1-5][0-9][0-9] ")
529 (setq response (cons (read (point-marker)) response)
530 response-continue nil)))
533 (defun smtp-send-command (process command)
535 (set-buffer (process-buffer process))
536 (goto-char (point-max))
537 (insert command "\r\n")
538 (setq smtp-read-point (point))
539 (process-send-string process command)
540 (process-send-string process "\r\n")))
542 (defun smtp-send-data (process data)
543 ;; Escape "." at start of a line.
544 (if (eq (string-to-char data) ?.)
545 (process-send-string process "."))
546 (process-send-string process data)
547 (process-send-string process "\r\n"))
549 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
550 "Get address list suitable for smtp RCPT TO:<address>."
551 (let ((simple-address-list "")
555 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
559 (set-buffer smtp-address-buffer)
560 (setq case-fold-search t)
562 (insert (save-excursion
563 (set-buffer smtp-text-buffer)
564 (buffer-substring-no-properties header-start header-end)))
565 (goto-char (point-min))
566 ;; RESENT-* fields should stop processing of regular fields.
568 (if (re-search-forward "^RESENT-TO:" header-end t)
570 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
571 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
573 (while (re-search-forward addr-regexp header-end t)
575 (setq this-line (match-beginning 0))
577 ;; get any continuation lines.
578 (while (and (looking-at "^[ \t]+") (< (point) header-end))
580 (setq this-line-end (point-marker))
581 (setq simple-address-list
582 (concat simple-address-list " "
583 (mail-strip-quoted-names
584 (buffer-substring this-line this-line-end)))))
587 (insert-string simple-address-list)
590 (subst-char-in-region (point-min) (point-max) 10 ? t)
592 (subst-char-in-region (point-min) (point-max) ?, ? t)
594 (subst-char-in-region (point-min) (point-max) 9 ? t)
596 (goto-char (point-min))
597 ;; tidyness in case hook is not robust when it looks at this
598 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
600 (goto-char (point-min))
601 (let (recipient-address-list)
602 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
604 (setq recipient-address-list
605 (cons (buffer-substring (match-beginning 1) (match-end 1))
606 recipient-address-list)))
607 recipient-address-list))
608 (kill-buffer smtp-address-buffer))))
612 ;;; smtp.el ends here