1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
6 ;; Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
7 ;; ESMTP support: Simon Leinen <simon@switch.ch>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; Send Mail to smtp host from smtpmail temp buffer.
31 ;; Please add these lines in your .emacs(_emacs).
33 ;;(setq send-mail-function 'smtpmail-send-it)
34 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
35 ;;(setq smtpmail-smtp-service "smtp")
36 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
37 ;;(setq smtpmail-debug-info t)
38 ;;(load-library "smtpmail")
39 ;;(setq smtpmail-code-conv-from nil)
40 ;;(setq user-full-name "YOUR NAME HERE")
42 ;; To queue mail, set smtpmail-queue-mail to t and use
43 ;; smtpmail-send-queued-mail to send.
52 (defgroup smtpmail nil
53 "SMTP protocol for sending mail."
57 (defcustom smtpmail-default-smtp-server nil
58 "*Specify default SMTP server."
59 :type '(choice (const nil) string)
62 (defcustom smtpmail-smtp-server
63 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
64 "*The name of the host running SMTP server."
65 :type '(choice (const nil) string)
68 (defcustom smtpmail-smtp-service 25
69 "*SMTP service port number. smtp or 25 ."
73 (defcustom smtpmail-local-domain nil
74 "*Local domain name without a host name.
75 If the function (system-name) returns the full internet address,
76 don't define this value."
77 :type '(choice (const nil) string)
80 (defcustom smtpmail-debug-info nil
81 "*smtpmail debug info printout. messages and process buffer."
85 (defcustom smtpmail-code-conv-from nil ;; *junet*
86 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
90 (defcustom smtpmail-queue-mail nil
91 "*Specify if mail is queued (if t) or sent immediately (if nil).
92 If queued, it is stored in the directory `smtpmail-queue-dir'
93 and sent with `smtpmail-send-queued-mail'."
97 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
98 "*Directory where `smtpmail.el' stores queued mail."
102 (defvar smtpmail-queue-index-file "index"
103 "File name of queued mail index,
104 This is relative to `smtpmail-queue-dir'.")
106 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
107 smtpmail-queue-index-file))
113 (defun smtpmail-send-it ()
114 (require 'mail-utils)
115 (let ((errbuf (if mail-interactive
116 (generate-new-buffer " smtpmail errors")
118 (tembuf (generate-new-buffer " smtpmail temp"))
119 (case-fold-search nil)
121 (mailbuf (current-buffer)))
126 (insert-buffer-substring mailbuf)
127 (goto-char (point-max))
128 ;; require one newline at the end.
129 (or (= (preceding-char) ?\n)
131 ;; Change header-delimiter to be what sendmail expects.
132 (mail-sendmail-undelimit-header)
133 (setq delimline (point-marker))
134 ;; (sendmail-synch-aliases)
136 (expand-mail-aliases (point-min) delimline))
137 (goto-char (point-min))
138 ;; ignore any blank lines in the header
139 (while (and (re-search-forward "\n\n\n*" delimline t)
140 (< (point) delimline))
141 (replace-match "\n"))
142 (let ((case-fold-search t))
143 ;; We used to process Resent-... headers here,
144 ;; but it was not done properly, and the job
145 ;; is done correctly in smtpmail-deduce-address-list.
146 ;; Don't send out a blank subject line
147 (goto-char (point-min))
148 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
150 ;; This one matches a Subject just before the header delimiter.
151 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
152 (= (match-end 0) delimline))
154 ;; Put the "From:" field in unless for some odd reason
155 ;; they put one in themselves.
156 (goto-char (point-min))
157 (if (not (re-search-forward "^From:" delimline t))
158 (let* ((login user-mail-address)
159 (fullname (user-full-name)))
160 (cond ((eq mail-from-style 'angles)
161 (insert "From: " fullname)
162 (let ((fullname-start (+ (point-min) 6))
163 (fullname-end (point-marker)))
164 (goto-char fullname-start)
165 ;; Look for a character that cannot appear unquoted
166 ;; according to RFC 822.
167 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
170 ;; Quote fullname, escaping specials.
171 (goto-char fullname-start)
173 (while (re-search-forward "[\"\\]"
175 (replace-match "\\\\\\&" t))
177 (insert " <" login ">\n"))
178 ((eq mail-from-style 'parens)
179 (insert "From: " login " (")
180 (let ((fullname-start (point)))
182 (let ((fullname-end (point-marker)))
183 (goto-char fullname-start)
184 ;; RFC 822 says \ and nonmatching parentheses
185 ;; must be escaped in comments.
186 ;; Escape every instance of ()\ ...
187 (while (re-search-forward "[()\\]" fullname-end 1)
188 (replace-match "\\\\\\&" t))
189 ;; ... then undo escaping of matching parentheses,
190 ;; including matching nested parentheses.
191 (goto-char fullname-start)
192 (while (re-search-forward
193 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
195 (replace-match "\\1(\\3)" t)
196 (goto-char fullname-start))))
198 ((null mail-from-style)
199 (insert "From: " login "\n")))))
200 ;; Insert an extra newline if we need it to work around
201 ;; Sun's bug that swallows newlines.
202 (goto-char (1+ delimline))
203 (if (eval mail-mailer-swallows-blank-line)
205 ;; Find and handle any FCC fields.
206 (goto-char (point-min))
207 (if (re-search-forward "^FCC:" delimline t)
208 (mail-do-fcc delimline))
216 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
217 (setq smtpmail-recipient-address-list
218 (smtpmail-deduce-address-list tembuf (point-min) delimline))
219 (kill-buffer smtpmail-address-buffer)
221 (smtpmail-do-bcc delimline)
223 (if (not smtpmail-queue-mail)
224 (if (not (null smtpmail-recipient-address-list))
225 (if (not (smtpmail-via-smtp
226 smtpmail-recipient-address-list tembuf))
227 (error "Sending failed; SMTP protocol error"))
228 (error "Sending failed; no recipients"))
229 (let* ((file-data (concat
231 (concat (time-stamp-yyyy-mm-dd)
232 "_" (time-stamp-hh:mm:ss))))
233 (file-elisp (concat file-data ".el"))
234 (buffer-data (create-file-buffer file-data))
235 (buffer-elisp (create-file-buffer file-elisp))
236 (buffer-scratch "*queue-mail*"))
238 (set-buffer buffer-data)
240 (insert-buffer tembuf)
241 (write-file file-data)
242 (set-buffer buffer-elisp)
245 "(setq smtpmail-recipient-address-list '"
246 (prin1-to-string smtpmail-recipient-address-list)
248 (write-file file-elisp)
249 (set-buffer (generate-new-buffer buffer-scratch))
250 (insert (concat file-data "\n"))
251 (append-to-file (point-min)
253 smtpmail-queue-index)
255 (kill-buffer buffer-scratch)
256 (kill-buffer buffer-data)
257 (kill-buffer buffer-elisp))))
260 (kill-buffer errbuf)))))
262 (defun smtpmail-send-queued-mail ()
263 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
265 ;;; Get index, get first mail, send it, get second mail, etc...
266 (let ((buffer-index (find-file-noselect smtpmail-queue-index))
270 (set-buffer buffer-index)
271 (beginning-of-buffer)
273 (setq file-msg (buffer-substring (point) (save-excursion
277 (setq tembuf (find-file-noselect file-msg))
278 (if (not (null smtpmail-recipient-address-list))
279 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
281 (error "Sending failed; SMTP protocol error"))
282 (error "Sending failed; no recipients"))
283 (delete-file file-msg)
284 (delete-file (concat file-msg ".el"))
287 (set-buffer buffer-index)
288 (save-buffer smtpmail-queue-index)
289 (kill-buffer buffer-index)
292 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
294 (defun smtpmail-fqdn ()
295 (if smtpmail-local-domain
296 (concat (system-name) "." smtpmail-local-domain)
299 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
301 (host smtpmail-smtp-server)
302 (port smtpmail-smtp-service)
306 (supported-extensions '()))
309 ;; get or create the trace buffer
311 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
313 ;; clear the trace buffer of old output
315 (set-buffer process-buffer)
318 ;; open the connection to the server
319 (setq process (open-network-stream "SMTP" process-buffer host port))
320 (and (null process) (throw 'done nil))
322 ;; set the send-filter
323 (set-process-filter process 'smtpmail-process-filter)
326 (set-buffer process-buffer)
327 (make-local-variable 'smtpmail-read-point)
328 (setq smtpmail-read-point (point-min))
331 (if (or (null (car (setq greeting (smtpmail-read-response process))))
332 (not (integerp (car greeting)))
333 (>= (car greeting) 400))
338 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
340 (if (or (null (car (setq response-code (smtpmail-read-response process))))
341 (not (integerp (car response-code)))
342 (>= (car response-code) 400))
345 (smtpmail-send-command process (format "HELO %s" (smtpmail-fqdn)))
347 (if (or (null (car (setq response-code (smtpmail-read-response process))))
348 (not (integerp (car response-code)))
349 (>= (car response-code) 400))
351 (let ((extension-lines (cdr (cdr response-code))))
352 (while extension-lines
353 (let ((name (intern (downcase (substring (car extension-lines) 4)))))
355 (cond ((memq name '(verb xvrb 8bitmime onex xone
358 (setq supported-extensions
359 (cons name supported-extensions)))
360 (t (message "unknown extension %s"
362 (setq extension-lines (cdr extension-lines)))))
364 (if (or (member 'onex supported-extensions)
365 (member 'xone supported-extensions))
367 (smtpmail-send-command process (format "ONEX"))
368 (if (or (null (car (setq response-code (smtpmail-read-response process))))
369 (not (integerp (car response-code)))
370 (>= (car response-code) 400))
373 (if (and smtpmail-debug-info
374 (or (member 'verb supported-extensions)
375 (member 'xvrb supported-extensions)))
377 (smtpmail-send-command process (format "VERB"))
378 (if (or (null (car (setq response-code (smtpmail-read-response process))))
379 (not (integerp (car response-code)))
380 (>= (car response-code) 400))
383 (if (member 'xusr supported-extensions)
385 (smtpmail-send-command process (format "XUSR"))
386 (if (or (null (car (setq response-code (smtpmail-read-response process))))
387 (not (integerp (car response-code)))
388 (>= (car response-code) 400))
391 ;; MAIL FROM: <sender>
393 (if (member 'size supported-extensions)
396 (set-buffer smtpmail-text-buffer)
398 (+ (- (point-max) (point-min))
399 ;; Add one byte for each change-of-line
400 ;; because or CR-LF representation:
401 (count-lines (point-min) (point-max))
402 ;; For some reason, an empty line is
403 ;; added to the message. Maybe this
404 ;; is a bug, but it can't hurt to add
405 ;; those two bytes anyway:
409 (if (member '8bitmime supported-extensions)
411 ;; Code should be added here that transforms
412 ;; the contents of the message buffer into
413 ;; something the receiving SMTP can handle.
414 ;; For a receiver that supports 8BITMIME, this
415 ;; may mean converting BINARY to BASE64, or
416 ;; adding Content-Transfer-Encoding and the
417 ;; other MIME headers. The code should also
418 ;; return an indication of what encoding the
419 ;; message buffer is now, i.e. ASCII or
425 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
426 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
431 (if (or (null (car (setq response-code (smtpmail-read-response process))))
432 (not (integerp (car response-code)))
433 (>= (car response-code) 400))
437 ;; RCPT TO: <recipient>
439 (while (not (null (nth n recipient)))
440 (smtpmail-send-command process (format "RCPT TO: <%s>" (nth n recipient)))
443 (setq response-code (smtpmail-read-response process))
444 (if (or (null (car response-code))
445 (not (integerp (car response-code)))
446 (>= (car response-code) 400))
452 (smtpmail-send-command process "DATA")
454 (if (or (null (car (setq response-code (smtpmail-read-response process))))
455 (not (integerp (car response-code)))
456 (>= (car response-code) 400))
461 (smtpmail-send-data process smtpmail-text-buffer)
464 (smtpmail-send-command process ".")
466 (if (or (null (car (setq response-code (smtpmail-read-response process))))
467 (not (integerp (car response-code)))
468 (>= (car response-code) 400))
473 ; (smtpmail-send-command process "QUIT")
474 ; (and (null (car (smtpmail-read-response process)))
479 (set-buffer (process-buffer process))
480 (smtpmail-send-command process "QUIT")
481 (smtpmail-read-response process)
483 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
484 ; (not (integerp (car response-code)))
485 ; (>= (car response-code) 400))
488 (delete-process process))))))
491 (defun smtpmail-process-filter (process output)
493 (set-buffer (process-buffer process))
494 (goto-char (point-max))
497 (defun smtpmail-read-response (process)
498 (let ((case-fold-search nil)
499 (response-strings nil)
500 (response-continue t)
501 (return-value '(nil ()))
504 (while response-continue
505 (goto-char smtpmail-read-point)
506 (while (not (search-forward "\r\n" nil t))
507 (accept-process-output process)
508 (goto-char smtpmail-read-point))
510 (setq match-end (point))
511 (setq response-strings
512 (cons (buffer-substring smtpmail-read-point (- match-end 2))
515 (goto-char smtpmail-read-point)
516 (if (looking-at "[0-9]+ ")
517 (let ((begin (match-beginning 0))
519 (if smtpmail-debug-info
520 (message "%s" (car response-strings)))
522 (setq smtpmail-read-point match-end)
524 ;; ignore lines that start with "0"
525 (if (looking-at "0[0-9]+ ")
527 (setq response-continue nil)
530 (buffer-substring begin end))
531 (nreverse response-strings)))))
533 (if (looking-at "[0-9]+-")
534 (progn (if smtpmail-debug-info
535 (message "%s" (car response-strings)))
536 (setq smtpmail-read-point match-end)
537 (setq response-continue t))
539 (setq smtpmail-read-point match-end)
540 (setq response-continue nil)
542 (cons nil (nreverse response-strings)))
545 (setq smtpmail-read-point match-end)
549 (defun smtpmail-send-command (process command)
550 (goto-char (point-max))
551 (if (= (aref command 0) ?P)
552 (insert "PASS <omitted>\r\n")
553 (insert command "\r\n"))
554 (setq smtpmail-read-point (point))
555 (process-send-string process command)
556 (process-send-string process "\r\n"))
558 (defun smtpmail-send-data-1 (process data)
559 (goto-char (point-max))
561 (if (not (null smtpmail-code-conv-from))
562 (setq data (code-convert-string data smtpmail-code-conv-from *internal*)))
564 (if smtpmail-debug-info
565 (insert data "\r\n"))
567 (setq smtpmail-read-point (point))
568 ;; Escape "." at start of a line
569 (if (eq (string-to-char data) ?.)
570 (process-send-string process "."))
571 (process-send-string process data)
572 (process-send-string process "\r\n")
575 (defun smtpmail-send-data (process buffer)
584 (goto-char (point-min)))
590 (setq this-line (point))
592 (setq this-line-end (point))
593 (setq sending-data nil)
594 (setq sending-data (buffer-substring this-line this-line-end))
595 (if (/= (forward-line 1) 0)
596 (setq data-continue nil)))
598 (smtpmail-send-data-1 process sending-data)
604 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
605 "Get address list suitable for smtp RCPT TO: <address>."
606 (require 'mail-utils) ;; pick up mail-strip-quoted-names
608 ((case-fold-search t)
609 (simple-address-list "")
617 (set-buffer smtpmail-address-buffer) (erase-buffer)
618 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
619 (goto-char (point-min))
620 ;; RESENT-* fields should stop processing of regular fields.
622 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" header-end t)
623 (setq addr-regexp "^Resent-\\(to\\|cc\\|bcc\\):")
624 (setq addr-regexp "^\\(To:\\|Cc:\\|Bcc:\\)")))
626 (while (re-search-forward addr-regexp header-end t)
628 (setq this-line (match-beginning 0))
630 ;; get any continuation lines
631 (while (and (looking-at "^[ \t]+") (< (point) header-end))
633 (setq this-line-end (point-marker))
634 (setq simple-address-list
635 (concat simple-address-list " "
636 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
640 (insert-string simple-address-list)
642 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
643 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
644 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
646 (goto-char (point-min))
647 ;; tidyness in case hook is not robust when it looks at this
648 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
650 (goto-char (point-min))
651 (let (recipient-address-list)
652 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
654 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
655 recipient-address-list))
657 (setq smtpmail-recipient-address-list recipient-address-list))
665 (defun smtpmail-do-bcc (header-end)
666 "Delete [Resent-]BCC: and their continuation lines from the header area.
667 There may be multiple BCC: lines, and each may have arbitrarily
668 many continuation lines."
669 (let ((case-fold-search t))
670 (save-excursion (goto-char (point-min))
671 ;; iterate over all BCC: lines
672 (while (re-search-forward "^\(RESENT-\)?BCC:" header-end t)
673 (delete-region (match-beginning 0) (progn (forward-line 1) (point)))
674 ;; get rid of any continuation lines
675 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
686 ;;; smtpmail.el ends here