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) or use customize.
33 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
34 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use `message'
35 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
36 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
37 ;;(setq smtpmail-debug-info t) ; only to debug problems
39 ;; To queue mail, set smtpmail-queue-mail to t and use
40 ;; smtpmail-send-queued-mail to send.
49 (defgroup smtpmail nil
50 "SMTP protocol for sending mail."
54 (defcustom smtpmail-default-smtp-server nil
55 "*Specify default SMTP server."
56 :type
'(choice (const nil
) string
)
59 (defcustom smtpmail-smtp-server
60 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server
)
61 "*The name of the host running SMTP server."
62 :type
'(choice (const nil
) string
)
65 (defcustom smtpmail-smtp-service
25
66 "*SMTP service port number. smtp or 25 ."
70 (defcustom smtpmail-local-domain nil
71 "*Local domain name without a host name.
72 If the function (system-name) returns the full internet address,
73 don't define this value."
74 :type
'(choice (const nil
) string
)
77 (defcustom smtpmail-debug-info nil
78 "*smtpmail debug info printout. messages and process buffer."
82 (defcustom smtpmail-code-conv-from nil
;; *junet*
83 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
87 (defcustom smtpmail-queue-mail nil
88 "*Specify if mail is queued (if t) or sent immediately (if nil).
89 If queued, it is stored in the directory `smtpmail-queue-dir'
90 and sent with `smtpmail-send-queued-mail'."
94 (defcustom smtpmail-queue-dir
"~/Mail/queued-mail/"
95 "*Directory where `smtpmail.el' stores queued mail."
99 (defvar smtpmail-queue-index-file
"index"
100 "File name of queued mail index,
101 This is relative to `smtpmail-queue-dir'.")
103 (defvar smtpmail-queue-index
(concat smtpmail-queue-dir
104 smtpmail-queue-index-file
))
111 (defun smtpmail-send-it ()
112 (require 'mail-utils
)
113 (let ((errbuf (if mail-interactive
114 (generate-new-buffer " smtpmail errors")
116 (tembuf (generate-new-buffer " smtpmail temp"))
117 (case-fold-search nil
)
119 (mailbuf (current-buffer)))
124 (insert-buffer-substring mailbuf
)
125 (goto-char (point-max))
126 ;; require one newline at the end.
127 (or (= (preceding-char) ?
\n)
129 ;; Change header-delimiter to be what sendmail expects.
130 (mail-sendmail-undelimit-header)
131 (setq delimline
(point-marker))
132 ;; (sendmail-synch-aliases)
134 (expand-mail-aliases (point-min) delimline
))
135 (goto-char (point-min))
136 ;; ignore any blank lines in the header
137 (while (and (re-search-forward "\n\n\n*" delimline t
)
138 (< (point) delimline
))
139 (replace-match "\n"))
140 (let ((case-fold-search t
))
141 ;; We used to process Resent-... headers here,
142 ;; but it was not done properly, and the job
143 ;; is done correctly in smtpmail-deduce-address-list.
144 ;; Don't send out a blank subject line
145 (goto-char (point-min))
146 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t
)
148 ;; This one matches a Subject just before the header delimiter.
149 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t
)
150 (= (match-end 0) delimline
))
152 ;; Put the "From:" field in unless for some odd reason
153 ;; they put one in themselves.
154 (goto-char (point-min))
155 (if (not (re-search-forward "^From:" delimline t
))
156 (let* ((login user-mail-address
)
157 (fullname (user-full-name)))
158 (cond ((eq mail-from-style
'angles
)
159 (insert "From: " fullname
)
160 (let ((fullname-start (+ (point-min) 6))
161 (fullname-end (point-marker)))
162 (goto-char fullname-start
)
163 ;; Look for a character that cannot appear unquoted
164 ;; according to RFC 822.
165 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
168 ;; Quote fullname, escaping specials.
169 (goto-char fullname-start
)
171 (while (re-search-forward "[\"\\]"
173 (replace-match "\\\\\\&" t
))
175 (insert " <" login
">\n"))
176 ((eq mail-from-style
'parens
)
177 (insert "From: " login
" (")
178 (let ((fullname-start (point)))
180 (let ((fullname-end (point-marker)))
181 (goto-char fullname-start
)
182 ;; RFC 822 says \ and nonmatching parentheses
183 ;; must be escaped in comments.
184 ;; Escape every instance of ()\ ...
185 (while (re-search-forward "[()\\]" fullname-end
1)
186 (replace-match "\\\\\\&" t
))
187 ;; ... then undo escaping of matching parentheses,
188 ;; including matching nested parentheses.
189 (goto-char fullname-start
)
190 (while (re-search-forward
191 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
193 (replace-match "\\1(\\3)" t
)
194 (goto-char fullname-start
))))
196 ((null mail-from-style
)
197 (insert "From: " login
"\n")))))
198 ;; Insert an extra newline if we need it to work around
199 ;; Sun's bug that swallows newlines.
200 (goto-char (1+ delimline
))
201 (if (eval mail-mailer-swallows-blank-line
)
203 ;; Find and handle any FCC fields.
204 (goto-char (point-min))
205 (if (re-search-forward "^FCC:" delimline t
)
206 (mail-do-fcc delimline
))
214 (setq smtpmail-address-buffer
(generate-new-buffer "*smtp-mail*"))
215 (setq smtpmail-recipient-address-list
216 (smtpmail-deduce-address-list tembuf
(point-min) delimline
))
217 (kill-buffer smtpmail-address-buffer
)
219 (smtpmail-do-bcc delimline
)
221 (if (not smtpmail-queue-mail
)
222 (if (not (null smtpmail-recipient-address-list
))
223 (if (not (smtpmail-via-smtp
224 smtpmail-recipient-address-list tembuf
))
225 (error "Sending failed; SMTP protocol error"))
226 (error "Sending failed; no recipients"))
227 (let* ((file-data (concat
229 (concat (time-stamp-yyyy-mm-dd)
230 "_" (time-stamp-hh:mm
:ss
))))
231 (file-elisp (concat file-data
".el"))
232 (buffer-data (create-file-buffer file-data
))
233 (buffer-elisp (create-file-buffer file-elisp
))
234 (buffer-scratch "*queue-mail*"))
236 (set-buffer buffer-data
)
238 (insert-buffer tembuf
)
239 (write-file file-data
)
240 (set-buffer buffer-elisp
)
243 "(setq smtpmail-recipient-address-list '"
244 (prin1-to-string smtpmail-recipient-address-list
)
246 (write-file file-elisp
)
247 (set-buffer (generate-new-buffer buffer-scratch
))
248 (insert (concat file-data
"\n"))
249 (append-to-file (point-min)
251 smtpmail-queue-index
)
253 (kill-buffer buffer-scratch
)
254 (kill-buffer buffer-data
)
255 (kill-buffer buffer-elisp
))))
258 (kill-buffer errbuf
)))))
260 (defun smtpmail-send-queued-mail ()
261 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
263 ;;; Get index, get first mail, send it, get second mail, etc...
264 (let ((buffer-index (find-file-noselect smtpmail-queue-index
))
268 (set-buffer buffer-index
)
269 (beginning-of-buffer)
271 (setq file-msg
(buffer-substring (point) (save-excursion
275 (setq tembuf
(find-file-noselect file-msg
))
276 (if (not (null smtpmail-recipient-address-list
))
277 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
279 (error "Sending failed; SMTP protocol error"))
280 (error "Sending failed; no recipients"))
281 (delete-file file-msg
)
282 (delete-file (concat file-msg
".el"))
285 (set-buffer buffer-index
)
286 (save-buffer smtpmail-queue-index
)
287 (kill-buffer buffer-index
)
290 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
292 (defun smtpmail-fqdn ()
293 (if smtpmail-local-domain
294 (concat (system-name) "." smtpmail-local-domain
)
297 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
)
299 (host (or smtpmail-smtp-server
300 (error "`smtpmail-smtp-server' not defined")))
301 (port smtpmail-smtp-service
)
305 (supported-extensions '()))
308 ;; get or create the trace buffer
310 (get-buffer-create (format "*trace of SMTP session to %s*" host
)))
312 ;; clear the trace buffer of old output
314 (set-buffer process-buffer
)
317 ;; open the connection to the server
318 (setq process
(open-network-stream "SMTP" process-buffer host port
))
319 (and (null process
) (throw 'done nil
))
321 ;; set the send-filter
322 (set-process-filter process
'smtpmail-process-filter
)
325 (set-buffer process-buffer
)
326 (set-buffer-process-coding-system 'raw-text-unix
'raw-text-unix
)
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 (car (split-string (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