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 ;; Kenichi OKADA <okada@opaopa.org> (SASL support)
9 ;; Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
10 ;; Maintainer: Kenichi OKADA <okada@opaopa.org>
11 ;; Keywords: SMTP, mail, SASL
13 ;; This file is part of FLIM (Faithful Library about Internet Message).
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2, or (at
18 ;; your option) any later version.
20 ;; This program is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
35 (require 'mail-utils) ; mail-strip-quoted-names
38 (autoload 'starttls-open-stream "starttls")
39 (autoload 'starttls-negotiate "starttls")
40 (autoload 'sasl-cram-md5 "sasl")
41 (autoload 'sasl-plain "sasl"))
43 (eval-when-compile (require 'cl)) ; push
46 "SMTP protocol for sending mail."
49 (defcustom smtp-default-server nil
50 "*Specify default SMTP server."
51 :type '(choice (const nil) string)
54 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
55 "*The name of the host running SMTP server. It can also be a function
56 called from `smtp-via-smtp' with arguments SENDER and RECIPIENTS."
57 :type '(choice (string :tag "Name")
58 (function :tag "Function"))
61 (defcustom smtp-service "smtp"
62 "*SMTP service port number. \"smtp\" or 25."
63 :type '(choice (integer :tag "25" 25)
64 (string :tag "smtp" "smtp"))
67 (defcustom smtp-use-8bitmime t
68 "*If non-nil, use ESMTP 8BITMIME if available."
72 (defcustom smtp-local-domain nil
73 "*Local domain name without a host name.
74 If the function (system-name) returns the full internet address,
75 don't define this value."
76 :type '(choice (const nil) string)
79 (defcustom smtp-debug-info nil
80 "*smtp debug info printout. messages and process buffer."
84 (defcustom smtp-notify-success nil
85 "*If non-nil, notification for successful mail delivery is returned
90 (defcustom smtp-authentication-type nil
91 "*SMTP authentication mechanism (RFC2554)."
95 (defvar smtp-authentication-user nil)
96 (defvar smtp-authentication-passphrase nil)
98 (defvar smtp-authentication-method-alist
99 '((cram-md5 smtp-auth-cram-md5)
100 (plain smtp-auth-plain)
101 (login smtp-auth-login)
104 (defcustom smtp-connection-type nil
105 "*SMTP connection type."
106 :type '(choice (const nil) (const :tag "TLS" starttls))
109 (defvar smtp-read-point nil)
111 (defun smtp-make-fqdn ()
112 "Return user's fully qualified domain name."
113 (let ((system-name (system-name)))
116 (concat system-name "." smtp-local-domain))
117 ((string-match "[^.]\\.[^.]" system-name)
120 (error "Cannot generate valid FQDN. Set `smtp-local-domain' correctly.")))))
122 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
123 (let ((server (if (functionp smtp-server)
124 (funcall smtp-server sender recipients)
126 process response extensions)
130 (format "*trace of SMTP session to %s*" server)))
132 (make-local-variable 'smtp-read-point)
133 (setq smtp-read-point (point-min))
138 (if smtp-connection-type
139 (starttls-open-stream
140 "SMTP" (current-buffer) server smtp-service)
141 (open-network-stream-as-binary
142 "SMTP" (current-buffer) server smtp-service)))
143 (or process (throw 'done nil))
145 (set-process-filter process 'smtp-process-filter)
147 (if (eq smtp-connection-type 'force)
148 (starttls-negotiate process))
151 (setq response (smtp-read-response process))
152 (if (or (null (car response))
153 (not (integerp (car response)))
154 (>= (car response) 400))
155 (throw 'done (car (cdr response))))
158 (smtp-send-command process
159 (format "EHLO %s" (smtp-make-fqdn)))
160 (setq response (smtp-read-response process))
161 (if (or (null (car response))
162 (not (integerp (car response)))
163 (>= (car response) 400))
166 (smtp-send-command process
167 (format "HELO %s" (smtp-make-fqdn)))
168 (setq response (smtp-read-response process))
169 (if (or (null (car response))
170 (not (integerp (car response)))
171 (>= (car response) 400))
172 (throw 'done (car (cdr response)))))
173 (let ((extension-lines (cdr (cdr response)))
175 (while extension-lines
179 (downcase (substring (car extension-lines) 4))))
180 (while (string-match "\\([^ ]+\\)" extension (match-end 1))
181 (push (intern (match-string 1 extension)) extensions))
182 (push (intern extension) extensions))
183 (setq extension-lines (cdr extension-lines)))))
185 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
186 (when (and smtp-connection-type
187 (null (eq smtp-connection-type 'force))
188 (memq 'starttls extensions))
189 (smtp-send-command process "STARTTLS")
190 (setq response (smtp-read-response process))
191 (if (or (null (car response))
192 (not (integerp (car response)))
193 (>= (car response) 400))
194 (throw 'done (car (cdr response))))
195 (starttls-negotiate process))
197 ;; AUTH --- SMTP Service Extension for Authentication (RFC2554)
198 (when smtp-authentication-type
199 (let ((auth (intern smtp-authentication-type)) method)
201 (memq auth extensions)
202 (setq method (nth 1 (assq auth smtp-authentication-method-alist))))
203 (funcall method process)
205 (format "AUTH mechanism %s not available" auth)))))
207 ;; ONEX --- One message transaction only (sendmail extension?)
208 (if (or (memq 'onex extensions)
209 (memq 'xone extensions))
211 (smtp-send-command process "ONEX")
212 (setq response (smtp-read-response process))
213 (if (or (null (car response))
214 (not (integerp (car response)))
215 (>= (car response) 400))
216 (throw 'done (car (cdr response))))))
218 ;; VERB --- Verbose (sendmail extension?)
219 (if (and smtp-debug-info
220 (or (memq 'verb extensions)
221 (memq 'xvrb extensions)))
223 (smtp-send-command process "VERB")
224 (setq response (smtp-read-response process))
225 (if (or (null (car response))
226 (not (integerp (car response)))
227 (>= (car response) 400))
228 (throw 'done (car (cdr response))))))
230 ;; XUSR --- Initial (user) submission (sendmail extension?)
231 (if (memq 'xusr extensions)
233 (smtp-send-command process "XUSR")
234 (setq response (smtp-read-response process))
235 (if (or (null (car response))
236 (not (integerp (car response)))
237 (>= (car response) 400))
238 (throw 'done (car (cdr response))))))
240 ;; MAIL FROM:<sender>
243 (format "MAIL FROM:<%s>%s%s"
245 ;; SIZE --- Message Size Declaration (RFC1870)
246 (if (memq 'size extensions)
249 (set-buffer smtp-text-buffer)
250 (+ (- (point-max) (point-min))
251 ;; Add one byte for each change-of-line
252 ;; because or CR-LF representation:
253 (count-lines (point-min) (point-max))
254 ;; For some reason, an empty line is
255 ;; added to the message. Maybe this
256 ;; is a bug, but it can't hurt to add
257 ;; those two bytes anyway:
260 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
261 (if (and (memq '8bitmime extensions)
265 (setq response (smtp-read-response process))
266 (if (or (null (car response))
267 (not (integerp (car response)))
268 (>= (car response) 400))
269 (throw 'done (car (cdr response))))
271 ;; RCPT TO:<recipient>
273 (smtp-send-command process
275 (if smtp-notify-success
276 "RCPT TO:<%s> NOTIFY=SUCCESS"
279 (setq recipients (cdr recipients))
280 (setq response (smtp-read-response process))
281 (if (or (null (car response))
282 (not (integerp (car response)))
283 (>= (car response) 400))
284 (throw 'done (car (cdr response)))))
287 (smtp-send-command process "DATA")
288 (setq response (smtp-read-response process))
289 (if (or (null (car response))
290 (not (integerp (car response)))
291 (>= (car response) 400))
292 (throw 'done (car (cdr response))))
295 (smtp-send-data process smtp-text-buffer)
298 (smtp-send-command process ".")
299 (setq response (smtp-read-response process))
300 (if (or (null (car response))
301 (not (integerp (car response)))
302 (>= (car response) 400))
303 (throw 'done (car (cdr response))))
308 (eq (process-status process) 'open))
311 (smtp-send-command process "QUIT")
312 (smtp-read-response process)
313 (delete-process process)))))))
315 (defun smtp-process-filter (process output)
317 (set-buffer (process-buffer process))
318 (goto-char (point-max))
321 (defun smtp-read-response (process)
322 (let ((case-fold-search nil)
323 (response-strings nil)
324 (response-continue t)
325 (return-value '(nil ()))
328 (while response-continue
329 (goto-char smtp-read-point)
330 (while (not (search-forward "\r\n" nil t))
331 (accept-process-output process)
332 (goto-char smtp-read-point))
334 (setq match-end (point))
335 (setq response-strings
336 (cons (buffer-substring smtp-read-point (- match-end 2))
339 (goto-char smtp-read-point)
340 (if (looking-at "[0-9]+ ")
341 (let ((begin (match-beginning 0))
344 (message "%s" (car response-strings)))
346 (setq smtp-read-point match-end)
348 ;; ignore lines that start with "0"
349 (if (looking-at "0[0-9]+ ")
351 (setq response-continue nil)
354 (buffer-substring begin end))
355 (nreverse response-strings)))))
357 (if (looking-at "[0-9]+-")
358 (progn (if smtp-debug-info
359 (message "%s" (car response-strings)))
360 (setq smtp-read-point match-end)
361 (setq response-continue t))
363 (setq smtp-read-point match-end)
364 (setq response-continue nil)
366 (cons nil (nreverse response-strings)))))))
367 (setq smtp-read-point match-end)
370 (defun smtp-send-command (process command)
371 (goto-char (point-max))
372 (insert command "\r\n")
373 (setq smtp-read-point (point))
374 (process-send-string process command)
375 (process-send-string process "\r\n"))
377 (defun smtp-send-data-1 (process data)
378 (goto-char (point-max))
380 (insert data "\r\n"))
381 (setq smtp-read-point (point))
382 ;; Escape "." at start of a line.
383 (if (eq (string-to-char data) ?.)
384 (process-send-string process "."))
385 (process-send-string process data)
386 (process-send-string process "\r\n"))
388 (defun smtp-send-data (process buffer)
389 (let ((data-continue t)
396 (goto-char (point-min)))
402 (setq this-line (point))
404 (setq this-line-end (point))
405 (setq sending-data nil)
406 (setq sending-data (buffer-substring this-line this-line-end))
407 (if (or (/= (forward-line 1) 0) (eobp))
408 (setq data-continue nil)))
410 (smtp-send-data-1 process sending-data))))
412 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
413 "Get address list suitable for smtp RCPT TO:<address>."
414 (let ((case-fold-search t)
415 (simple-address-list "")
419 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
423 (set-buffer smtp-address-buffer)
425 (insert (save-excursion
426 (set-buffer smtp-text-buffer)
427 (buffer-substring-no-properties header-start header-end)))
428 (goto-char (point-min))
429 ;; RESENT-* fields should stop processing of regular fields.
431 (if (re-search-forward "^RESENT-TO:" header-end t)
433 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
434 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
436 (while (re-search-forward addr-regexp header-end t)
438 (setq this-line (match-beginning 0))
440 ;; get any continuation lines.
441 (while (and (looking-at "^[ \t]+") (< (point) header-end))
443 (setq this-line-end (point-marker))
444 (setq simple-address-list
445 (concat simple-address-list " "
446 (mail-strip-quoted-names
447 (buffer-substring this-line this-line-end)))))
450 (insert-string simple-address-list)
453 (subst-char-in-region (point-min) (point-max) 10 ? t)
455 (subst-char-in-region (point-min) (point-max) ?, ? t)
457 (subst-char-in-region (point-min) (point-max) 9 ? t)
459 (goto-char (point-min))
460 ;; tidyness in case hook is not robust when it looks at this
461 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
463 (goto-char (point-min))
464 (let (recipient-address-list)
465 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
467 (setq recipient-address-list
468 (cons (buffer-substring (match-beginning 1) (match-end 1))
469 recipient-address-list)))
470 recipient-address-list))
471 (kill-buffer smtp-address-buffer))))
473 (defun smtp-auth-cram-md5 (process)
475 (smtp-send-command process "AUTH CRAM-MD5")
476 (setq response (smtp-read-response process))
477 (if (or (null (car response))
478 (not (integerp (car response)))
479 (>= (car response) 400))
480 (throw 'done (car (cdr response))))
483 (base64-encode-string
485 smtp-authentication-user smtp-authentication-passphrase
486 (base64-decode-string
487 (substring (car (cdr response)) 4)))))
488 (setq response (smtp-read-response process))
489 (if (or (null (car response))
490 (not (integerp (car response)))
491 (>= (car response) 400))
492 (throw 'done (car (cdr response))))))
494 (defun smtp-auth-plain (process)
495 (let ((enc-word (copy-sequence smtp-authentication-passphrase))
499 (setq enc-word (unwind-protect
500 (sasl-plain "" smtp-authentication-user enc-word)
501 (fillarray enc-word 0))
502 enc-word (unwind-protect
503 (base64-encode-string enc-word)
504 (fillarray enc-word 0))
505 enc-word (unwind-protect
506 (concat "AUTH PLAIN " enc-word)
507 (fillarray enc-word 0))))
508 (fillarray enc-word 0))
509 (setq response (smtp-read-response process))
510 (if (or (null (car response))
511 (not (integerp (car response)))
512 (>= (car response) 400))
513 (throw 'done (car (cdr response)))))
515 (defun smtp-auth-login (process)
519 (concat "AUTH LOGIN " smtp-authentication-user))
520 (setq response (smtp-read-response process))
521 (if (or (null (car response))
522 (not (integerp (car response)))
523 (>= (car response) 400))
524 (throw 'done (car (cdr response))))
527 (base64-encode-string smtp-authentication-passphrase))
528 (setq response (smtp-read-response process))
529 (if (or (null (car response))
530 (not (integerp (car response)))
531 (>= (car response) 400))
532 (throw 'done (car (cdr response))))))
536 ;;; smtp.el ends here