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-stream (tram-stream)
44 (luna-define-internal-accessors 'smtp-stream))
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-stream-process-internal trans))))
108 (or (smtp-check-response response)
109 (tram-stream-error trans 'greeting))
112 (defun smtp-ehlo (trans)
114 (smtp-stream-process-internal trans)
115 (format "EHLO %s" (smtp-make-fqdn)))
118 (smtp-stream-process-internal trans))))
119 (or (smtp-check-response response)
120 (tram-stream-error trans 'ehlo))
121 (smtp-stream-set-extensions-internal
124 (car (read-from-string (downcase extension))))
128 (defun smtp-helo (trans)
130 (smtp-stream-process-internal trans)
131 (format "HELO %s" (smtp-make-fqdn)))
134 (smtp-stream-process-internal trans))))
135 (or (smtp-check-response response)
136 (tram-stream-error trans 'helo))
139 (defun smtp-mailfrom (sender trans)
141 (smtp-stream-process-internal trans)
142 (format "MAIL FROM:<%s>%s"
144 ;; SIZE --- Message Size Declaration (RFC1870)
146 ;;; (smtp-stream-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-stream-extensions-internal trans))
168 (smtp-stream-process-internal trans))))
169 (or (smtp-check-response response)
170 (tram-stream-error trans 'mailfrom))
173 (defun smtp-rcptto (recipient trans)
176 (smtp-stream-process-internal trans)
178 (if smtp-notify-success
179 "RCPT TO:<%s> NOTIFY=SUCCESS"
184 (smtp-stream-process-internal trans)))
185 (or (smtp-check-response response)
186 (tram-stream-error trans 'rcptto))
189 (defun smtp-data (buffer trans)
191 (smtp-stream-process-internal trans)
195 (smtp-stream-process-internal trans))))
196 (or (smtp-check-response response)
197 (tram-stream-error trans 'data))
201 (smtp-stream-process-internal trans)
205 (smtp-stream-process-internal trans)
209 (smtp-stream-process-internal trans)))
210 (or (smtp-check-response response)
211 (tram-stream-error trans 'data))
214 (defun smtp-default-transaction-compose-function (sender recipients buffer)
215 (tram-compose-transaction
217 (|| smtp-ehlo smtp-helo)
218 ,(closure-partial-call #'smtp-mailfrom sender)
221 (closure-partial-call #'smtp-rcptto recipient))
223 ,(closure-partial-call #'smtp-data buffer))))
225 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
226 (let ((server (if (functionp smtp-server)
227 (funcall smtp-server sender recipients)
229 process response extensions trans error)
233 (format "*trace of SMTP session to %s*" server)))
234 (buffer-disable-undo)
236 (make-local-variable 'smtp-read-point)
237 (setq smtp-read-point (point-min))
240 (funcall smtp-transaction-compose-function
241 sender recipients smtp-text-buffer)))
242 (or (functionp function)
243 (error "Unable to compose SMTP commands"))
244 (if (eq (car-safe function) 'lambda)
245 (setq function (byte-compile function)))
248 (funcall smtp-open-connection-function
249 "SMTP" (current-buffer) server smtp-service)))
251 (set-process-filter process 'smtp-process-filter)
253 (luna-make-entity 'smtp-stream :process process)
255 (catch (tram-stream-error-name trans)
256 (funcall function trans)
260 (memq (process-status process) '(open run)))
262 (smtp-send-command process "QUIT")
263 (delete-process process))))))
265 (defun smtp-process-filter (process output)
267 (set-buffer (process-buffer process))
268 (goto-char (point-max))
271 (defun smtp-read-response (process)
272 (let ((case-fold-search nil)
274 (response-continue t)
276 (while response-continue
277 (goto-char smtp-read-point)
278 (while (not (search-forward "\r\n" nil t))
279 (accept-process-output process)
280 (goto-char smtp-read-point))
281 (setq match-end (point))
284 (list (buffer-substring (+ 4 smtp-read-point)
286 (goto-char smtp-read-point)
287 (when (looking-at "[1-5][0-9][0-9] ")
288 (setq response-continue nil)
289 (push (read (point-marker)) response))
290 (setq smtp-read-point match-end))
293 (defun smtp-check-response (response)
294 (memq (/ (car response) 100) '(2 3)));; XXX
296 (defun smtp-send-command (process command)
297 (goto-char (point-max))
298 (insert command "\r\n")
299 (setq smtp-read-point (point))
300 (process-send-string process command)
301 (process-send-string process "\r\n"))
303 (defun smtp-send-data-1 (process data)
304 (goto-char (point-max))
305 (setq smtp-read-point (point))
306 ;; Escape "." at start of a line.
307 (if (eq (string-to-char data) ?.)
308 (process-send-string process "."))
309 (process-send-string process data)
310 (process-send-string process "\r\n"))
312 (defun smtp-send-data (process buffer)
313 (let ((data-continue t)
320 (goto-char (point-min)))
326 (setq this-line (point))
328 (setq this-line-end (point))
329 (setq sending-data nil)
330 (setq sending-data (buffer-substring this-line this-line-end))
331 (if (or (/= (forward-line 1) 0) (eobp))
332 (setq data-continue nil)))
334 (smtp-send-data-1 process sending-data))))
336 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
337 "Get address list suitable for smtp RCPT TO:<address>."
338 (let ((simple-address-list "")
342 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
346 (set-buffer smtp-address-buffer)
347 (setq case-fold-search t)
349 (insert (save-excursion
350 (set-buffer smtp-text-buffer)
351 (buffer-substring-no-properties header-start header-end)))
352 (goto-char (point-min))
353 ;; RESENT-* fields should stop processing of regular fields.
355 (if (re-search-forward "^RESENT-TO:" header-end t)
357 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
358 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
360 (while (re-search-forward addr-regexp header-end t)
362 (setq this-line (match-beginning 0))
364 ;; get any continuation lines.
365 (while (and (looking-at "^[ \t]+") (< (point) header-end))
367 (setq this-line-end (point-marker))
368 (setq simple-address-list
369 (concat simple-address-list " "
370 (mail-strip-quoted-names
371 (buffer-substring this-line this-line-end)))))
374 (insert-string simple-address-list)
377 (subst-char-in-region (point-min) (point-max) 10 ? t)
379 (subst-char-in-region (point-min) (point-max) ?, ? t)
381 (subst-char-in-region (point-min) (point-max) 9 ? t)
383 (goto-char (point-min))
384 ;; tidyness in case hook is not robust when it looks at this
385 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
387 (goto-char (point-min))
388 (let (recipient-address-list)
389 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
391 (setq recipient-address-list
392 (cons (buffer-substring (match-beginning 1) (match-end 1))
393 recipient-address-list)))
394 recipient-address-list))
395 (kill-buffer smtp-address-buffer))))
399 ;;; smtp.el ends here