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."
60 (defcustom smtp-coding-system 'binary
61 "*Coding-system for SMTP output."
68 (concat (system-name) "." smtp-local-domain)
71 (defun smtp-via-smtp (recipient smtp-text-buffer)
78 (supported-extensions '())
79 (coding-system-for-read smtp-coding-system)
80 (coding-system-for-write smtp-coding-system))
83 ;; get or create the trace buffer
86 (format "*trace of SMTP session to %s*" host)))
88 ;; clear the trace buffer of old output
90 (set-buffer process-buffer)
93 ;; open the connection to the server
94 (setq process (open-network-stream "SMTP" process-buffer host port))
95 (and (null process) (throw 'done nil))
97 ;; set the send-filter
98 (set-process-filter process 'smtp-process-filter)
101 (set-buffer process-buffer)
102 (make-local-variable 'smtp-read-point)
103 (setq smtp-read-point (point-min))
105 (if (or (null (car (setq greeting (smtp-read-response process))))
106 (not (integerp (car greeting)))
107 (>= (car greeting) 400))
112 (smtp-send-command process (format "EHLO %s" (smtp-fqdn)))
114 (if (or (null (car (setq response-code (smtp-read-response process))))
115 (not (integerp (car response-code)))
116 (>= (car response-code) 400))
119 (smtp-send-command process (format "HELO %s" (smtp-fqdn)))
121 (if (or (null (car (setq response-code (smtp-read-response process))))
122 (not (integerp (car response-code)))
123 (>= (car response-code) 400))
125 (let ((extension-lines (cdr (cdr response-code))))
126 (while extension-lines
127 (let ((name (intern (downcase (substring (car extension-lines) 4)))))
129 (cond ((memq name '(verb xvrb 8bitmime onex xone
132 (setq supported-extensions
133 (cons name supported-extensions)))
134 (t (message "unknown extension %s"
136 (setq extension-lines (cdr extension-lines)))))
138 (if (or (member 'onex supported-extensions)
139 (member 'xone supported-extensions))
141 (smtp-send-command process (format "ONEX"))
142 (if (or (null (car (setq response-code (smtp-read-response process))))
143 (not (integerp (car response-code)))
144 (>= (car response-code) 400))
147 (if (and smtp-debug-info
148 (or (member 'verb supported-extensions)
149 (member 'xvrb supported-extensions)))
151 (smtp-send-command process (format "VERB"))
152 (if (or (null (car (setq response-code (smtp-read-response process))))
153 (not (integerp (car response-code)))
154 (>= (car response-code) 400))
157 (if (member 'xusr supported-extensions)
159 (smtp-send-command process (format "XUSR"))
160 (if (or (null (car (setq response-code (smtp-read-response process))))
161 (not (integerp (car response-code)))
162 (>= (car response-code) 400))
165 ;; MAIL FROM: <sender>
167 (if (member 'size supported-extensions)
170 (set-buffer smtp-text-buffer)
172 (+ (- (point-max) (point-min))
173 ;; Add one byte for each change-of-line
174 ;; because or CR-LF representation:
175 (count-lines (point-min) (point-max))
176 ;; For some reason, an empty line is
177 ;; added to the message. Maybe this
178 ;; is a bug, but it can't hurt to add
179 ;; those two bytes anyway:
183 (if (member '8bitmime supported-extensions)
185 ;; Code should be added here that transforms
186 ;; the contents of the message buffer into
187 ;; something the receiving SMTP can handle.
188 ;; For a receiver that supports 8BITMIME, this
189 ;; may mean converting BINARY to BASE64, or
190 ;; adding Content-Transfer-Encoding and the
191 ;; other MIME headers. The code should also
192 ;; return an indication of what encoding the
193 ;; message buffer is now, i.e. ASCII or
199 ; (smtp-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtp-fqdn)))
200 (smtp-send-command process (format "MAIL FROM: <%s>%s%s"
205 (if (or (null (car (setq response-code (smtp-read-response process))))
206 (not (integerp (car response-code)))
207 (>= (car response-code) 400))
211 ;; RCPT TO: <recipient>
213 (while (not (null (nth n recipient)))
214 (smtp-send-command process (format "RCPT TO: <%s>" (nth n recipient)))
217 (setq response-code (smtp-read-response process))
218 (if (or (null (car response-code))
219 (not (integerp (car response-code)))
220 (>= (car response-code) 400))
226 (smtp-send-command process "DATA")
228 (if (or (null (car (setq response-code (smtp-read-response process))))
229 (not (integerp (car response-code)))
230 (>= (car response-code) 400))
235 (smtp-send-data process smtp-text-buffer)
238 (smtp-send-command process ".")
240 (if (or (null (car (setq response-code (smtp-read-response process))))
241 (not (integerp (car response-code)))
242 (>= (car response-code) 400))
247 ; (smtp-send-command process "QUIT")
248 ; (and (null (car (smtp-read-response process)))
253 (set-buffer (process-buffer process))
254 (smtp-send-command process "QUIT")
255 (smtp-read-response process)
257 ; (if (or (null (car (setq response-code (smtp-read-response process))))
258 ; (not (integerp (car response-code)))
259 ; (>= (car response-code) 400))
262 (delete-process process))))))
264 (defun smtp-process-filter (process output)
266 (set-buffer (process-buffer process))
267 (goto-char (point-max))
270 (defun smtp-read-response (process)
271 (let ((case-fold-search nil)
272 (response-strings nil)
273 (response-continue t)
274 (return-value '(nil ()))
277 (while response-continue
278 (goto-char smtp-read-point)
279 (while (not (search-forward "\r\n" nil t))
280 (accept-process-output process)
281 (goto-char smtp-read-point))
283 (setq match-end (point))
284 (setq response-strings
285 (cons (buffer-substring smtp-read-point (- match-end 2))
288 (goto-char smtp-read-point)
289 (if (looking-at "[0-9]+ ")
290 (let ((begin (match-beginning 0))
293 (message "%s" (car response-strings)))
295 (setq smtp-read-point match-end)
297 ;; ignore lines that start with "0"
298 (if (looking-at "0[0-9]+ ")
300 (setq response-continue nil)
303 (buffer-substring begin end))
304 (nreverse response-strings)))))
306 (if (looking-at "[0-9]+-")
307 (progn (if smtp-debug-info
308 (message "%s" (car response-strings)))
309 (setq smtp-read-point match-end)
310 (setq response-continue t))
312 (setq smtp-read-point match-end)
313 (setq response-continue nil)
315 (cons nil (nreverse response-strings)))
318 (setq smtp-read-point match-end)
321 (defun smtp-send-command (process command)
322 (goto-char (point-max))
323 (if (= (aref command 0) ?P)
324 (insert "PASS <omitted>\r\n")
325 (insert command "\r\n"))
326 (setq smtp-read-point (point))
327 (process-send-string process command)
328 (process-send-string process "\r\n"))
330 (defun smtp-send-data-1 (process data)
331 (goto-char (point-max))
334 (insert data "\r\n"))
336 (setq smtp-read-point (point))
337 ;; Escape "." at start of a line
338 (if (eq (string-to-char data) ?.)
339 (process-send-string process "."))
340 (process-send-string process data)
341 (process-send-string process "\r\n")
344 (defun smtp-send-data (process buffer)
353 (goto-char (point-min)))
359 (setq this-line (point))
361 (setq this-line-end (point))
362 (setq sending-data nil)
363 (setq sending-data (buffer-substring this-line this-line-end))
364 (if (or (/= (forward-line 1) 0) (eobp))
365 (setq data-continue nil)))
367 (smtp-send-data-1 process sending-data)
372 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
373 "Get address list suitable for smtp RCPT TO: <address>."
374 (require 'mail-utils) ;; pick up mail-strip-quoted-names
375 (let ((case-fold-search t)
376 (simple-address-list "")
380 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
384 (set-buffer smtp-address-buffer)
386 (insert-buffer-substring smtp-text-buffer
387 header-start header-end)
388 (goto-char (point-min))
389 ;; RESENT-* fields should stop processing of regular fields.
391 (if (re-search-forward "^RESENT-TO:" header-end t)
393 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
394 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
396 (while (re-search-forward addr-regexp header-end t)
398 (setq this-line (match-beginning 0))
400 ;; get any continuation lines
401 (while (and (looking-at "^[ \t]+") (< (point) header-end))
403 (setq this-line-end (point-marker))
404 (setq simple-address-list
405 (concat simple-address-list " "
406 (mail-strip-quoted-names
407 (buffer-substring this-line this-line-end))))
411 (insert-string simple-address-list)
414 (subst-char-in-region (point-min) (point-max) 10 ? t)
416 (subst-char-in-region (point-min) (point-max) ?, ? t)
418 (subst-char-in-region (point-min) (point-max) 9 ? t)
420 (goto-char (point-min))
421 ;; tidyness in case hook is not robust when it looks at this
422 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
424 (goto-char (point-min))
425 (let (recipient-address-list)
426 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
428 (setq recipient-address-list
429 (cons (buffer-substring (match-beginning 1) (match-end 1))
430 recipient-address-list))
432 recipient-address-list)
434 (kill-buffer smtp-address-buffer))
437 (defun smtp-do-bcc (header-end)
438 "Delete BCC: and their continuation lines from the header area.
439 There may be multiple BCC: lines, and each may have arbitrarily
440 many continuation lines."
441 (let ((case-fold-search t))
443 (goto-char (point-min))
444 ;; iterate over all BCC: lines
445 (while (re-search-forward "^BCC:" header-end t)
446 (delete-region (match-beginning 0) (progn (forward-line 1) (point)))
447 ;; get rid of any continuation lines
448 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
457 ;;; smtp.el ends here