1 ;;; smtp.el --- basic functions to send mail with SMTP server
3 ;; Copyright (C) 1995, 1996, 1998 Free Software Foundation, Inc.
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
6 ;; ESMTP support: Simon Leinen <simon@switch.ch>
7 ;; Keywords: SMTP, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 (require 'mail-utils) ; pick up mail-strip-quoted-names
31 "SMTP protocol for sending mail."
34 (defcustom smtp-default-server nil
35 "*Specify default SMTP server."
36 :type '(choice (const nil) string)
39 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
40 "*The name of the host running SMTP server."
41 :type '(choice (const nil) string)
44 (defcustom smtp-service "smtp"
45 "*SMTP service port number. \"smtp\" or 25."
46 :type '(choice (integer :tag "25" 25)
47 (string :tag "smtp" "smtp"))
50 (defcustom smtp-use-8bitmime t
51 "*If non-nil, use ESMTP 8BITMIME if available."
55 (defcustom smtp-local-domain nil
56 "*Local domain name without a host name.
57 If the function (system-name) returns the full internet address,
58 don't define this value."
59 :type '(choice (const nil) string)
62 (defcustom smtp-coding-system 'binary
63 "*Coding-system for SMTP output."
67 (defvar smtp-debug-info nil)
68 (defvar smtp-read-point nil)
70 (defun smtp-make-fqdn ()
71 "Return user's fully qualified domain name."
72 (let ((system-name (system-name)))
75 (concat system-name "." smtp-local-domain))
76 ((string-match "[^.]\\.[^.]" system-name)
79 (error "Cannot generate valid FQDN. Set `smtp-local-domain' correctly.")))))
81 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
82 (let ((coding-system-for-read smtp-coding-system)
83 (coding-system-for-write smtp-coding-system)
84 process response extensions)
88 (format "*trace of SMTP session to %s*" smtp-server)))
90 (make-local-variable 'smtp-read-point)
91 (setq smtp-read-point (point-min))
95 (setq process (open-network-stream "SMTP"
97 smtp-server smtp-service))
98 (or process (throw 'done nil))
100 (set-process-filter process 'smtp-process-filter)
103 (setq response (smtp-read-response process))
104 (if (or (null (car response))
105 (not (integerp (car response)))
106 (>= (car response) 400))
107 (throw 'done (car (cdr response))))
110 (smtp-send-command process
111 (format "EHLO %s" (smtp-make-fqdn)))
112 (setq response (smtp-read-response process))
113 (if (or (null (car response))
114 (not (integerp (car response)))
115 (>= (car response) 400))
118 (smtp-send-command process
119 (format "HELO %s" (smtp-make-fqdn)))
120 (setq response (smtp-read-response process))
121 (if (or (null (car response))
122 (not (integerp (car response)))
123 (>= (car response) 400))
124 (throw 'done (car (cdr response)))))
125 (let ((extension-lines (cdr (cdr response))))
126 (while extension-lines
127 (push (intern (downcase (substring (car extension-lines) 4)))
129 (setq extension-lines (cdr extension-lines)))))
131 ;; ONEX --- One message transaction only (sendmail extension?)
132 (if (or (memq 'onex extensions)
133 (memq 'xone extensions))
135 (smtp-send-command process "ONEX")
136 (setq response (smtp-read-response process))
137 (if (or (null (car response))
138 (not (integerp (car response)))
139 (>= (car response) 400))
140 (throw 'done (car (cdr response))))))
142 ;; VERB --- Verbose (sendmail extension?)
143 (if (and smtp-debug-info
144 (or (memq 'verb extensions)
145 (memq 'xvrb extensions)))
147 (smtp-send-command process "VERB")
148 (setq response (smtp-read-response process))
149 (if (or (null (car response))
150 (not (integerp (car response)))
151 (>= (car response) 400))
152 (throw 'done (car (cdr response))))))
154 ;; XUSR --- Initial (user) submission (sendmail extension?)
155 (if (memq 'xusr extensions)
157 (smtp-send-command process "XUSR")
158 (setq response (smtp-read-response process))
159 (if (or (null (car response))
160 (not (integerp (car response)))
161 (>= (car response) 400))
162 (throw 'done (car (cdr response))))))
164 ;; MAIL FROM:<sender>
167 (format "MAIL FROM:<%s>%s%s"
169 ;; SIZE --- Message Size Declaration (RFC1870)
170 (if (memq 'size extensions)
173 (set-buffer smtp-text-buffer)
174 (+ (- (point-max) (point-min))
175 ;; Add one byte for each change-of-line
176 ;; because or CR-LF representation:
177 (count-lines (point-min) (point-max))
178 ;; For some reason, an empty line is
179 ;; added to the message. Maybe this
180 ;; is a bug, but it can't hurt to add
181 ;; those two bytes anyway:
184 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
185 (if (and (memq '8bitmime extensions)
189 (setq response (smtp-read-response process))
190 (if (or (null (car response))
191 (not (integerp (car response)))
192 (>= (car response) 400))
193 (throw 'done (car (cdr response))))
195 ;; RCPT TO:<recipient>
197 (smtp-send-command process
198 (format "RCPT TO:<%s>" (car recipients)))
199 (setq recipients (cdr recipients))
200 (setq response (smtp-read-response process))
201 (if (or (null (car response))
202 (not (integerp (car response)))
203 (>= (car response) 400))
204 (throw 'done (car (cdr response)))))
207 (smtp-send-command process "DATA")
208 (setq response (smtp-read-response process))
209 (if (or (null (car response))
210 (not (integerp (car response)))
211 (>= (car response) 400))
212 (throw 'done (car (cdr response))))
215 (smtp-send-data process smtp-text-buffer)
218 (smtp-send-command process ".")
219 (setq response (smtp-read-response process))
220 (if (or (null (car response))
221 (not (integerp (car response)))
222 (>= (car response) 400))
223 (throw 'done (car (cdr response))))
228 (eq (process-status process) 'open))
231 (smtp-send-command process "QUIT")
232 (smtp-read-response process)
233 (delete-process process)))))))
235 (defun smtp-process-filter (process output)
237 (set-buffer (process-buffer process))
238 (goto-char (point-max))
241 (defun smtp-read-response (process)
242 (let ((case-fold-search nil)
243 (response-strings nil)
244 (response-continue t)
245 (return-value '(nil ()))
248 (while response-continue
249 (goto-char smtp-read-point)
250 (while (not (search-forward "\r\n" nil t))
251 (accept-process-output process)
252 (goto-char smtp-read-point))
254 (setq match-end (point))
255 (setq response-strings
256 (cons (buffer-substring smtp-read-point (- match-end 2))
259 (goto-char smtp-read-point)
260 (if (looking-at "[0-9]+ ")
261 (let ((begin (match-beginning 0))
264 (message "%s" (car response-strings)))
266 (setq smtp-read-point match-end)
268 ;; ignore lines that start with "0"
269 (if (looking-at "0[0-9]+ ")
271 (setq response-continue nil)
274 (buffer-substring begin end))
275 (nreverse response-strings)))))
277 (if (looking-at "[0-9]+-")
278 (progn (if smtp-debug-info
279 (message "%s" (car response-strings)))
280 (setq smtp-read-point match-end)
281 (setq response-continue t))
283 (setq smtp-read-point match-end)
284 (setq response-continue nil)
286 (cons nil (nreverse response-strings)))))))
287 (setq smtp-read-point match-end)
290 (defun smtp-send-command (process command)
291 (goto-char (point-max))
292 (insert command "\r\n")
293 (setq smtp-read-point (point))
294 (process-send-string process command)
295 (process-send-string process "\r\n"))
297 (defun smtp-send-data-1 (process data)
298 (goto-char (point-max))
300 (insert data "\r\n"))
301 (setq smtp-read-point (point))
302 ;; Escape "." at start of a line.
303 (if (eq (string-to-char data) ?.)
304 (process-send-string process "."))
305 (process-send-string process data)
306 (process-send-string process "\r\n"))
308 (defun smtp-send-data (process buffer)
309 (let ((data-continue t)
316 (goto-char (point-min)))
322 (setq this-line (point))
324 (setq this-line-end (point))
325 (setq sending-data nil)
326 (setq sending-data (buffer-substring this-line this-line-end))
327 (if (or (/= (forward-line 1) 0) (eobp))
328 (setq data-continue nil)))
330 (smtp-send-data-1 process sending-data))))
332 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
333 "Get address list suitable for smtp RCPT TO:<address>."
334 (let ((case-fold-search t)
335 (simple-address-list "")
339 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
343 (set-buffer smtp-address-buffer)
345 (insert (save-excursion
346 (set-buffer smtp-text-buffer)
347 (buffer-substring-no-properties header-start header-end)))
348 (goto-char (point-min))
349 ;; RESENT-* fields should stop processing of regular fields.
351 (if (re-search-forward "^RESENT-TO:" header-end t)
353 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
354 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
356 (while (re-search-forward addr-regexp header-end t)
358 (setq this-line (match-beginning 0))
360 ;; get any continuation lines.
361 (while (and (looking-at "^[ \t]+") (< (point) header-end))
363 (setq this-line-end (point-marker))
364 (setq simple-address-list
365 (concat simple-address-list " "
366 (mail-strip-quoted-names
367 (buffer-substring this-line this-line-end)))))
370 (insert-string simple-address-list)
373 (subst-char-in-region (point-min) (point-max) 10 ? t)
375 (subst-char-in-region (point-min) (point-max) ?, ? t)
377 (subst-char-in-region (point-min) (point-max) 9 ? t)
379 (goto-char (point-min))
380 ;; tidyness in case hook is not robust when it looks at this
381 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
383 (goto-char (point-min))
384 (let (recipient-address-list)
385 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
387 (setq recipient-address-list
388 (cons (buffer-substring (match-beginning 1) (match-end 1))
389 recipient-address-list)))
390 recipient-address-list))
391 (kill-buffer smtp-address-buffer))))
395 ;;; smtp.el ends here