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
37 (eval-when-compile (require 'sasl))
39 (autoload 'starttls-open-stream "starttls")
40 (autoload 'starttls-negotiate "starttls")
41 (autoload 'sasl-cram-md5 "sasl")
42 (autoload 'sasl-plain "sasl")
43 (autoload 'sasl-scram-md5-client-msg-1 "sasl")
44 (autoload 'sasl-scram-md5-client-msg-2 "sasl")
45 (autoload 'sasl-scram-md5-authenticate-server "sasl")
46 (autoload 'sasl-digest-md5-digest-response "sasl"))
48 (eval-when-compile (require 'cl)) ; push
51 "SMTP protocol for sending mail."
54 (defcustom smtp-default-server nil
55 "*Specify default SMTP server."
56 :type '(choice (const nil) string)
59 (defcustom smtp-server (or (getenv "SMTPSERVER") smtp-default-server)
60 "*The name of the host running SMTP server. It can also be a function
61 called from `smtp-via-smtp' with arguments SENDER and RECIPIENTS."
62 :type '(choice (string :tag "Name")
63 (function :tag "Function"))
66 (defcustom smtp-service "smtp"
67 "*SMTP service port number. \"smtp\" or 25."
68 :type '(choice (integer :tag "25" 25)
69 (string :tag "smtp" "smtp"))
72 (defcustom smtp-use-8bitmime t
73 "*If non-nil, use ESMTP 8BITMIME if available."
77 (defcustom smtp-local-domain nil
78 "*Local domain name without a host name.
79 If the function (system-name) returns the full internet address,
80 don't define this value."
81 :type '(choice (const nil) string)
84 (defcustom smtp-fqdn nil
85 "*User's fully qualified domain name."
86 :type '(choice (const nil) string)
89 (defcustom smtp-debug-info nil
90 "*smtp debug info printout. messages and process buffer."
94 (defcustom smtp-notify-success nil
95 "*If non-nil, notification for successful mail delivery is returned
100 (defcustom smtp-authenticate-type nil
101 "*SMTP authentication mechanism (RFC2554)."
105 (defvar smtp-authenticate-user nil)
106 (defvar smtp-authenticate-passphrase nil)
108 (defvar smtp-authenticate-method-alist
109 '((cram-md5 smtp-auth-cram-md5)
110 (plain smtp-auth-plain)
111 (login smtp-auth-login)
112 (anonymous smtp-auth-anonymous)
113 (scram-md5 smtp-auth-scram-md5)
114 (digest-md5 smtp-auth-digest-md5)))
116 (defcustom smtp-connection-type nil
117 "*SMTP connection type."
118 :type '(choice (const nil) (const :tag "TLS" starttls))
121 (defvar smtp-read-point nil)
123 (defun smtp-make-fqdn ()
124 "Return user's fully qualified domain name."
125 (let ((system-name (system-name)))
130 (concat system-name "." smtp-local-domain))
131 ((string-match "[^.]\\.[^.]" system-name)
134 (error "Cannot generate valid FQDN. Set `smtp-fqdn' or `smtp-local-domain' correctly.")))))
136 (defun smtp-via-smtp (sender recipients smtp-text-buffer)
137 (let ((server (if (functionp smtp-server)
138 (funcall smtp-server sender recipients)
140 process response extensions)
144 (format "*trace of SMTP session to %s*" server)))
146 (make-local-variable 'smtp-read-point)
147 (setq smtp-read-point (point-min))
152 (if smtp-connection-type
154 (starttls-open-stream
155 "SMTP" (current-buffer) server smtp-service))
156 (open-network-stream-as-binary
157 "SMTP" (current-buffer) server smtp-service)))
159 (set-process-filter process 'smtp-process-filter)
161 (if (eq smtp-connection-type 'force)
162 (starttls-negotiate process))
165 (setq response (smtp-read-response process))
166 (if (or (null (car response))
167 (not (integerp (car response)))
168 (>= (car response) 400))
169 (throw 'done (car (cdr response))))
172 (smtp-send-command process
173 (format "EHLO %s" (smtp-make-fqdn)))
174 (setq response (smtp-read-response process))
175 (if (or (null (car response))
176 (not (integerp (car response)))
177 (>= (car response) 400))
180 (smtp-send-command process
181 (format "HELO %s" (smtp-make-fqdn)))
182 (setq response (smtp-read-response process))
183 (if (or (null (car response))
184 (not (integerp (car response)))
185 (>= (car response) 400))
186 (throw 'done (car (cdr response)))))
187 (let ((extension-lines (cdr (cdr response)))
189 (while extension-lines
193 (downcase (substring (car extension-lines) 4))))
194 (while (string-match "\\([^ ]+\\)" extension (match-end 1))
195 (push (intern (match-string 1 extension)) extensions))
196 (push (intern extension) extensions))
197 (setq extension-lines (cdr extension-lines)))))
199 ;; STARTTLS --- begin a TLS negotiation (RFC 2595)
200 (when (and smtp-connection-type
201 (null (eq smtp-connection-type 'force))
202 (memq 'starttls extensions))
203 (smtp-send-command process "STARTTLS")
204 (setq response (smtp-read-response process))
205 (if (or (null (car response))
206 (not (integerp (car response)))
207 (>= (car response) 400))
208 (throw 'done (car (cdr response))))
209 (starttls-negotiate process)
210 ;; for sendmail warning XXX
211 (smtp-send-command process (format "HELO %s" (smtp-make-fqdn)))
212 (setq response (smtp-read-response process)))
214 ;; AUTH --- SMTP Service Extension for Authentication (RFC2554)
215 (when smtp-authenticate-type
216 (let ((auth smtp-authenticate-type) method)
218 (memq auth extensions)
219 (setq method (nth 1 (assq auth smtp-authenticate-method-alist))))
220 (funcall method process)
222 (format "AUTH mechanism %s not available" auth)))))
224 ;; ONEX --- One message transaction only (sendmail extension?)
225 ;;; (if (or (memq 'onex extensions)
226 ;;; (memq 'xone extensions))
228 ;;; (smtp-send-command process "ONEX")
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 ;; VERB --- Verbose (sendmail extension?)
236 ;;; (if (and smtp-debug-info
237 ;;; (or (memq 'verb extensions)
238 ;;; (memq 'xvrb extensions)))
240 ;;; (smtp-send-command process "VERB")
241 ;;; (setq response (smtp-read-response process))
242 ;;; (if (or (null (car response))
243 ;;; (not (integerp (car response)))
244 ;;; (>= (car response) 400))
245 ;;; (throw 'done (car (cdr response))))))
247 ;; XUSR --- Initial (user) submission (sendmail extension?)
248 ;;; (if (memq 'xusr extensions)
250 ;;; (smtp-send-command process "XUSR")
251 ;;; (setq response (smtp-read-response process))
252 ;;; (if (or (null (car response))
253 ;;; (not (integerp (car response)))
254 ;;; (>= (car response) 400))
255 ;;; (throw 'done (car (cdr response))))))
257 ;; MAIL FROM:<sender>
260 (format "MAIL FROM:<%s>%s%s"
262 ;; SIZE --- Message Size Declaration (RFC1870)
263 (if (memq 'size extensions)
266 (set-buffer smtp-text-buffer)
267 (+ (- (point-max) (point-min))
268 ;; Add one byte for each change-of-line
269 ;; because or CR-LF representation:
270 (count-lines (point-min) (point-max))
271 ;; For some reason, an empty line is
272 ;; added to the message. Maybe this
273 ;; is a bug, but it can't hurt to add
274 ;; those two bytes anyway:
277 ;; 8BITMIME --- 8bit-MIMEtransport (RFC1652)
278 (if (and (memq '8bitmime extensions)
282 (setq response (smtp-read-response process))
283 (if (or (null (car response))
284 (not (integerp (car response)))
285 (>= (car response) 400))
286 (throw 'done (car (cdr response))))
288 ;; RCPT TO:<recipient>
290 (smtp-send-command process
292 (if smtp-notify-success
293 (if (memq 'dsn extensions)
294 "RCPT TO:<%s> NOTIFY=SUCCESS"
296 (format "Delivery Status Notifications is not available")))
299 (setq recipients (cdr recipients))
300 (setq response (smtp-read-response process))
301 (if (or (null (car response))
302 (not (integerp (car response)))
303 (>= (car response) 400))
304 (throw 'done (car (cdr response)))))
307 (smtp-send-command process "DATA")
308 (setq response (smtp-read-response process))
309 (if (or (null (car response))
310 (not (integerp (car response)))
311 (>= (car response) 400))
312 (throw 'done (car (cdr response))))
315 (smtp-send-data process smtp-text-buffer)
318 (smtp-send-command process ".")
319 (setq response (smtp-read-response process))
320 (if (or (null (car response))
321 (not (integerp (car response)))
322 (>= (car response) 400))
323 (throw 'done (car (cdr response))))
328 (memq (process-status process) '(open run)))
331 (smtp-send-command process "QUIT")
332 (smtp-read-response process)
333 (delete-process process)))))))
335 (defun smtp-process-filter (process output)
337 (set-buffer (process-buffer process))
338 (goto-char (point-max))
341 (defun smtp-read-response (process)
342 (let ((case-fold-search nil)
343 (response-strings nil)
344 (response-continue t)
345 (return-value '(nil ()))
348 (while response-continue
349 (goto-char smtp-read-point)
350 (while (not (search-forward "\r\n" nil t))
351 (accept-process-output process)
352 (goto-char smtp-read-point))
354 (setq match-end (point))
355 (setq response-strings
356 (cons (buffer-substring smtp-read-point (- match-end 2))
359 (goto-char smtp-read-point)
360 (if (looking-at "[0-9]+ ")
361 (let ((begin (match-beginning 0))
364 (message "%s" (car response-strings)))
366 (setq smtp-read-point match-end)
368 ;; ignore lines that start with "0"
369 (if (looking-at "0[0-9]+ ")
371 (setq response-continue nil)
374 (buffer-substring begin end))
375 (nreverse response-strings)))))
377 (if (looking-at "[0-9]+-")
378 (progn (if smtp-debug-info
379 (message "%s" (car response-strings)))
380 (setq smtp-read-point match-end)
381 (setq response-continue t))
383 (setq smtp-read-point match-end)
384 (setq response-continue nil)
386 (cons nil (nreverse response-strings)))))))
387 (setq smtp-read-point match-end)
390 (defun smtp-send-command (process command &optional secure)
391 (goto-char (point-max))
393 (insert "Here is insecure words.\r\n")
394 (insert command "\r\n"))
395 (setq smtp-read-point (point))
396 (process-send-string process command)
397 (process-send-string process "\r\n"))
399 (defun smtp-send-data-1 (process data)
400 (goto-char (point-max))
402 (insert data "\r\n"))
403 (setq smtp-read-point (point))
404 ;; Escape "." at start of a line.
405 (if (eq (string-to-char data) ?.)
406 (process-send-string process "."))
407 (process-send-string process data)
408 (process-send-string process "\r\n"))
410 (defun smtp-send-data (process buffer)
411 (let ((data-continue t)
418 (goto-char (point-min)))
424 (setq this-line (point))
426 (setq this-line-end (point))
427 (setq sending-data nil)
428 (setq sending-data (buffer-substring this-line this-line-end))
429 (if (or (/= (forward-line 1) 0) (eobp))
430 (setq data-continue nil)))
432 (smtp-send-data-1 process sending-data))))
434 (defun smtp-deduce-address-list (smtp-text-buffer header-start header-end)
435 "Get address list suitable for smtp RCPT TO:<address>."
436 (let ((simple-address-list "")
440 (smtp-address-buffer (generate-new-buffer " *smtp-mail*")))
444 (set-buffer smtp-address-buffer)
445 (setq case-fold-search t)
447 (insert (save-excursion
448 (set-buffer smtp-text-buffer)
449 (buffer-substring-no-properties header-start header-end)))
450 (goto-char (point-min))
451 ;; RESENT-* fields should stop processing of regular fields.
453 (if (re-search-forward "^RESENT-TO:" header-end t)
455 "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
456 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
458 (while (re-search-forward addr-regexp header-end t)
460 (setq this-line (match-beginning 0))
462 ;; get any continuation lines.
463 (while (and (looking-at "^[ \t]+") (< (point) header-end))
465 (setq this-line-end (point-marker))
466 (setq simple-address-list
467 (concat simple-address-list " "
468 (mail-strip-quoted-names
469 (buffer-substring this-line this-line-end)))))
472 (insert-string simple-address-list)
475 (subst-char-in-region (point-min) (point-max) 10 ? t)
477 (subst-char-in-region (point-min) (point-max) ?, ? t)
479 (subst-char-in-region (point-min) (point-max) 9 ? t)
481 (goto-char (point-min))
482 ;; tidyness in case hook is not robust when it looks at this
483 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
485 (goto-char (point-min))
486 (let (recipient-address-list)
487 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
489 (setq recipient-address-list
490 (cons (buffer-substring (match-beginning 1) (match-end 1))
491 recipient-address-list)))
492 recipient-address-list))
493 (kill-buffer smtp-address-buffer))))
495 (defun smtp-auth-cram-md5 (process)
496 (let ((secure-word (copy-sequence smtp-authenticate-passphrase))
498 (smtp-send-command process "AUTH CRAM-MD5")
499 (setq response (smtp-read-response process))
500 (if (or (null (car response))
501 (not (integerp (car response)))
502 (>= (car response) 400))
503 (throw 'done (car (cdr response))))
506 (setq secure-word (unwind-protect
508 smtp-authenticate-user secure-word
509 (base64-decode-string
510 (substring (car (cdr response)) 4)))
511 (fillarray secure-word 0))
512 secure-word (unwind-protect
513 (base64-encode-string secure-word)
514 (fillarray secure-word 0))) t)
515 (fillarray secure-word 0)
516 (setq response (smtp-read-response process))
517 (if (or (null (car response))
518 (not (integerp (car response)))
519 (>= (car response) 400))
520 (throw 'done (car (cdr response))))))
522 (defun smtp-auth-plain (process)
523 (let ((secure-word (copy-sequence smtp-authenticate-passphrase))
527 (setq secure-word (unwind-protect
528 (sasl-plain "" smtp-authenticate-user secure-word)
529 (fillarray secure-word 0))
530 secure-word (unwind-protect
531 (base64-encode-string secure-word)
532 (fillarray secure-word 0))
533 secure-word (unwind-protect
534 (concat "AUTH PLAIN " secure-word)
535 (fillarray secure-word 0))) t)
536 (fillarray secure-word 0)
537 (setq response (smtp-read-response process))
538 (if (or (null (car response))
539 (not (integerp (car response)))
540 (>= (car response) 400))
541 (throw 'done (car (cdr response))))))
543 (defun smtp-auth-login (process)
544 (let ((secure-word (copy-sequence smtp-authenticate-passphrase))
546 (smtp-send-command process "AUTH LOGIN")
547 (setq response (smtp-read-response process))
548 (if (or (null (car response))
549 (not (integerp (car response)))
550 (>= (car response) 400))
551 (throw 'done (car (cdr response))))
554 (base64-encode-string
555 smtp-authenticate-user))
556 (setq response (smtp-read-response process))
557 (if (or (null (car response))
558 (not (integerp (car response)))
559 (>= (car response) 400))
560 (throw 'done (car (cdr response))))
563 (setq secure-word (unwind-protect
564 (base64-encode-string secure-word)
565 (fillarray secure-word 0))) t)
566 (fillarray secure-word 0)
567 (setq response (smtp-read-response process))
568 (if (or (null (car response))
569 (not (integerp (car response)))
570 (>= (car response) 400))
571 (throw 'done (car (cdr response))))))
573 (defun smtp-auth-anonymous (process &optional token)
576 process "AUTH ANONYMOUS")
577 (setq response (smtp-read-response process))
578 (if (or (null (car response))
579 (not (integerp (car response)))
580 (>= (car response) 400))
581 (throw 'done (car (cdr response))))
582 (smtp-send-command process
583 (base64-encode-string
587 (setq response (smtp-read-response process))
588 (if (or (null (car response))
589 (not (integerp (car response)))
590 (>= (car response) 400))
591 (throw 'done (car (cdr response))))))
593 (defun smtp-auth-scram-md5 (process)
595 (let (server-msg-1 server-msg-2 client-msg-1 salted-pass
596 response secure-word)
597 (smtp-send-command process "AUTH SCRAM-MD5")
598 (setq response (smtp-read-response process))
599 (if (or (null (car response))
600 (not (integerp (car response)))
601 (>= (car response) 400))
602 (throw 'done (car (cdr response))))
607 (base64-encode-string
609 (sasl-scram-md5-client-msg-1
610 smtp-authenticate-user)))) t)
611 (fillarray secure-word 0))
612 (setq response (smtp-read-response process))
613 (if (or (null (car response))
614 (not (integerp (car response)))
615 (>= (car response) 400))
617 (fillarray client-msg-1 0)
618 (throw 'done (car (cdr response)))))
621 (substring (car (cdr response)) 4)
622 (fillarray (car (cdr response)) 0)))
625 (base64-decode-string secure-word)
626 (fillarray secure-word 0)))
628 (sasl-scram-md5-client-msg-2
629 server-msg-1 client-msg-1
631 (sasl-scram-md5-make-salted-pass
632 smtp-authenticate-passphrase server-msg-1))))
635 (base64-encode-string secure-word)
636 (fillarray secure-word 0)))
638 (smtp-send-command process secure-word t)
639 (fillarray secure-word 0))
640 (setq response (smtp-read-response process))
641 (if (or (null (car response))
642 (not (integerp (car response)))
643 (>= (car response) 400))
645 (fillarray salted-pass 0)
646 (fillarray server-msg-1 0)
647 (fillarray client-msg-1 0)
648 (throw 'done (car (cdr response)))))
651 (base64-decode-string
653 (substring (car (cdr response)) 4)))
654 (fillarray secure-word 0)))
657 (sasl-scram-md5-authenticate-server
662 (fillarray salted-pass 0)
663 (fillarray server-msg-1 0)
664 (fillarray server-msg-2 0)
665 (fillarray client-msg-1 0)))
667 (smtp-send-command process "")
668 (setq response (smtp-read-response process))
669 (if (or (null (car response))
670 (not (integerp (car response)))
671 (>= (car response) 400))
672 (throw 'done (car (cdr response)))) ))
674 (defun smtp-auth-digest-md5 (process)
675 "Login to server using the AUTH DIGEST-MD5 method."
676 (let (user realm response)
677 (smtp-send-command process "AUTH DIGEST-MD5")
678 (setq response (smtp-read-response process))
679 (if (or (null (car response))
680 (not (integerp (car response)))
681 (>= (car response) 400))
682 (throw 'done (car (cdr response))))
683 (if (string-match "^\\([^@]*\\)@\\([^@]*\\)"
684 smtp-authenticate-user)
685 (setq user (match-string 1 smtp-authenticate-user)
686 realm (match-string 2 smtp-authenticate-user))
687 (setq user smtp-authenticate-user
689 (smtp-send-command process
690 (base64-encode-string
691 (sasl-digest-md5-digest-response
692 (base64-decode-string
693 (substring (car (cdr response)) 4))
695 smtp-authenticate-passphrase
696 "smtp" smtp-server realm)
698 (setq response (smtp-read-response process))
699 (if (or (null (car response))
700 (not (integerp (car response)))
701 (>= (car response) 400))
702 (throw 'done (car (cdr response))))
703 (smtp-send-command process "")))
707 ;;; smtp.el ends here