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")
42 (autoload 'sasl-scram-md5-client-msg-1 "sasl")
43 (autoload 'sasl-scram-md5-client-msg-2 "sasl")
44 (autoload 'sasl-scram-md5-authenticate-server "sasl"))
46 (eval-when-compile (require 'cl)) ; push
49 "SMTP protocol for sending mail."
52 (defcustom smtp-default-server nil
53 "*Specify default SMTP server."
54 :type '(choice (const nil) string)
57 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
58 "*The name of the host running SMTP server. It can also be a function
59 called from `smtp-via-smtp' with arguments SENDER and RECIPIENTS."
60 :type '(choice (string :tag "Name")
61 (function :tag "Function"))
64 (defcustom smtp-service "smtp"
65 "*SMTP service port number. \"smtp\" or 25."
66 :type '(choice (integer :tag "25" 25)
67 (string :tag "smtp" "smtp"))
70 (defcustom smtp-use-8bitmime t
71 "*If non-nil, use ESMTP 8BITMIME if available."
75 (defcustom smtp-local-domain nil
76 "*Local domain name without a host name.
77 If the function (system-name) returns the full internet address,
78 don't define this value."
79 :type '(choice (const nil) string)
82 (defcustom smtp-debug-info nil
83 "*smtp debug info printout. messages and process buffer."
87 (defcustom smtp-notify-success nil
88 "*If non-nil, notification for successful mail delivery is returned
93 (defcustom smtp-authentication-type nil
94 "*SMTP authentication mechanism (RFC2554)."
98 (defvar smtp-authentication-user nil)
99 (defvar smtp-authentication-passphrase nil)
101 (defvar smtp-authentication-method-alist
102 '((cram-md5 smtp-auth-cram-md5)
103 (plain smtp-auth-plain)
104 (login smtp-auth-login)
105 (anonymous smtp-auth-anonymous)
106 (scram-md5 smtp-auth-scram-md5)
109 (defcustom smtp-connection-type nil
110 "*SMTP connection type."
111 :type '(choice (const nil) (const :tag "TLS" starttls))
114 (defvar smtp-read-point nil)
116 (defun smtp-make-fqdn ()
117 "Return user's fully qualified domain name."
118 (let ((system-name (system-name)))
121 (concat system-name "." smtp-local-domain))
122 ((string-match "[^.]\\.[^.]" system-name)
125 (error "Cannot generate valid FQDN. Set `smtp-local-domain' correctly.")))))
127 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
128 (let ((server (if (functionp smtp-server)
129 (funcall smtp-server sender recipients)
131 process response extensions)
135 (format "*trace of SMTP session to %s*" server)))
137 (make-local-variable 'smtp-read-point)
138 (setq smtp-read-point (point-min))
143 (if smtp-connection-type
145 (starttls-open-stream
146 "SMTP" (current-buffer) server smtp-service))
147 (open-network-stream-as-binary
148 "SMTP" (current-buffer) server smtp-service)))
150 (set-process-filter process 'smtp-process-filter)
152 (if (eq smtp-connection-type 'force)
153 (starttls-negotiate process))
156 (setq response (smtp-read-response process))
157 (if (or (null (car response))
158 (not (integerp (car response)))
159 (>= (car response) 400))
160 (throw 'done (car (cdr response))))
163 (smtp-send-command process
164 (format "EHLO %s" (smtp-make-fqdn)))
165 (setq response (smtp-read-response process))
166 (if (or (null (car response))
167 (not (integerp (car response)))
168 (>= (car response) 400))
171 (smtp-send-command process
172 (format "HELO %s" (smtp-make-fqdn)))
173 (setq response (smtp-read-response process))
174 (if (or (null (car response))
175 (not (integerp (car response)))
176 (>= (car response) 400))
177 (throw 'done (car (cdr response)))))
178 (let ((extension-lines (cdr (cdr response)))
180 (while extension-lines
184 (downcase (substring (car extension-lines) 4))))
185 (while (string-match "\\([^ ]+\\)" extension (match-end 1))
186 (push (intern (match-string 1 extension)) extensions))
187 (push (intern extension) extensions))
188 (setq extension-lines (cdr extension-lines)))))
190 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
191 (when (and smtp-connection-type
192 (null (eq smtp-connection-type 'force))
193 (memq 'starttls extensions))
194 (smtp-send-command process "STARTTLS")
195 (setq response (smtp-read-response process))
196 (if (or (null (car response))
197 (not (integerp (car response)))
198 (>= (car response) 400))
199 (throw 'done (car (cdr response))))
200 (starttls-negotiate process))
202 ;; AUTH --- SMTP Service Extension for Authentication (RFC2554)
203 (when smtp-authentication-type
204 (let ((auth (intern smtp-authentication-type)) method)
206 (memq auth extensions)
207 (setq method (nth 1 (assq auth smtp-authentication-method-alist))))
208 (funcall method process)
210 (format "AUTH mechanism %s not available" auth)))))
212 ;; ONEX --- One message transaction only (sendmail extension?)
213 ;;; (if (or (memq 'onex extensions)
214 ;;; (memq 'xone extensions))
216 ;;; (smtp-send-command process "ONEX")
217 ;;; (setq response (smtp-read-response process))
218 ;;; (if (or (null (car response))
219 ;;; (not (integerp (car response)))
220 ;;; (>= (car response) 400))
221 ;;; (throw 'done (car (cdr response))))))
223 ;; VERB --- Verbose (sendmail extension?)
224 ;;; (if (and smtp-debug-info
225 ;;; (or (memq 'verb extensions)
226 ;;; (memq 'xvrb extensions)))
228 ;;; (smtp-send-command process "VERB")
229 ;;; (setq response (smtp-read-response process))
230 ;;; (if (or (null (car response))
231 ;;; (not (integerp (car response)))
232 ;;; (>= (car response) 400))
233 ;;; (throw 'done (car (cdr response))))))
235 ;; XUSR --- Initial (user) submission (sendmail extension?)
236 ;;; (if (memq 'xusr extensions)
238 ;;; (smtp-send-command process "XUSR")
239 ;;; (setq response (smtp-read-response process))
240 ;;; (if (or (null (car response))
241 ;;; (not (integerp (car response)))
242 ;;; (>= (car response) 400))
243 ;;; (throw 'done (car (cdr response))))))
245 ;; MAIL FROM:<sender>
248 (format "MAIL FROM:<%s>%s%s"
250 ;; SIZE --- Message Size Declaration (RFC1870)
251 (if (memq 'size extensions)
254 (set-buffer smtp-text-buffer)
255 (+ (- (point-max) (point-min))
256 ;; Add one byte for each change-of-line
257 ;; because or CR-LF representation:
258 (count-lines (point-min) (point-max))
259 ;; For some reason, an empty line is
260 ;; added to the message. Maybe this
261 ;; is a bug, but it can't hurt to add
262 ;; those two bytes anyway:
265 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
266 (if (and (memq '8bitmime extensions)
270 (setq response (smtp-read-response process))
271 (if (or (null (car response))
272 (not (integerp (car response)))
273 (>= (car response) 400))
274 (throw 'done (car (cdr response))))
276 ;; RCPT TO:<recipient>
278 (smtp-send-command process
280 (if smtp-notify-success
281 "RCPT TO:<%s> NOTIFY=SUCCESS"
284 (setq recipients (cdr recipients))
285 (setq response (smtp-read-response process))
286 (if (or (null (car response))
287 (not (integerp (car response)))
288 (>= (car response) 400))
289 (throw 'done (car (cdr response)))))
292 (smtp-send-command process "DATA")
293 (setq response (smtp-read-response process))
294 (if (or (null (car response))
295 (not (integerp (car response)))
296 (>= (car response) 400))
297 (throw 'done (car (cdr response))))
300 (smtp-send-data process smtp-text-buffer)
303 (smtp-send-command process ".")
304 (setq response (smtp-read-response process))
305 (if (or (null (car response))
306 (not (integerp (car response)))
307 (>= (car response) 400))
308 (throw 'done (car (cdr response))))
313 (eq (process-status process) 'open))
316 (smtp-send-command process "QUIT")
317 (smtp-read-response process)
318 (delete-process process)))))))
320 (defun smtp-process-filter (process output)
322 (set-buffer (process-buffer process))
323 (goto-char (point-max))
326 (defun smtp-read-response (process)
327 (let ((case-fold-search nil)
328 (response-strings nil)
329 (response-continue t)
330 (return-value '(nil ()))
333 (while response-continue
334 (goto-char smtp-read-point)
335 (while (not (search-forward "\r\n" nil t))
336 (accept-process-output process)
337 (goto-char smtp-read-point))
339 (setq match-end (point))
340 (setq response-strings
341 (cons (buffer-substring smtp-read-point (- match-end 2))
344 (goto-char smtp-read-point)
345 (if (looking-at "[0-9]+ ")
346 (let ((begin (match-beginning 0))
349 (message "%s" (car response-strings)))
351 (setq smtp-read-point match-end)
353 ;; ignore lines that start with "0"
354 (if (looking-at "0[0-9]+ ")
356 (setq response-continue nil)
359 (buffer-substring begin end))
360 (nreverse response-strings)))))
362 (if (looking-at "[0-9]+-")
363 (progn (if smtp-debug-info
364 (message "%s" (car response-strings)))
365 (setq smtp-read-point match-end)
366 (setq response-continue t))
368 (setq smtp-read-point match-end)
369 (setq response-continue nil)
371 (cons nil (nreverse response-strings)))))))
372 (setq smtp-read-point match-end)
375 (defun smtp-send-command (process command &optional secure)
376 (goto-char (point-max))
378 (insert "Here is insecure words.\r\n")
379 (insert command "\r\n"))
380 (setq smtp-read-point (point))
381 (process-send-string process command)
382 (process-send-string process "\r\n"))
384 (defun smtp-send-data-1 (process data)
385 (goto-char (point-max))
387 (insert data "\r\n"))
388 (setq smtp-read-point (point))
389 ;; Escape "." at start of a line.
390 (if (eq (string-to-char data) ?.)
391 (process-send-string process "."))
392 (process-send-string process data)
393 (process-send-string process "\r\n"))
395 (defun smtp-send-data (process buffer)
396 (let ((data-continue t)
403 (goto-char (point-min)))
409 (setq this-line (point))
411 (setq this-line-end (point))
412 (setq sending-data nil)
413 (setq sending-data (buffer-substring this-line this-line-end))
414 (if (or (/= (forward-line 1) 0) (eobp))
415 (setq data-continue nil)))
417 (smtp-send-data-1 process sending-data))))
419 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
420 "Get address list suitable for smtp RCPT TO:<address>."
421 (let ((case-fold-search t)
422 (simple-address-list "")
426 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
430 (set-buffer smtp-address-buffer)
432 (insert (save-excursion
433 (set-buffer smtp-text-buffer)
434 (buffer-substring-no-properties header-start header-end)))
435 (goto-char (point-min))
436 ;; RESENT-* fields should stop processing of regular fields.
438 (if (re-search-forward "^RESENT-TO:" header-end t)
440 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
441 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
443 (while (re-search-forward addr-regexp header-end t)
445 (setq this-line (match-beginning 0))
447 ;; get any continuation lines.
448 (while (and (looking-at "^[ \t]+") (< (point) header-end))
450 (setq this-line-end (point-marker))
451 (setq simple-address-list
452 (concat simple-address-list " "
453 (mail-strip-quoted-names
454 (buffer-substring this-line this-line-end)))))
457 (insert-string simple-address-list)
460 (subst-char-in-region (point-min) (point-max) 10 ? t)
462 (subst-char-in-region (point-min) (point-max) ?, ? t)
464 (subst-char-in-region (point-min) (point-max) 9 ? t)
466 (goto-char (point-min))
467 ;; tidyness in case hook is not robust when it looks at this
468 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
470 (goto-char (point-min))
471 (let (recipient-address-list)
472 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
474 (setq recipient-address-list
475 (cons (buffer-substring (match-beginning 1) (match-end 1))
476 recipient-address-list)))
477 recipient-address-list))
478 (kill-buffer smtp-address-buffer))))
480 (defun smtp-auth-cram-md5 (process)
481 (let ((secure-word (copy-sequence smtp-authentication-passphrase))
483 (smtp-send-command process "AUTH CRAM-MD5")
484 (setq response (smtp-read-response process))
485 (if (or (null (car response))
486 (not (integerp (car response)))
487 (>= (car response) 400))
488 (throw 'done (car (cdr response))))
491 (setq secure-word (unwind-protect
493 smtp-authentication-user secure-word
494 (base64-decode-string
495 (substring (car (cdr response)) 4)))
496 (fillarray secure-word 0))
497 secure-word (unwind-protect
498 (base64-encode-string secure-word)
499 (fillarray secure-word 0))) t)
500 (fillarray secure-word 0)
501 (setq response (smtp-read-response process))
502 (if (or (null (car response))
503 (not (integerp (car response)))
504 (>= (car response) 400))
505 (throw 'done (car (cdr response))))))
507 (defun smtp-auth-plain (process)
508 (let ((secure-word (copy-sequence smtp-authentication-passphrase))
512 (setq secure-word (unwind-protect
513 (sasl-plain "" smtp-authentication-user secure-word)
514 (fillarray secure-word 0))
515 secure-word (unwind-protect
516 (base64-encode-string secure-word)
517 (fillarray secure-word 0))
518 secure-word (unwind-protect
519 (concat "AUTH PLAIN " secure-word)
520 (fillarray secure-word 0))) t)
521 (fillarray secure-word 0)
522 (setq response (smtp-read-response process))
523 (if (or (null (car response))
524 (not (integerp (car response)))
525 (>= (car response) 400))
526 (throw 'done (car (cdr response))))))
528 (defun smtp-auth-login (process)
529 (let ((secure-word (copy-sequence smtp-authentication-passphrase))
533 (concat "AUTH LOGIN " smtp-authentication-user))
534 (setq response (smtp-read-response process))
535 (if (or (null (car response))
536 (not (integerp (car response)))
537 (>= (car response) 400))
538 (throw 'done (car (cdr response))))
541 (setq secure-word (unwind-protect
542 (base64-encode-string secure-word)
543 (fillarray secure-word 0))) t)
544 (fillarray secure-word 0)
545 (setq response (smtp-read-response process))
546 (if (or (null (car response))
547 (not (integerp (car response)))
548 (>= (car response) 400))
549 (throw 'done (car (cdr response))))))
551 (defun smtp-auth-anonymous (process &optional token)
554 process "AUTH ANONYMOUS")
555 (setq response (smtp-read-response process))
556 (if (or (null (car response))
557 (not (integerp (car response)))
558 (>= (car response) 400))
559 (throw 'done (car (cdr response))))
560 (smtp-send-command process
561 (base64-encode-string
565 (setq response (smtp-read-response process))
566 (if (or (null (car response))
567 (not (integerp (car response)))
568 (>= (car response) 400))
569 (throw 'done (car (cdr response))))))
571 (defun smtp-auth-scram-md5 (process)
573 (let (server-msg-1 server-msg-2 client-msg-1 salted-pass
574 response secure-word)
575 (smtp-send-command process "AUTH SCRAM-MD5")
576 (setq response (smtp-read-response process))
577 (if (or (null (car response))
578 (not (integerp (car response)))
579 (>= (car response) 400))
580 (throw 'done (car (cdr response))))
585 (base64-encode-string
587 (sasl-scram-md5-client-msg-1
588 smtp-authentication-user)))) t)
589 (fillarray secure-word 0))
590 (setq response (smtp-read-response process))
591 (if (or (null (car response))
592 (not (integerp (car response)))
593 (>= (car response) 400))
595 (fillarray client-msg-1 0)
596 (throw 'done (car (cdr response)))))
599 (substring (car (cdr response)) 4)
600 (fillarray (car (cdr response)) 0)))
603 (base64-decode-string secure-word)
604 (fillarray secure-word 0)))
606 (sasl-scram-md5-client-msg-2
607 server-msg-1 client-msg-1
609 (sasl-scram-md5-make-salted-pass
610 smtp-authentication-passphrase server-msg-1))))
613 (base64-encode-string secure-word)
614 (fillarray secure-word 0)))
616 (smtp-send-command process secure-word t)
617 (fillarray secure-word 0))
618 (setq response (smtp-read-response process))
619 (if (or (null (car response))
620 (not (integerp (car response)))
621 (>= (car response) 400))
623 (fillarray salted-pass 0)
624 (fillarray server-msg-1 0)
625 (fillarray client-msg-1 0)
626 (throw 'done (car (cdr response)))))
629 (base64-decode-string
631 (substring (car (cdr response)) 4)))
632 (fillarray secure-word 0)))
635 (sasl-scram-md5-authenticate-server
640 (fillarray salted-pass 0)
641 (fillarray server-msg-1 0)
642 (fillarray server-msg-2 0)
643 (fillarray client-msg-1 0)))
645 (smtp-send-command process "")
646 (setq response (smtp-read-response process))
647 (if (or (null (car response))
648 (not (integerp (car response)))
649 (>= (car response) 400))
650 (throw 'done (car (cdr response)))) ))
654 ;;; smtp.el ends here