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.
29 "SMTP protocol for sending mail."
32 (defcustom smtp-default-server nil
33 "*Specify default SMTP server."
34 :type '(choice (const nil) string)
37 (defcustom smtp-server
38 (or (getenv "SMTPSERVER") smtp-default-server)
39 "*The name of the host running SMTP server."
40 :type '(choice (const nil) string)
43 (defcustom smtp-service 25
44 "*SMTP service port number. smtp or 25 ."
48 (defcustom smtp-local-domain nil
49 "*Local domain name without a host name.
50 If the function (system-name) returns the full internet address,
51 don't define this value."
52 :type '(choice (const nil) string)
55 (defcustom smtp-debug-info nil
56 "*smtp debug info printout. messages and process buffer."
63 (concat (system-name) "." smtp-local-domain)
66 (defun smtp-via-smtp (recipient smtp-text-buffer)
73 (supported-extensions '()))
76 ;; get or create the trace buffer
79 (format "*trace of SMTP session to %s*" host)))
81 ;; clear the trace buffer of old output
83 (set-buffer process-buffer)
86 ;; open the connection to the server
87 (setq process (open-network-stream-as-binary
88 "SMTP" process-buffer host port))
89 (and (null process) (throw 'done nil))
91 ;; set the send-filter
92 (set-process-filter process 'smtp-process-filter)
95 (set-buffer process-buffer)
96 (make-local-variable 'smtp-read-point)
97 (setq smtp-read-point (point-min))
99 (if (or (null (car (setq greeting (smtp-read-response process))))
100 (not (integerp (car greeting)))
101 (>= (car greeting) 400))
106 (smtp-send-command process (format "EHLO %s" (smtp-fqdn)))
108 (if (or (null (car (setq response-code (smtp-read-response process))))
109 (not (integerp (car response-code)))
110 (>= (car response-code) 400))
113 (smtp-send-command process (format "HELO %s" (smtp-fqdn)))
115 (if (or (null (car (setq response-code (smtp-read-response process))))
116 (not (integerp (car response-code)))
117 (>= (car response-code) 400))
119 (let ((extension-lines (cdr (cdr response-code))))
120 (while extension-lines
121 (let ((name (intern (downcase (substring (car extension-lines) 4)))))
123 (cond ((memq name '(verb xvrb 8bitmime onex xone
126 (setq supported-extensions
127 (cons name supported-extensions)))
128 (t (message "unknown extension %s"
130 (setq extension-lines (cdr extension-lines)))))
132 (if (or (member 'onex supported-extensions)
133 (member 'xone supported-extensions))
135 (smtp-send-command process (format "ONEX"))
136 (if (or (null (car (setq response-code (smtp-read-response process))))
137 (not (integerp (car response-code)))
138 (>= (car response-code) 400))
141 (if (and smtp-debug-info
142 (or (member 'verb supported-extensions)
143 (member 'xvrb supported-extensions)))
145 (smtp-send-command process (format "VERB"))
146 (if (or (null (car (setq response-code (smtp-read-response process))))
147 (not (integerp (car response-code)))
148 (>= (car response-code) 400))
151 (if (member 'xusr supported-extensions)
153 (smtp-send-command process (format "XUSR"))
154 (if (or (null (car (setq response-code (smtp-read-response process))))
155 (not (integerp (car response-code)))
156 (>= (car response-code) 400))
159 ;; MAIL FROM: <sender>
161 (if (member 'size supported-extensions)
164 (set-buffer smtp-text-buffer)
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:
177 (if (member '8bitmime supported-extensions)
179 ;; Code should be added here that transforms
180 ;; the contents of the message buffer into
181 ;; something the receiving SMTP can handle.
182 ;; For a receiver that supports 8BITMIME, this
183 ;; may mean converting BINARY to BASE64, or
184 ;; adding Content-Transfer-Encoding and the
185 ;; other MIME headers. The code should also
186 ;; return an indication of what encoding the
187 ;; message buffer is now, i.e. ASCII or
193 ; (smtp-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtp-fqdn)))
194 (smtp-send-command process (format "MAIL FROM: <%s>%s%s"
199 (if (or (null (car (setq response-code (smtp-read-response process))))
200 (not (integerp (car response-code)))
201 (>= (car response-code) 400))
205 ;; RCPT TO: <recipient>
207 (while (not (null (nth n recipient)))
208 (smtp-send-command process (format "RCPT TO: <%s>" (nth n recipient)))
211 (setq response-code (smtp-read-response process))
212 (if (or (null (car response-code))
213 (not (integerp (car response-code)))
214 (>= (car response-code) 400))
220 (smtp-send-command process "DATA")
222 (if (or (null (car (setq response-code (smtp-read-response process))))
223 (not (integerp (car response-code)))
224 (>= (car response-code) 400))
229 (smtp-send-data process smtp-text-buffer)
232 (smtp-send-command process ".")
234 (if (or (null (car (setq response-code (smtp-read-response process))))
235 (not (integerp (car response-code)))
236 (>= (car response-code) 400))
241 ; (smtp-send-command process "QUIT")
242 ; (and (null (car (smtp-read-response process)))
247 (set-buffer (process-buffer process))
248 (smtp-send-command process "QUIT")
249 (smtp-read-response process)
251 ; (if (or (null (car (setq response-code (smtp-read-response process))))
252 ; (not (integerp (car response-code)))
253 ; (>= (car response-code) 400))
256 (delete-process process))))))
258 (defun smtp-process-filter (process output)
260 (set-buffer (process-buffer process))
261 (goto-char (point-max))
264 (defun smtp-read-response (process)
265 (let ((case-fold-search nil)
266 (response-strings nil)
267 (response-continue t)
268 (return-value '(nil ()))
271 (while response-continue
272 (goto-char smtp-read-point)
273 (while (not (search-forward "\r\n" nil t))
274 (accept-process-output process)
275 (goto-char smtp-read-point))
277 (setq match-end (point))
278 (setq response-strings
279 (cons (buffer-substring smtp-read-point (- match-end 2))
282 (goto-char smtp-read-point)
283 (if (looking-at "[0-9]+ ")
284 (let ((begin (match-beginning 0))
287 (message "%s" (car response-strings)))
289 (setq smtp-read-point match-end)
291 ;; ignore lines that start with "0"
292 (if (looking-at "0[0-9]+ ")
294 (setq response-continue nil)
297 (buffer-substring begin end))
298 (nreverse response-strings)))))
300 (if (looking-at "[0-9]+-")
301 (progn (if smtp-debug-info
302 (message "%s" (car response-strings)))
303 (setq smtp-read-point match-end)
304 (setq response-continue t))
306 (setq smtp-read-point match-end)
307 (setq response-continue nil)
309 (cons nil (nreverse response-strings)))
312 (setq smtp-read-point match-end)
315 (defun smtp-send-command (process command)
316 (goto-char (point-max))
317 (if (= (aref command 0) ?P)
318 (insert "PASS <omitted>\r\n")
319 (insert command "\r\n"))
320 (setq smtp-read-point (point))
321 (process-send-string process command)
322 (process-send-string process "\r\n"))
324 (defun smtp-send-data-1 (process data)
325 (goto-char (point-max))
328 (insert data "\r\n"))
330 (setq smtp-read-point (point))
331 ;; Escape "." at start of a line
332 (if (eq (string-to-char data) ?.)
333 (process-send-string process "."))
334 (process-send-string process data)
335 (process-send-string process "\r\n")
338 (defun smtp-send-data (process buffer)
347 (goto-char (point-min)))
353 (setq this-line (point))
355 (setq this-line-end (point))
356 (setq sending-data nil)
357 (setq sending-data (buffer-substring this-line this-line-end))
358 (if (or (/= (forward-line 1) 0) (eobp))
359 (setq data-continue nil)))
361 (smtp-send-data-1 process sending-data)
366 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
367 "Get address list suitable for smtp RCPT TO: <address>."
368 (require 'mail-utils) ;; pick up mail-strip-quoted-names
369 (let ((case-fold-search t)
370 (simple-address-list "")
374 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
378 (set-buffer smtp-address-buffer)
380 (insert-buffer-substring smtp-text-buffer
381 header-start header-end)
382 (goto-char (point-min))
383 ;; RESENT-* fields should stop processing of regular fields.
385 (if (re-search-forward "^RESENT-TO:" header-end t)
387 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
388 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
390 (while (re-search-forward addr-regexp header-end t)
392 (setq this-line (match-beginning 0))
394 ;; get any continuation lines
395 (while (and (looking-at "^[ \t]+") (< (point) header-end))
397 (setq this-line-end (point-marker))
398 (setq simple-address-list
399 (concat simple-address-list " "
400 (mail-strip-quoted-names
401 (buffer-substring this-line this-line-end))))
405 (insert-string simple-address-list)
408 (subst-char-in-region (point-min) (point-max) 10 ? t)
410 (subst-char-in-region (point-min) (point-max) ?, ? t)
412 (subst-char-in-region (point-min) (point-max) 9 ? t)
414 (goto-char (point-min))
415 ;; tidyness in case hook is not robust when it looks at this
416 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
418 (goto-char (point-min))
419 (let (recipient-address-list)
420 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
422 (setq recipient-address-list
423 (cons (buffer-substring (match-beginning 1) (match-end 1))
424 recipient-address-list))
426 recipient-address-list)
428 (kill-buffer smtp-address-buffer))
431 (defun smtp-do-bcc (header-end)
432 "Delete BCC: and their continuation lines from the header area.
433 There may be multiple BCC: lines, and each may have arbitrarily
434 many continuation lines."
435 (let ((case-fold-search t))
437 (goto-char (point-min))
438 ;; iterate over all BCC: lines
439 (while (re-search-forward "^BCC:" header-end t)
440 (delete-region (match-beginning 0) (progn (forward-line 1) (point)))
441 ;; get rid of any continuation lines
442 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
451 ;;; smtp.el ends here