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 (goto-char (point-min))
134 (concat "^" (regexp-quote mail-header-separator
) "\n"))
137 (setq delimline
(point-marker))
138 ;; (sendmail-synch-aliases)
140 (expand-mail-aliases (point-min) delimline
))
141 (goto-char (point-min))
142 ;; ignore any blank lines in the header
143 (while (and (re-search-forward "\n\n\n*" delimline t
)
144 (< (point) delimline
))
145 (replace-match "\n"))
146 (let ((case-fold-search t
))
147 ;; We used to process Resent-... headers here,
148 ;; but it was not done properly, and the job
149 ;; is done correctly in smtpmail-deduce-address-list.
150 ;; Don't send out a blank subject line
151 (goto-char (point-min))
152 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t
)
154 ;; This one matches a Subject just before the header delimiter.
155 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t
)
156 (= (match-end 0) delimline
))
158 ;; Put the "From:" field in unless for some odd reason
159 ;; they put one in themselves.
160 (goto-char (point-min))
161 (if (not (re-search-forward "^From:" delimline t
))
162 (let* ((login user-mail-address
)
163 (fullname (user-full-name)))
164 (cond ((eq mail-from-style
'angles
)
165 (insert "From: " fullname
)
166 (let ((fullname-start (+ (point-min) 6))
167 (fullname-end (point-marker)))
168 (goto-char fullname-start
)
169 ;; Look for a character that cannot appear unquoted
170 ;; according to RFC 822.
171 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
174 ;; Quote fullname, escaping specials.
175 (goto-char fullname-start
)
177 (while (re-search-forward "[\"\\]"
179 (replace-match "\\\\\\&" t
))
181 (insert " <" login
">\n"))
182 ((eq mail-from-style
'parens
)
183 (insert "From: " login
" (")
184 (let ((fullname-start (point)))
186 (let ((fullname-end (point-marker)))
187 (goto-char fullname-start
)
188 ;; RFC 822 says \ and nonmatching parentheses
189 ;; must be escaped in comments.
190 ;; Escape every instance of ()\ ...
191 (while (re-search-forward "[()\\]" fullname-end
1)
192 (replace-match "\\\\\\&" t
))
193 ;; ... then undo escaping of matching parentheses,
194 ;; including matching nested parentheses.
195 (goto-char fullname-start
)
196 (while (re-search-forward
197 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
199 (replace-match "\\1(\\3)" t
)
200 (goto-char fullname-start
))))
202 ((null mail-from-style
)
203 (insert "From: " login
"\n")))))
204 ;; Insert an extra newline if we need it to work around
205 ;; Sun's bug that swallows newlines.
206 (goto-char (1+ delimline
))
207 (if (eval mail-mailer-swallows-blank-line
)
209 ;; Find and handle any FCC fields.
210 (goto-char (point-min))
211 (if (re-search-forward "^FCC:" delimline t
)
212 (mail-do-fcc delimline
))
220 (setq smtpmail-address-buffer
(generate-new-buffer "*smtp-mail*"))
221 (setq smtpmail-recipient-address-list
222 (smtpmail-deduce-address-list tembuf
(point-min) delimline
))
223 (kill-buffer smtpmail-address-buffer
)
225 (smtpmail-do-bcc delimline
)
227 (if (not smtpmail-queue-mail
)
228 (if (not (null smtpmail-recipient-address-list
))
229 (if (not (smtpmail-via-smtp
230 smtpmail-recipient-address-list tembuf
))
231 (error "Sending failed; SMTP protocol error"))
232 (error "Sending failed; no recipients"))
233 (let* ((file-data (concat
235 (concat (time-stamp-yyyy-mm-dd)
236 "_" (time-stamp-hh:mm
:ss
))))
237 (file-elisp (concat file-data
".el"))
238 (buffer-data (create-file-buffer file-data
))
239 (buffer-elisp (create-file-buffer file-elisp
))
240 (buffer-scratch "*queue-mail*"))
242 (set-buffer buffer-data
)
244 (insert-buffer tembuf
)
245 (write-file file-data
)
246 (set-buffer buffer-elisp
)
249 "(setq smtpmail-recipient-address-list '"
250 (prin1-to-string smtpmail-recipient-address-list
)
252 (write-file file-elisp
)
253 (set-buffer (generate-new-buffer buffer-scratch
))
254 (insert (concat file-data
"\n"))
255 (append-to-file (point-min)
257 smtpmail-queue-index
)
259 (kill-buffer buffer-scratch
)
260 (kill-buffer buffer-data
)
261 (kill-buffer buffer-elisp
))))
264 (kill-buffer errbuf
)))))
266 (defun smtpmail-send-queued-mail ()
267 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
269 ;;; Get index, get first mail, send it, get second mail, etc...
270 (let ((buffer-index (find-file-noselect smtpmail-queue-index
))
274 (set-buffer buffer-index
)
275 (beginning-of-buffer)
277 (setq file-msg
(buffer-substring (point) (save-excursion
281 (setq tembuf
(find-file-noselect file-msg
))
282 (if (not (null smtpmail-recipient-address-list
))
283 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
285 (error "Sending failed; SMTP protocol error"))
286 (error "Sending failed; no recipients"))
287 (delete-file file-msg
)
288 (delete-file (concat file-msg
".el"))
291 (set-buffer buffer-index
)
292 (save-buffer smtpmail-queue-index
)
293 (kill-buffer buffer-index
)
296 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
298 (defun smtpmail-fqdn ()
299 (if smtpmail-local-domain
300 (concat (system-name) "." smtpmail-local-domain
)
303 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
)
305 (host smtpmail-smtp-server
)
306 (port smtpmail-smtp-service
)
310 (supported-extensions '()))
313 ;; get or create the trace buffer
315 (get-buffer-create (format "*trace of SMTP session to %s*" host
)))
317 ;; clear the trace buffer of old output
319 (set-buffer process-buffer
)
322 ;; open the connection to the server
323 (setq process
(open-network-stream "SMTP" process-buffer host port
))
324 (and (null process
) (throw 'done nil
))
326 ;; set the send-filter
327 (set-process-filter process
'smtpmail-process-filter
)
330 (set-buffer process-buffer
)
331 (make-local-variable 'smtpmail-read-point
)
332 (setq smtpmail-read-point
(point-min))
335 (if (or (null (car (setq greeting
(smtpmail-read-response process
))))
336 (not (integerp (car greeting
)))
337 (>= (car greeting
) 400))
342 (smtpmail-send-command process
(format "EHLO %s" (smtpmail-fqdn)))
344 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
345 (not (integerp (car response-code
)))
346 (>= (car response-code
) 400))
349 (smtpmail-send-command process
(format "HELO %s" (smtpmail-fqdn)))
351 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
352 (not (integerp (car response-code
)))
353 (>= (car response-code
) 400))
355 (let ((extension-lines (cdr (cdr response-code
))))
356 (while extension-lines
357 (let ((name (intern (downcase (substring (car extension-lines
) 4)))))
359 (cond ((memq name
'(verb xvrb
8bitmime onex xone
362 (setq supported-extensions
363 (cons name supported-extensions
)))
364 (t (message "unknown extension %s"
366 (setq extension-lines
(cdr extension-lines
)))))
368 (if (or (member 'onex supported-extensions
)
369 (member 'xone supported-extensions
))
371 (smtpmail-send-command process
(format "ONEX"))
372 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
373 (not (integerp (car response-code
)))
374 (>= (car response-code
) 400))
377 (if (and smtpmail-debug-info
378 (or (member 'verb supported-extensions
)
379 (member 'xvrb supported-extensions
)))
381 (smtpmail-send-command process
(format "VERB"))
382 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
383 (not (integerp (car response-code
)))
384 (>= (car response-code
) 400))
387 (if (member 'xusr supported-extensions
)
389 (smtpmail-send-command process
(format "XUSR"))
390 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
391 (not (integerp (car response-code
)))
392 (>= (car response-code
) 400))
395 ;; MAIL FROM: <sender>
397 (if (member 'size supported-extensions
)
400 (set-buffer smtpmail-text-buffer
)
402 (+ (- (point-max) (point-min))
403 ;; Add one byte for each change-of-line
404 ;; because or CR-LF representation:
405 (count-lines (point-min) (point-max))
406 ;; For some reason, an empty line is
407 ;; added to the message. Maybe this
408 ;; is a bug, but it can't hurt to add
409 ;; those two bytes anyway:
413 (if (member '8bitmime supported-extensions
)
415 ;; Code should be added here that transforms
416 ;; the contents of the message buffer into
417 ;; something the receiving SMTP can handle.
418 ;; For a receiver that supports 8BITMIME, this
419 ;; may mean converting BINARY to BASE64, or
420 ;; adding Content-Transfer-Encoding and the
421 ;; other MIME headers. The code should also
422 ;; return an indication of what encoding the
423 ;; message buffer is now, i.e. ASCII or
429 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
430 (smtpmail-send-command process
(format "MAIL FROM: <%s>%s%s"
435 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
436 (not (integerp (car response-code
)))
437 (>= (car response-code
) 400))
441 ;; RCPT TO: <recipient>
443 (while (not (null (nth n recipient
)))
444 (smtpmail-send-command process
(format "RCPT TO: <%s>" (nth n recipient
)))
447 (setq response-code
(smtpmail-read-response process
))
448 (if (or (null (car response-code
))
449 (not (integerp (car response-code
)))
450 (>= (car response-code
) 400))
456 (smtpmail-send-command process
"DATA")
458 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
459 (not (integerp (car response-code
)))
460 (>= (car response-code
) 400))
465 (smtpmail-send-data process smtpmail-text-buffer
)
468 (smtpmail-send-command process
".")
470 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
471 (not (integerp (car response-code
)))
472 (>= (car response-code
) 400))
477 ; (smtpmail-send-command process "QUIT")
478 ; (and (null (car (smtpmail-read-response process)))
483 (set-buffer (process-buffer process
))
484 (smtpmail-send-command process
"QUIT")
485 (smtpmail-read-response process
)
487 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
488 ; (not (integerp (car response-code)))
489 ; (>= (car response-code) 400))
492 (delete-process process
))))))
495 (defun smtpmail-process-filter (process output
)
497 (set-buffer (process-buffer process
))
498 (goto-char (point-max))
501 (defun smtpmail-read-response (process)
502 (let ((case-fold-search nil
)
503 (response-strings nil
)
504 (response-continue t
)
505 (return-value '(nil ()))
508 (while response-continue
509 (goto-char smtpmail-read-point
)
510 (while (not (search-forward "\r\n" nil t
))
511 (accept-process-output process
)
512 (goto-char smtpmail-read-point
))
514 (setq match-end
(point))
515 (setq response-strings
516 (cons (buffer-substring smtpmail-read-point
(- match-end
2))
519 (goto-char smtpmail-read-point
)
520 (if (looking-at "[0-9]+ ")
521 (let ((begin (match-beginning 0))
523 (if smtpmail-debug-info
524 (message "%s" (car response-strings
)))
526 (setq smtpmail-read-point match-end
)
528 ;; ignore lines that start with "0"
529 (if (looking-at "0[0-9]+ ")
531 (setq response-continue nil
)
534 (buffer-substring begin end
))
535 (nreverse response-strings
)))))
537 (if (looking-at "[0-9]+-")
538 (progn (if smtpmail-debug-info
539 (message "%s" (car response-strings
)))
540 (setq smtpmail-read-point match-end
)
541 (setq response-continue t
))
543 (setq smtpmail-read-point match-end
)
544 (setq response-continue nil
)
546 (cons nil
(nreverse response-strings
)))
549 (setq smtpmail-read-point match-end
)
553 (defun smtpmail-send-command (process command
)
554 (goto-char (point-max))
555 (if (= (aref command
0) ?P
)
556 (insert "PASS <omitted>\r\n")
557 (insert command
"\r\n"))
558 (setq smtpmail-read-point
(point))
559 (process-send-string process command
)
560 (process-send-string process
"\r\n"))
562 (defun smtpmail-send-data-1 (process data
)
563 (goto-char (point-max))
565 (if (not (null smtpmail-code-conv-from
))
566 (setq data
(code-convert-string data smtpmail-code-conv-from
*internal
*)))
568 (if smtpmail-debug-info
569 (insert data
"\r\n"))
571 (setq smtpmail-read-point
(point))
572 ;; Escape "." at start of a line
573 (if (eq (string-to-char data
) ?.
)
574 (process-send-string process
"."))
575 (process-send-string process data
)
576 (process-send-string process
"\r\n")
579 (defun smtpmail-send-data (process buffer
)
588 (goto-char (point-min)))
594 (setq this-line
(point))
596 (setq this-line-end
(point))
597 (setq sending-data nil
)
598 (setq sending-data
(buffer-substring this-line this-line-end
))
599 (if (/= (forward-line 1) 0)
600 (setq data-continue nil
)))
602 (smtpmail-send-data-1 process sending-data
)
608 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end
)
609 "Get address list suitable for smtp RCPT TO: <address>."
610 (require 'mail-utils
) ;; pick up mail-strip-quoted-names
612 ((case-fold-search t
)
613 (simple-address-list "")
621 (set-buffer smtpmail-address-buffer
) (erase-buffer)
622 (insert-buffer-substring smtpmail-text-buffer header-start header-end
)
623 (goto-char (point-min))
624 ;; RESENT-* fields should stop processing of regular fields.
626 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" header-end t
)
627 (setq addr-regexp
"^Resent-\\(to\\|cc\\|bcc\\):")
628 (setq addr-regexp
"^\\(To:\\|Cc:\\|Bcc:\\)")))
630 (while (re-search-forward addr-regexp header-end t
)
632 (setq this-line
(match-beginning 0))
634 ;; get any continuation lines
635 (while (and (looking-at "^[ \t]+") (< (point) header-end
))
637 (setq this-line-end
(point-marker))
638 (setq simple-address-list
639 (concat simple-address-list
" "
640 (mail-strip-quoted-names (buffer-substring this-line this-line-end
))))
644 (insert-string simple-address-list
)
646 (subst-char-in-region (point-min) (point-max) 10 ? t
);; newline --> blank
647 (subst-char-in-region (point-min) (point-max) ?
, ? t
);; comma --> blank
648 (subst-char-in-region (point-min) (point-max) 9 ? t
);; tab --> blank
650 (goto-char (point-min))
651 ;; tidyness in case hook is not robust when it looks at this
652 (while (re-search-forward "[ \t]+" header-end t
) (replace-match " "))
654 (goto-char (point-min))
655 (let (recipient-address-list)
656 (while (re-search-forward " \\([^ ]+\\) " (point-max) t
)
658 (setq recipient-address-list
(cons (buffer-substring (match-beginning 1) (match-end 1))
659 recipient-address-list
))
661 (setq smtpmail-recipient-address-list recipient-address-list
))
669 (defun smtpmail-do-bcc (header-end)
670 "Delete [Resent-]BCC: and their continuation lines from the header area.
671 There may be multiple BCC: lines, and each may have arbitrarily
672 many continuation lines."
673 (let ((case-fold-search t
))
674 (save-excursion (goto-char (point-min))
675 ;; iterate over all BCC: lines
676 (while (re-search-forward "^\(RESENT-\)?BCC:" header-end t
)
677 (delete-region (match-beginning 0) (progn (forward-line 1) (point)))
678 ;; get rid of any continuation lines
679 (while (and (looking-at "^[ \t].*\n") (< (point) header-end
))
690 ;;; smtpmail.el ends here