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.
33 (require 'mail-utils) ; mail-strip-quoted-names
35 (eval-when-compile (require 'cl)) ; push
40 (luna-define-class smtp-transaction (net-transaction)
44 (luna-define-internal-accessors 'smtp-transaction))
47 "SMTP protocol for sending mail."
50 (defcustom smtp-default-server nil
51 "Specify default SMTP server."
52 :type '(choice (const nil) string)
55 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
56 "The name of the host running SMTP server. It can also be a function
57 called from `smtp-via-smtp' with arguments SENDER and RECIPIENTS."
58 :type '(choice (string :tag "Name")
59 (function :tag "Function"))
62 (defcustom smtp-service "smtp"
63 "SMTP service port number. \"smtp\" or 25."
64 :type '(choice (integer :tag "25" 25)
65 (string :tag "smtp" "smtp"))
68 (defcustom smtp-use-8bitmime t
69 "If non-nil, use ESMTP 8BITMIME if available."
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-notify-success nil
81 "If non-nil, notification for successful mail delivery is returned
86 (defvar smtp-transaction-compose-function
87 #'smtp-default-transaction-compose-function)
89 (defvar smtp-open-connection-function (function open-network-stream))
91 (defvar smtp-read-point nil)
93 (defun smtp-make-fqdn ()
94 "Return user's fully qualified domain name."
95 (let ((system-name (system-name)))
98 (concat system-name "." smtp-local-domain))
99 ((string-match "[^.]\\.[^.]" system-name)
102 (error "Cannot generate valid FQDN. Set `smtp-local-domain' correctly.")))))
104 (defun smtp-greeting (trans)
107 (smtp-transaction-process-internal trans))))
108 (or (smtp-check-response response)
109 (net-transaction-error trans 'greeting))
112 (defun smtp-ehlo (trans)
114 (smtp-transaction-process-internal trans)
115 (format "EHLO %s" (smtp-make-fqdn)))
118 (smtp-transaction-process-internal trans))))
119 (or (smtp-check-response response)
120 (net-transaction-error trans 'ehlo))
121 (smtp-transaction-set-extensions-internal
124 (car (read-from-string (downcase extension))))
128 (defun smtp-helo (trans)
130 (smtp-transaction-process-internal trans)
131 (format "HELO %s" (smtp-make-fqdn)))
134 (smtp-transaction-process-internal trans))))
135 (or (smtp-check-response response)
136 (net-transaction-error trans 'helo))
139 (defun smtp-mailfrom (sender trans)
141 (smtp-transaction-process-internal trans)
142 (format "MAIL FROM:<%s>%s"
144 ;; SIZE --- Message Size Declaration (RFC1870)
146 ;;; (smtp-transaction-extensions-internal trans))
147 ;;; (format " SIZE=%d"
149 ;;; (set-buffer buffer)
150 ;;; (+ (- (point-max) (point-min))
151 ;;; ;; Add one byte for each change-of-line
152 ;;; ;; because or CR-LF representation:
153 ;;; (count-lines (point-min) (point-max))
154 ;;; ;; For some reason, an empty line is
155 ;;; ;; added to the message. Maybe this
156 ;;; ;; is a bug, but it can't hurt to add
157 ;;; ;; those two bytes anyway:
160 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
161 (if (and (memq '8bitmime
162 (smtp-transaction-extensions-internal trans))
168 (smtp-transaction-process-internal trans))))
169 (or (smtp-check-response response)
170 (net-transaction-error trans 'mailfrom))
173 (defun smtp-rcptto (recipient trans)
176 (smtp-transaction-process-internal trans)
178 (if smtp-notify-success
179 "RCPT TO:<%s> NOTIFY=SUCCESS"
184 (smtp-transaction-process-internal trans)))
185 (or (smtp-check-response response)
186 (net-transaction-error trans 'rcptto))
189 (defun smtp-data (buffer trans)
191 (smtp-transaction-process-internal trans)
195 (smtp-transaction-process-internal trans))))
196 (or (smtp-check-response response)
197 (net-transaction-error trans 'data))
201 (smtp-transaction-process-internal trans)
205 (smtp-transaction-process-internal trans)
209 (smtp-transaction-process-internal trans)))
210 (or (smtp-check-response response)
211 (net-transaction-error trans 'data))
214 (defun smtp-closure-partial-apply (function &rest args)
215 `(lambda (trans) (funcall #',function ,@args trans)))
217 (defun smtp-default-transaction-compose-function (sender recipients buffer)
218 (net-transaction-compose-&&
219 (net-transaction-compose-&&
220 (net-transaction-compose-&&
221 (net-transaction-compose-&&
223 (net-transaction-compose-|| #'smtp-ehlo #'smtp-helo))
224 (smtp-closure-partial-apply #'smtp-mailfrom sender))
225 (net-transaction-fold-left
226 (lambda (accu recipient)
227 (net-transaction-compose-&&
228 accu (smtp-closure-partial-apply #'smtp-rcptto recipient)))
229 #'identity recipients))
230 (smtp-closure-partial-apply #'smtp-data buffer)))
232 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
233 (let ((server (if (functionp smtp-server)
234 (funcall smtp-server sender recipients)
236 process response extensions trans error)
240 (format "*trace of SMTP session to %s*" server)))
241 (buffer-disable-undo)
243 (make-local-variable 'smtp-read-point)
244 (setq smtp-read-point (point-min))
247 (funcall smtp-transaction-compose-function
248 sender recipients smtp-text-buffer)))
249 (or (functionp function)
250 (error "Unable to compose SMTP commands"))
251 (if (and (listp function) (eq (car function) 'lambda))
252 (setq function (byte-compile function)));; XXX
255 (funcall smtp-open-connection-function
256 "SMTP" (current-buffer) server smtp-service)))
258 (set-process-filter process 'smtp-process-filter)
260 (luna-make-entity 'smtp-transaction
263 (catch (net-transaction-error-name trans)
264 (funcall function trans)
268 (memq (process-status process) '(open run)))
270 (smtp-send-command process "QUIT")
271 (delete-process process))))))
273 (defun smtp-process-filter (process output)
275 (set-buffer (process-buffer process))
276 (goto-char (point-max))
279 (defun smtp-read-response (process)
280 (let ((case-fold-search nil)
282 (response-continue t)
284 (while response-continue
285 (goto-char smtp-read-point)
286 (while (not (search-forward "\r\n" nil t))
287 (accept-process-output process)
288 (goto-char smtp-read-point))
289 (setq match-end (point))
292 (list (buffer-substring (+ 4 smtp-read-point)
294 (goto-char smtp-read-point)
295 (when (looking-at "[1-5][0-9][0-9] ")
296 (setq response-continue nil)
297 (push (read (point-marker)) response))
298 (setq smtp-read-point match-end))
301 (defun smtp-check-response (response)
302 (memq (/ (car response) 100) '(2 3)));; XXX
304 (defun smtp-send-command (process command)
305 (goto-char (point-max))
306 (insert command "\r\n")
307 (setq smtp-read-point (point))
308 (process-send-string process command)
309 (process-send-string process "\r\n"))
311 (defun smtp-send-data-1 (process data)
312 (goto-char (point-max))
313 (setq smtp-read-point (point))
314 ;; Escape "." at start of a line.
315 (if (eq (string-to-char data) ?.)
316 (process-send-string process "."))
317 (process-send-string process data)
318 (process-send-string process "\r\n"))
320 (defun smtp-send-data (process buffer)
321 (let ((data-continue t)
328 (goto-char (point-min)))
334 (setq this-line (point))
336 (setq this-line-end (point))
337 (setq sending-data nil)
338 (setq sending-data (buffer-substring this-line this-line-end))
339 (if (or (/= (forward-line 1) 0) (eobp))
340 (setq data-continue nil)))
342 (smtp-send-data-1 process sending-data))))
344 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
345 "Get address list suitable for smtp RCPT TO:<address>."
346 (let ((simple-address-list "")
350 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
354 (set-buffer smtp-address-buffer)
355 (setq case-fold-search t)
357 (insert (save-excursion
358 (set-buffer smtp-text-buffer)
359 (buffer-substring-no-properties header-start header-end)))
360 (goto-char (point-min))
361 ;; RESENT-* fields should stop processing of regular fields.
363 (if (re-search-forward "^RESENT-TO:" header-end t)
365 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
366 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
368 (while (re-search-forward addr-regexp header-end t)
370 (setq this-line (match-beginning 0))
372 ;; get any continuation lines.
373 (while (and (looking-at "^[ \t]+") (< (point) header-end))
375 (setq this-line-end (point-marker))
376 (setq simple-address-list
377 (concat simple-address-list " "
378 (mail-strip-quoted-names
379 (buffer-substring this-line this-line-end)))))
382 (insert-string simple-address-list)
385 (subst-char-in-region (point-min) (point-max) 10 ? t)
387 (subst-char-in-region (point-min) (point-max) ?, ? t)
389 (subst-char-in-region (point-min) (point-max) 9 ? t)
391 (goto-char (point-min))
392 ;; tidyness in case hook is not robust when it looks at this
393 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
395 (goto-char (point-min))
396 (let (recipient-address-list)
397 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
399 (setq recipient-address-list
400 (cons (buffer-substring (match-beginning 1) (match-end 1))
401 recipient-address-list)))
402 recipient-address-list))
403 (kill-buffer smtp-address-buffer))))
407 ;;; smtp.el ends here