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)
47 (luna-define-internal-accessors 'smtp-transaction))
50 "SMTP protocol for sending mail."
53 (defcustom smtp-default-server nil
54 "Specify default SMTP server."
55 :type '(choice (const nil) string)
58 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
59 "The name of the host running SMTP server. It can also be a function
60 called from `smtp-via-smtp' with arguments SENDER and RECIPIENTS."
61 :type '(choice (string :tag "Name")
62 (function :tag "Function"))
65 (defcustom smtp-service "smtp"
66 "SMTP service port number. \"smtp\" or 25."
67 :type '(choice (integer :tag "25" 25)
68 (string :tag "smtp" "smtp"))
71 (defcustom smtp-use-8bitmime t
72 "If non-nil, use ESMTP 8BITMIME if available."
76 (defcustom smtp-local-domain nil
77 "Local domain name without a host name.
78 If the function (system-name) returns the full internet address,
79 don't define this value."
80 :type '(choice (const nil) string)
83 (defcustom smtp-notify-success nil
84 "If non-nil, notification for successful mail delivery is returned
89 (defvar smtp-open-connection-function (function open-network-stream))
91 (defvar smtp-default-commands
92 '(&& smtp-greeting (|| smtp-ehlo smtp-helo)
93 smtp-mailfrom smtp-rcptto smtp-data))
95 (defvar smtp-commands smtp-default-commands)
97 (defvar smtp-read-point nil)
99 (defvar smtp-transaction-function nil)
101 (defun smtp-make-fqdn ()
102 "Return user's fully qualified domain name."
103 (let ((system-name (system-name)))
106 (concat system-name "." smtp-local-domain))
107 ((string-match "[^.]\\.[^.]" system-name)
110 (error "Cannot generate valid FQDN. Set `smtp-local-domain' correctly.")))))
112 (luna-define-generic smtp-greeting (trans))
113 (luna-define-generic smtp-ehlo (trans))
114 (luna-define-generic smtp-helo (trans))
115 (luna-define-generic smtp-mailfrom (trans))
116 (luna-define-generic smtp-rcptto (trans))
117 (luna-define-generic smtp-data (trans))
119 (luna-define-method smtp-greeting ((trans smtp-transaction))
122 (smtp-transaction-process-internal trans))))
123 (or (smtp-check-response response)
124 (net-transaction-error trans 'greeting))
127 (luna-define-method smtp-ehlo ((trans smtp-transaction))
129 (smtp-transaction-process-internal trans)
130 (format "EHLO %s" (smtp-make-fqdn)))
133 (smtp-transaction-process-internal trans))))
134 (or (smtp-check-response response)
135 (net-transaction-error trans 'ehlo))
136 (smtp-transaction-set-extensions-internal
139 (car (read-from-string (downcase extension))))
143 (luna-define-method smtp-helo ((trans smtp-transaction))
145 (smtp-transaction-process-internal trans)
146 (format "HELO %s" (smtp-make-fqdn)))
149 (smtp-transaction-process-internal trans))))
150 (or (smtp-check-response response)
151 (net-transaction-error trans 'helo))
154 (luna-define-method smtp-mailfrom ((trans smtp-transaction))
156 (smtp-transaction-process-internal trans)
157 (format "MAIL FROM:<%s>%s%s"
158 (smtp-transaction-sender-internal trans)
159 ;; SIZE --- Message Size Declaration (RFC1870)
161 (smtp-transaction-extensions-internal trans))
165 (smtp-transaction-buffer-internal trans))
166 (+ (- (point-max) (point-min))
167 ;; Add one byte for each change-of-line
168 ;; because or CR-LF representation:
169 (count-lines (point-min) (point-max))
170 ;; For some reason, an empty line is
171 ;; added to the message. Maybe this
172 ;; is a bug, but it can't hurt to add
173 ;; those two bytes anyway:
176 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
177 (if (and (memq '8bitmime
178 (smtp-transaction-extensions-internal trans))
184 (smtp-transaction-process-internal trans))))
185 (or (smtp-check-response response)
186 (net-transaction-error trans 'mailfrom))
189 (luna-define-method smtp-rcptto ((trans smtp-transaction))
191 (smtp-transaction-recipients-internal trans))
195 (smtp-transaction-process-internal trans)
197 (if smtp-notify-success
198 "RCPT TO:<%s> NOTIFY=SUCCESS"
203 (smtp-transaction-process-internal trans)))
204 (or (smtp-check-response response)
205 (net-transaction-error trans 'rcptto))
206 (setq recipients (cdr recipients)))
209 (luna-define-method smtp-data ((trans smtp-transaction))
211 (smtp-transaction-process-internal trans)
215 (smtp-transaction-process-internal trans))))
216 (or (smtp-check-response response)
217 (net-transaction-error trans 'data))
221 (smtp-transaction-process-internal trans)
222 (smtp-transaction-buffer-internal trans))
226 (smtp-transaction-process-internal trans)
230 (smtp-transaction-process-internal trans)))
231 (or (smtp-check-response response)
232 (net-transaction-error trans 'data))
235 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
236 (let ((server (if (functionp smtp-server)
237 (funcall smtp-server sender recipients)
239 process response extensions trans error)
243 (format "*trace of SMTP session to %s*" server)))
244 (buffer-disable-undo)
246 (make-local-variable 'smtp-read-point)
247 (setq smtp-read-point (point-min))
248 (make-local-variable 'smtp-transaction-function)
249 (or smtp-transaction-function
250 (let ((function (net-transaction-compose-commands smtp-commands)))
251 (or (functionp function)
252 (error "Unable to compose SMTP commands"))
253 (setq smtp-transaction-function function)))
258 (funcall smtp-open-connection-function
259 "SMTP" (current-buffer) server smtp-service)))
261 (set-process-filter process 'smtp-process-filter)
263 (luna-make-entity 'smtp-transaction
266 :recipients recipients
267 :buffer smtp-text-buffer)
269 (catch (net-transaction-error-name trans)
270 (funcall smtp-transaction-function trans)
274 (memq (process-status process) '(open run)))
276 (smtp-send-command process "QUIT")
277 (delete-process process))))))
279 (defun smtp-process-filter (process output)
281 (set-buffer (process-buffer process))
282 (goto-char (point-max))
285 (defun smtp-read-response (process)
286 (let ((case-fold-search nil)
288 (response-continue t)
290 (while response-continue
291 (goto-char smtp-read-point)
292 (while (not (search-forward "\r\n" nil t))
293 (accept-process-output process)
294 (goto-char smtp-read-point))
295 (setq match-end (point))
298 (list (buffer-substring (+ 4 smtp-read-point)
300 (goto-char smtp-read-point)
301 (when (looking-at "[1-5][0-9][0-9] ")
302 (setq response-continue nil)
303 (push (read (point-marker)) response))
304 (setq smtp-read-point match-end))
307 (defun smtp-check-response (response)
308 (= (/ (car response) 100) 2))
310 (defun smtp-send-command (process command)
311 (goto-char (point-max))
312 (insert command "\r\n")
313 (setq smtp-read-point (point))
314 (process-send-string process command)
315 (process-send-string process "\r\n"))
317 (defun smtp-send-data-1 (process data)
318 (goto-char (point-max))
319 (setq smtp-read-point (point))
320 ;; Escape "." at start of a line.
321 (if (eq (string-to-char data) ?.)
322 (process-send-string process "."))
323 (process-send-string process data)
324 (process-send-string process "\r\n"))
326 (defun smtp-send-data (process buffer)
327 (let ((data-continue t)
334 (goto-char (point-min)))
340 (setq this-line (point))
342 (setq this-line-end (point))
343 (setq sending-data nil)
344 (setq sending-data (buffer-substring this-line this-line-end))
345 (if (or (/= (forward-line 1) 0) (eobp))
346 (setq data-continue nil)))
348 (smtp-send-data-1 process sending-data))))
350 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
351 "Get address list suitable for smtp RCPT TO:<address>."
352 (let ((simple-address-list "")
356 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
360 (set-buffer smtp-address-buffer)
361 (setq case-fold-search t)
363 (insert (save-excursion
364 (set-buffer smtp-text-buffer)
365 (buffer-substring-no-properties header-start header-end)))
366 (goto-char (point-min))
367 ;; RESENT-* fields should stop processing of regular fields.
369 (if (re-search-forward "^RESENT-TO:" header-end t)
371 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
372 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
374 (while (re-search-forward addr-regexp header-end t)
376 (setq this-line (match-beginning 0))
378 ;; get any continuation lines.
379 (while (and (looking-at "^[ \t]+") (< (point) header-end))
381 (setq this-line-end (point-marker))
382 (setq simple-address-list
383 (concat simple-address-list " "
384 (mail-strip-quoted-names
385 (buffer-substring this-line this-line-end)))))
388 (insert-string simple-address-list)
391 (subst-char-in-region (point-min) (point-max) 10 ? t)
393 (subst-char-in-region (point-min) (point-max) ?, ? t)
395 (subst-char-in-region (point-min) (point-max) 9 ? t)
397 (goto-char (point-min))
398 ;; tidyness in case hook is not robust when it looks at this
399 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
401 (goto-char (point-min))
402 (let (recipient-address-list)
403 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
405 (setq recipient-address-list
406 (cons (buffer-substring (match-beginning 1) (match-end 1))
407 recipient-address-list)))
408 recipient-address-list))
409 (kill-buffer smtp-address-buffer))))
413 ;;; smtp.el ends here