1 ;;; sendmail.el --- mail sending commands for Emacs.
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This mode provides mail-sending facilities from within Emacs. It is
27 ;; documented in the Emacs user's manual.
32 (defvar mail-from-style 'angles "\
33 *Specifies how \"From:\" fields look.
35 If `nil', they contain just the return address like:
37 If `parens', they look like:
38 king@grassland.com (Elvis Parsley)
39 If `angles', they look like:
40 Elvis Parsley <king@grassland.com>")
43 (defvar mail-self-blind nil "\
44 Non-nil means insert BCC to self in messages to be sent.
45 This is done when the message is initialized,
46 so you can remove or alter the BCC field to override the default.")
49 (defvar mail-interactive nil "\
50 Non-nil means when sending a message wait for and display errors.
51 nil means let mailer mail back a message to report errors.")
54 (defvar mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
55 Delete these headers from old message when it's inserted in a reply.")
57 ;; Useful to set in site-init.el
59 (defvar send-mail-function 'sendmail-send-it "\
60 Function to call to send the current buffer as mail.
61 The headers should be delimited by a line whose contents
62 match the variable `mail-header-separator'.")
65 (defvar mail-header-separator "--text follows this line--" "\
66 *Line used to separate headers from text in messages being composed.")
69 (defvar mail-archive-file-name nil "\
70 *Name of file to write all outgoing messages in, or nil for none.
71 This can be an inbox file or an Rmail file.")
74 (defvar mail-default-reply-to nil
75 "*Address to insert as default Reply-to field of outgoing messages.
76 If nil, it will be initialized from the REPLYTO environment variable
77 when you first send mail.")
80 (defvar mail-alias-file nil
81 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
82 This file defines aliases to be expanded by the mailer; this is a different
83 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
84 This variable has no effect unless your system uses sendmail as its mailer.")
87 (defvar mail-personal-alias-file "~/.mailrc"
88 "*If non-nil, the name of the user's personal mail alias file.
89 This file typically should be in same format as the `.mailrc' file used by
90 the `Mail' or `mailx' program.
91 This file need not actually exist.")
93 (defvar mail-setup-hook nil
94 "Normal hook, run each time a new outgoing mail message is initialized.
95 The function `mail-setup' runs this hook.")
97 (defvar mail-aliases t
98 "Alist of mail address aliases,
99 or t meaning should be initialized from your mail aliases file.
100 \(The file's name is normally `~/.mailrc', but your MAILRC environment
101 variable can override that name.)
102 The alias definitions in the file have this form:
103 alias ALIAS MEANING")
105 (defvar mail-alias-modtime nil
106 "The modification time of your mail alias file when it was last examined.")
108 (defvar mail-yank-prefix nil
109 "*Prefix insert on lines of yanked message being replied to.
110 nil means use indentation.")
111 (defvar mail-indentation-spaces 3
112 "*Number of spaces to insert at the beginning of each cited line.
113 Used by `mail-yank-original' via `mail-yank-cite'.")
114 (defvar mail-yank-hooks nil
115 "Obsolete hook for modifying a citation just inserted in the mail buffer.
116 Each hook function can find the citation between (point) and (mark t).
117 And each hook function should leave point and mark around the citation
120 This is a normal hook, misnamed for historical reasons.
121 It is semi-obsolete and mail agents should no longer use it.")
123 (defvar mail-citation-hook nil
124 "*Hook for modifying a citation just inserted in the mail buffer.
125 Each hook function can find the citation between (point) and (mark t).
126 And each hook function should leave point and mark around the citation
129 If this hook is entirely empty (nil), a default action is taken
130 instead of no action.")
132 (defvar mail-abbrevs-loaded nil)
133 (defvar mail-mode-map nil)
135 (autoload 'build-mail-aliases "mailalias"
136 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
139 (autoload 'expand-mail-aliases "mailalias"
140 "Expand all mail aliases in suitable header fields found between BEG and END.
141 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
142 Optional second arg EXCLUDE may be a regular expression defining text to be
143 removed from alias expansions."
147 (defvar mail-signature nil
148 "*Text inserted at end of mail buffer when a message is initialized.
149 If t, it means to insert the contents of the file `mail-signature-file'.")
151 (defvar mail-signature-file "~/.signature"
152 "*File containing the text inserted at end of mail buffer.")
154 (defvar mail-reply-buffer nil)
155 (defvar mail-send-actions nil
156 "A list of actions to be performed upon successful sending of a message.")
158 (defvar mail-default-headers nil
159 "*A string containing header lines, to be inserted in outgoing messages.
160 It is inserted before you edit the message,
161 so you can edit or delete these lines.")
163 (defvar mail-bury-selects-summary t
164 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
165 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
166 the RMAIL summary buffer before returning, if it exists and this variable
169 ;; Note: could use /usr/ucb/mail instead of sendmail;
170 ;; options -t, and -v if not interactive.
171 (defvar mail-mailer-swallows-blank-line
172 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
173 (file-readable-p "/etc/sendmail.cf")
174 (let ((buffer (get-buffer-create " *temp*")))
178 (insert-file-contents "/etc/sendmail.cf")
179 (goto-char (point-min))
180 (let ((case-fold-search nil))
181 (re-search-forward "^OR\\>" nil t)))
182 (kill-buffer buffer))))
183 ;; According to RFC822, "The field-name must be composed of printable
184 ;; ASCII characters (i.e. characters that have decimal values between
185 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
187 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
188 "Set this non-nil if the system's mailer runs the header and body together.
189 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
190 The value should be an expression to test whether the problem will
193 (defvar mail-mode-syntax-table nil
194 "Syntax table used while in mail mode.")
196 (if (not mail-mode-syntax-table)
198 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
199 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
201 (defvar mail-font-lock-keywords
202 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
203 (list '("^To:" . font-lock-function-name-face)
204 '("^B?CC:\\|^Reply-To:" . font-lock-keyword-face)
205 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
206 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
207 (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
208 1 'font-lock-comment-face)
209 (cons (concat "^[ \t]*"
210 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
212 'font-lock-reference-face)
213 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
214 . font-lock-string-face)))
215 "Additional expressions to highlight in Mail mode.")
217 (defvar mail-send-hook nil
218 "Normal hook run before sending mail, in Mail mode.")
220 (defun sendmail-synch-aliases ()
221 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
222 (or (equal mail-alias-modtime modtime)
223 (setq mail-alias-modtime modtime
226 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
227 (or mail-default-reply-to
228 (setq mail-default-reply-to (getenv "REPLYTO")))
229 (sendmail-synch-aliases)
230 (if (eq mail-aliases t)
232 (setq mail-aliases nil)
233 (if (file-exists-p mail-personal-alias-file)
234 (build-mail-aliases))))
235 (setq mail-send-actions actions)
236 (setq mail-reply-buffer replybuffer)
237 (goto-char (point-min))
241 ;; Here removed code to extract names from within <...>
242 ;; on the assumption that mail-strip-quoted-names
243 ;; has been called and has done so.
244 (let ((fill-prefix "\t")
245 (address-start (point)))
247 (fill-region-as-paragraph address-start (point-max)))
250 (let ((fill-prefix "\t")
251 (address-start (progn (insert "CC: ") (point))))
253 (fill-region-as-paragraph address-start (point-max))))
255 (let ((fill-prefix "\t")
257 (address-start (point)))
258 (insert "In-reply-to: " in-reply-to "\n")
259 (fill-region-as-paragraph address-start (point-max))))
260 (insert "Subject: " (or subject "") "\n")
261 (if mail-default-headers
262 (insert mail-default-headers))
263 (if mail-default-reply-to
264 (insert "Reply-to: " mail-default-reply-to "\n"))
266 (insert "BCC: " (user-login-name) "\n"))
267 (if mail-archive-file-name
268 (insert "FCC: " mail-archive-file-name "\n"))
269 (insert mail-header-separator "\n")
270 ;; Insert the signature. But remember the beginning of the message.
271 (if to (setq to (point)))
272 (cond ((eq mail-signature t)
273 (if (file-exists-p mail-signature-file)
276 (insert-file-contents mail-signature-file))))
278 (insert mail-signature)))
279 (goto-char (point-max))
280 (or (bolp) (newline)))
281 (if to (goto-char to))
282 (or to subject in-reply-to
283 (set-buffer-modified-p nil))
284 (run-hooks 'mail-setup-hook))
288 "Major mode for editing mail to be sent.
289 Like Text Mode but with these additional commands:
290 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
291 C-c C-f move to a header field (and create it if there isn't):
292 C-c C-f C-t move to To: C-c C-f C-s move to Subject:
293 C-c C-f C-c move to CC: C-c C-f C-b move to BCC:
294 C-c C-f C-f move to FCC:
295 C-c C-t mail-text (move to beginning of message text).
296 C-c C-w mail-signature (insert `mail-signature-file' file).
297 C-c C-y mail-yank-original (insert current message, in Rmail).
298 C-c C-q mail-fill-yanked-message (fill what was yanked).
299 C-c C-v mail-sent-via (add a Sent-via field for each To or CC)."
301 (kill-all-local-variables)
302 (make-local-variable 'mail-reply-buffer)
303 (setq mail-reply-buffer nil)
304 (make-local-variable 'mail-send-actions)
305 (set-syntax-table mail-mode-syntax-table)
306 (use-local-map mail-mode-map)
307 (setq local-abbrev-table text-mode-abbrev-table)
308 (setq major-mode 'mail-mode)
309 (setq mode-name "Mail")
310 (setq buffer-offer-save t)
311 (make-local-variable 'font-lock-defaults)
312 (setq font-lock-defaults '(mail-font-lock-keywords t))
313 (make-local-variable 'paragraph-separate)
314 (make-local-variable 'paragraph-start)
315 (setq paragraph-start (concat (regexp-quote mail-header-separator)
316 "$\\|[ \t]*[-_][-_][-_]+$\\|"
318 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
319 "$\\|[ \t]*[-_][-_][-_]+$\\|"
321 (run-hooks 'text-mode-hook 'mail-mode-hook))
327 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
328 (define-key mail-mode-map "\C-c?" 'describe-mode)
329 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
330 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
331 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
332 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
333 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
334 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
335 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
336 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
337 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
338 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
339 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
340 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
341 (define-key mail-mode-map "\C-c\C-s" 'mail-send))
343 (define-key mail-mode-map [menu-bar mail]
344 (cons "Mail" (make-sparse-keymap "Mail")))
346 (define-key mail-mode-map [menu-bar mail fill]
347 '("Fill Citation" . mail-fill-yanked-message))
349 (define-key mail-mode-map [menu-bar mail yank]
350 '("Cite Original" . mail-yank-original))
352 (define-key mail-mode-map [menu-bar mail signature]
353 '("Insert Signature" . mail-signature))
355 (define-key mail-mode-map [menu-bar mail cancel]
356 '("Cancel" . mail-dont-send))
358 (define-key mail-mode-map [menu-bar mail send-stay]
359 '("Send, Keep Editing" . mail-send))
361 (define-key mail-mode-map [menu-bar mail send]
362 '("Send Message" . mail-send-and-exit))
364 (define-key mail-mode-map [menu-bar headers]
365 (cons "Headers" (make-sparse-keymap "Move to Header")))
367 (define-key mail-mode-map [menu-bar headers reply-to]
368 '("Reply-To" . mail-reply-to))
370 (define-key mail-mode-map [menu-bar headers sent-via]
371 '("Sent Via" . mail-sent-via))
373 (define-key mail-mode-map [menu-bar headers text]
374 '("Text" . mail-text))
376 (define-key mail-mode-map [menu-bar headers bcc]
379 (define-key mail-mode-map [menu-bar headers fcc]
382 (define-key mail-mode-map [menu-bar headers cc]
385 (define-key mail-mode-map [menu-bar headers subject]
386 '("Subject" . mail-subject))
388 (define-key mail-mode-map [menu-bar headers to]
391 (defun mail-send-and-exit (arg)
392 "Send message like `mail-send', then, if no errors, exit from mail buffer.
393 Prefix arg means don't delete this window."
398 (defun mail-dont-send (arg)
399 "Don't send the message you have been editing.
400 Prefix arg means don't delete this window."
404 (defun mail-bury (arg)
405 "Bury this mail buffer."
406 (let ((newbuf (other-buffer (current-buffer))))
407 (bury-buffer (current-buffer))
408 (if (and (fboundp 'frame-parameters)
409 (cdr (assq 'dedicated (frame-parameters)))
410 (not (null (delq (selected-frame) (visible-frame-list)))))
411 (delete-frame (selected-frame))
412 (let (rmail-flag summary-buffer)
416 (set-buffer (window-buffer (next-window (selected-window) 'not)))
417 (setq rmail-flag (eq major-mode 'rmail-mode))
419 (and mail-bury-selects-summary
420 (boundp 'rmail-summary-buffer)
422 (buffer-name rmail-summary-buffer)
423 (not (get-buffer-window rmail-summary-buffer))
424 rmail-summary-buffer))))
426 ;; If the Rmail buffer has a summary, show that.
427 (if summary-buffer (switch-to-buffer summary-buffer)
429 (switch-to-buffer newbuf))))))
432 "Send the message in the current buffer.
433 If `mail-interactive' is non-nil, wait for success indication
434 or error messages, and inform user.
435 Otherwise any failure is reported in a message back to
436 the user from the mailer."
438 (if (if buffer-file-name
439 (y-or-n-p "Send buffer contents as mail message? ")
440 (or (buffer-modified-p)
441 (y-or-n-p "Message already sent; resend? ")))
443 (run-hooks 'mail-send-hook)
444 (message "Sending...")
445 (funcall send-mail-function)
446 ;; Now perform actions on successful sending.
447 (while mail-send-actions
449 (apply (car (car mail-send-actions))
450 (cdr (car mail-send-actions)))
452 (setq mail-send-actions (cdr mail-send-actions)))
453 (message "Sending...done")
454 ;; If buffer has no file, mark it as unmodified and delete autosave.
455 (if (not buffer-file-name)
457 (set-buffer-modified-p nil)
458 (delete-auto-save-file-if-necessary t))))))
460 (defun sendmail-send-it ()
461 (require 'mail-utils)
462 (let ((errbuf (if mail-interactive
463 (generate-new-buffer " sendmail errors")
465 (tembuf (generate-new-buffer " sendmail temp"))
466 (case-fold-search nil)
469 (mailbuf (current-buffer)))
474 (insert-buffer-substring mailbuf)
475 (goto-char (point-max))
476 ;; require one newline at the end.
477 (or (= (preceding-char) ?\n)
479 ;; Change header-delimiter to be what sendmail expects.
480 (goto-char (point-min))
482 (concat "^" (regexp-quote mail-header-separator) "\n"))
485 (setq delimline (point-marker))
486 (sendmail-synch-aliases)
488 (expand-mail-aliases (point-min) delimline))
489 (goto-char (point-min))
490 ;; ignore any blank lines in the header
491 (while (and (re-search-forward "\n\n\n*" delimline t)
492 (< (point) delimline))
493 (replace-match "\n"))
494 (let ((case-fold-search t))
495 (goto-char (point-min))
496 (while (re-search-forward "^Resent-to:" delimline t)
497 (setq resend-to-addresses
499 (narrow-to-region (point)
503 (append (mail-parse-comma-list)
504 resend-to-addresses))))
505 ;;; Apparently this causes a duplicate Sender.
506 ;;; ;; If the From is different than current user, insert Sender.
507 ;;; (goto-char (point-min))
508 ;;; (and (re-search-forward "^From:" delimline t)
510 ;;; (require 'mail-utils)
511 ;;; (not (string-equal
512 ;;; (mail-strip-quoted-names
513 ;;; (save-restriction
514 ;;; (narrow-to-region (point-min) delimline)
515 ;;; (mail-fetch-field "From")))
516 ;;; (user-login-name))))
519 ;;; (insert "Sender: " (user-login-name) "\n")))
520 ;; Don't send out a blank subject line
521 (goto-char (point-min))
522 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
524 ;; Put the "From:" field in unless for some odd reason
525 ;; they put one in themselves.
526 (goto-char (point-min))
527 (if (not (re-search-forward "^From:" delimline t))
528 (let* ((login user-mail-address)
529 (fullname (user-full-name)))
530 (cond ((eq mail-from-style 'angles)
531 (insert "From: " fullname)
532 (let ((fullname-start (+ (point-min) 6))
533 (fullname-end (point-marker)))
534 (goto-char fullname-start)
535 ;; Look for a character that cannot appear unquoted
536 ;; according to RFC 822.
537 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
540 ;; Quote fullname, escaping specials.
541 (goto-char fullname-start)
543 (while (re-search-forward "[\"\\]"
545 (replace-match "\\\\\\&" t))
547 (insert " <" login ">\n"))
548 ((eq mail-from-style 'parens)
549 (insert "From: " login " (")
550 (let ((fullname-start (point)))
552 (let ((fullname-end (point-marker)))
553 (goto-char fullname-start)
554 ;; RFC 822 says \ and nonmatching parentheses
555 ;; must be escaped in comments.
556 ;; Escape every instance of ()\ ...
557 (while (re-search-forward "[()\\]" fullname-end 1)
558 (replace-match "\\\\\\&" t))
559 ;; ... then undo escaping of matching parentheses,
560 ;; including matching nested parentheses.
561 (goto-char fullname-start)
562 (while (re-search-forward
563 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
565 (replace-match "\\1(\\3)" t)
566 (goto-char fullname-start))))
568 ((null mail-from-style)
569 (insert "From: " login "\n")))))
570 ;; Insert an extra newline if we need it to work around
571 ;; Sun's bug that swallows newlines.
572 (goto-char (1+ delimline))
573 (if (eval mail-mailer-swallows-blank-line)
575 ;; Find and handle any FCC fields.
576 (goto-char (point-min))
577 (if (re-search-forward "^FCC:" delimline t)
578 (mail-do-fcc delimline))
583 (let ((default-directory "/"))
584 (apply 'call-process-region
585 (append (list (point-min) (point-max)
586 (if (boundp 'sendmail-program)
589 nil errbuf nil "-oi")
590 ;; Always specify who from,
591 ;; since some systems have broken sendmails.
592 (list "-f" (user-login-name))
593 ;;; ;; Don't say "from root" if running under su.
594 ;;; (and (equal (user-real-login-name) "root")
595 ;;; (list "-f" (user-login-name)))
597 (list (concat "-oA" mail-alias-file)))
598 ;; These mean "report errors by mail"
599 ;; and "deliver in background".
600 (if (null mail-interactive) '("-oem" "-odb"))
601 ;; Get the addresses from the message
602 ;; unless this is a resend.
603 ;; We must not do that for a resend
604 ;; because we would find the original addresses.
605 ;; For a resend, include the specific addresses.
606 (or resend-to-addresses
611 (goto-char (point-min))
612 (while (re-search-forward "\n\n* *" nil t)
613 (replace-match "; "))
614 (if (not (zerop (buffer-size)))
615 (error "Sending...failed to %s"
616 (buffer-substring (point-min) (point-max)))))))
619 (kill-buffer errbuf)))))
621 (defun mail-do-fcc (header-end)
623 (rmailbuf (current-buffer))
624 (time (current-time))
625 (tembuf (generate-new-buffer " rmail output"))
626 (case-fold-search t))
628 (goto-char (point-min))
629 (while (re-search-forward "^FCC:[ \t]*" header-end t)
630 (setq fcc-list (cons (buffer-substring (point)
633 (skip-chars-backward " \t")
636 (delete-region (match-beginning 0)
637 (progn (forward-line 1) (point))))
640 ;; This initial newline is written out if the fcc file already exists.
641 (insert "\nFrom " (user-login-name) " "
642 (current-time-string time) "\n")
643 ;; Insert the time zone before the year.
646 (require 'mail-utils)
647 (insert (mail-rfc822-time-zone time) " ")
648 (goto-char (point-max))
649 (insert-buffer-substring rmailbuf)
650 ;; Make sure messages are separated.
651 (goto-char (point-max))
654 ;; ``Quote'' "^From " as ">From "
655 ;; (note that this isn't really quoting, as there is no requirement
656 ;; that "^[>]+From " be quoted in the same transparent way.)
657 (let ((case-fold-search nil))
658 (while (search-forward "\nFrom " nil t)
662 (let* ((buffer (find-buffer-visiting (car fcc-list)))
663 (curbuf (current-buffer))
664 (beg (point-min)) (end (point-max))
665 (beg2 (save-excursion (goto-char (point-min))
666 (forward-line 2) (point))))
668 ;; File is present in a buffer => append to that buffer.
671 ;; Keep the end of the accessible portion at the same place
672 ;; unless it is the end of the buffer.
673 (let ((max (if (/= (1+ (buffer-size)) (point-max))
676 ;; Code below lifted from rmailout.el
677 ;; function rmail-output-to-rmail-file:
678 (let ((buffer-read-only nil)
679 (msg (and (boundp 'rmail-current-message)
680 rmail-current-message)))
681 ;; If MSG is non-nil, buffer is in RMAIL mode.
684 (rmail-maybe-set-message-counters)
686 (narrow-to-region (point-max) (point-max))
687 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
688 "Date: " (mail-rfc822-date) "\n")
689 (insert-buffer-substring curbuf beg2 end)
691 (goto-char (point-min))
693 (search-backward "\n\^_")
694 (narrow-to-region (point) (point-max))
695 (rmail-count-new-messages t)
696 (rmail-show-message msg)
698 ;; Output file not in rmail mode
699 ;; => just insert at the end.
700 (narrow-to-region (point-min) (1+ (buffer-size)))
701 (goto-char (point-max))
702 (insert-buffer-substring curbuf beg end)))
703 (if max (narrow-to-region (point-min) max)))))
704 ;; Else append to the file directly.
705 (if (and (file-exists-p (car fcc-list))
706 (mail-file-babyl-p (car fcc-list)))
707 ;; If the file is a Babyl file,
708 ;; convert the message to Babyl format.
710 (set-buffer (get-buffer-create " mail-temp"))
711 (setq buffer-read-only nil)
713 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
714 "Date: " (mail-rfc822-date) "\n")
715 (insert-buffer-substring curbuf beg2 end)
717 (write-region (point-min) (point-max) (car fcc-list) t)
720 (1+ (point-min)) (point-max) (car fcc-list) t))))
721 (setq fcc-list (cdr fcc-list))))
722 (kill-buffer tembuf)))
724 (defun mail-sent-via ()
725 "Make a Sent-via header line from each To or CC header line."
728 (goto-char (point-min))
729 ;; find the header-separator
730 (search-forward (concat "\n" mail-header-separator "\n"))
732 ;; put a marker at the end of the header
733 (let ((end (point-marker))
736 (goto-char (point-min))
737 ;; search for the To: lines and make Sent-via: lines from them
738 ;; search for the next To: line
739 (while (re-search-forward "^\\(to\\|cc\\):" end t)
740 ;; Grab this line plus all its continuations, sans the `to:'.
742 (buffer-substring (point)
744 (if (re-search-forward "^[^ \t\n]" end t)
748 ;; Insert a copy, with altered header field name.
749 (insert-before-markers "Sent-via:" to-line))))))
752 "Move point to end of To-field."
755 (mail-position-on-field "To"))
757 (defun mail-subject ()
758 "Move point to end of Subject-field."
761 (mail-position-on-field "Subject"))
764 "Move point to end of CC-field. Create a CC field if none."
767 (or (mail-position-on-field "cc" t)
768 (progn (mail-position-on-field "to")
772 "Move point to end of BCC-field. Create a BCC field if none."
775 (or (mail-position-on-field "bcc" t)
776 (progn (mail-position-on-field "to")
777 (insert "\nBCC: "))))
779 (defun mail-fcc (folder)
780 "Add a new FCC field, with file name completion."
781 (interactive "FFolder carbon copy: ")
783 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
784 (mail-position-on-field "to"))
785 (insert "\nFCC: " folder))
787 (defun mail-reply-to ()
788 "Move point to end of Reply-To-field."
791 (mail-position-on-field "Reply-To"))
793 (defun mail-position-on-field (field &optional soft)
795 (case-fold-search t))
796 (goto-char (point-min))
797 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
798 (setq end (match-beginning 0))
799 (goto-char (point-min))
800 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
802 (re-search-forward "^[^ \t]" nil 'move)
804 (skip-chars-backward "\n")
807 (progn (goto-char end)
808 (insert field ": \n")
809 (skip-chars-backward "\n")))
813 "Move point to beginning of message text."
816 (goto-char (point-min))
817 (search-forward (concat "\n" mail-header-separator "\n")))
819 (defun mail-signature (atpoint)
820 "Sign letter with contents of the file `mail-signature-file'.
821 Prefix arg means put contents at point."
825 (goto-char (point-max)))
826 (skip-chars-backward " \t\n")
829 (delete-region (point) (point-max)))
831 (insert-file-contents (expand-file-name mail-signature-file))))
833 (defun mail-fill-yanked-message (&optional justifyp)
834 "Fill the paragraphs of a message yanked into this one.
835 Numeric argument means justify as well."
838 (goto-char (point-min))
839 (search-forward (concat "\n" mail-header-separator "\n") nil t)
840 (fill-individual-paragraphs (point)
845 (defun mail-indent-citation ()
846 "Modify text just inserted from a message to be cited.
847 The inserted text should be the region.
848 When this function returns, the region is again around the modified text.
850 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
851 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
852 (let ((start (point)))
853 (mail-yank-clear-headers start (mark t))
854 (if (null mail-yank-prefix)
855 (indent-rigidly start (mark t) mail-indentation-spaces)
858 (while (< (point) (mark t))
859 (insert mail-yank-prefix)
860 (forward-line 1))))))
862 (defun mail-yank-original (arg)
863 "Insert the message being replied to, if any (in rmail).
864 Puts point before the text and mark after.
865 Normally, indents each nonblank line ARG spaces (default 3).
866 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
868 Just \\[universal-argument] as argument means don't indent, insert no prefix,
869 and don't delete any header fields."
871 (if mail-reply-buffer
872 (let ((start (point)))
873 ;; If the original message is in another window in the same frame,
874 ;; delete that window to save screen space.
875 ;; t means don't alter other frames.
876 (delete-windows-on mail-reply-buffer t)
877 (insert-buffer mail-reply-buffer)
881 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
882 mail-indentation-spaces)))
883 (if mail-citation-hook
884 (run-hooks 'mail-citation-hook)
886 (run-hooks 'mail-yank-hooks)
887 (mail-indent-citation)))))
888 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
889 ;; It is cleaner to avoid activation, even though the command
890 ;; loop would deactivate the mark because we inserted text.
891 (goto-char (prog1 (mark t)
892 (set-marker (mark-marker) (point) (current-buffer))))
893 (if (not (eolp)) (insert ?\n)))))
895 (defun mail-yank-clear-headers (start end)
896 (if mail-yank-ignored-headers
899 (if (search-forward "\n\n" end t)
901 (narrow-to-region start (point))
903 (while (let ((case-fold-search t))
904 (re-search-forward mail-yank-ignored-headers nil t))
906 (delete-region (point)
907 (progn (re-search-forward "\n[^ \t]")
911 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
914 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
915 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
916 When this function returns, the buffer `*mail*' is selected.
917 The value is t if the message was newly initialized; otherwise, nil.
919 Optionally, the signature file `mail-signature-file' can be inserted at the
920 end; see the variable `mail-signature'.
923 While editing message, type \\[mail-send-and-exit] to send the message and exit.
925 Various special commands starting with C-c are available in sendmail mode
926 to move to message header fields:
929 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
930 when the message is initialized.
932 If `mail-default-reply-to' is non-nil, it should be an address (a string);
933 a Reply-to: field with that address is inserted.
935 If `mail-archive-file-name' is non-nil, an FCC field with that file name
938 The normal hook `mail-setup-hook' is run after the message is
939 initialized. It can add more default fields to the message.
941 When calling from a program, the first argument if non-nil says
942 not to erase the existing contents of the `*mail*' buffer.
944 The second through fifth arguments,
945 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
946 the initial contents of those header fields.
947 These arguments should not have final newlines.
948 The sixth argument REPLYBUFFER is a buffer whose contents
949 should be yanked if the user types C-c C-y.
950 The seventh argument ACTIONS is a list of actions to take
951 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
952 when the message is sent, we apply FUNCTION to ARGS.
953 This is how Rmail arranges to mark messages `answered'."
955 ;;; This is commented out because I found it was confusing in practice.
956 ;;; It is easy enough to rename *mail* by hand with rename-buffer
957 ;;; if you want to have multiple mail buffers.
958 ;;; And then you can control which messages to save. --rms.
961 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
964 ;;; (while (and (setq buffer
965 ;;; (get-buffer (if (= 1 index) "*mail*"
966 ;;; (format "*mail*<%d>" index))))
967 ;;; (not (buffer-modified-p buffer)))
968 ;;; (setq index (1+ index)))
969 ;;; (if buffer (switch-to-buffer buffer)
970 ;;; ;; If none exists, start a new message.
971 ;;; ;; This will never re-use an existing unmodified mail buffer
972 ;;; ;; (since index is not 1 anymore). Perhaps it should.
973 ;;; (setq noerase nil))))
974 ;;; ;; Unless we found a modified message and are happy, start a new message.
975 ;;; (if (not noerase)
977 ;;; ;; Look for existing unmodified mail buffer.
978 ;;; (while (and (setq buffer
979 ;;; (get-buffer (if (= 1 index) "*mail*"
980 ;;; (format "*mail*<%d>" index))))
981 ;;; (buffer-modified-p buffer))
982 ;;; (setq index (1+ index)))
983 ;;; ;; If none, make a new one.
985 ;;; (setq buffer (generate-new-buffer "*mail*")))
986 ;;; ;; Go there and initialize it.
987 ;;; (switch-to-buffer buffer)
989 ;;; (setq default-directory (expand-file-name "~/"))
990 ;;; (auto-save-mode auto-save-default)
992 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
993 ;;; (if (and buffer-auto-save-file-name
994 ;;; (file-exists-p buffer-auto-save-file-name))
995 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
997 (pop-to-buffer "*mail*")
998 (auto-save-mode auto-save-default)
1000 ;; Disconnect the buffer from its visited file
1001 ;; (in case the user has actually visited a file *mail*).
1002 ; (set-visited-file-name nil)
1005 (or (not (buffer-modified-p))
1006 (y-or-n-p "Unsent message being composed; erase it? "))
1007 (progn (erase-buffer)
1008 (mail-setup to subject in-reply-to cc replybuffer actions)
1009 (setq initialized t)))
1010 (if (and buffer-auto-save-file-name
1011 (file-exists-p buffer-auto-save-file-name))
1012 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1015 (defun mail-recover ()
1016 "Reread contents of current buffer from its last auto-save file."
1018 (let ((file-name (make-auto-save-file-name)))
1019 (cond ((save-window-excursion
1020 (if (not (eq system-type 'vax-vms))
1021 (with-output-to-temp-buffer "*Directory*"
1022 (buffer-disable-undo standard-output)
1023 (let ((default-directory "/"))
1025 "ls" nil standard-output nil "-l" file-name))))
1026 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1027 (let ((buffer-read-only nil))
1029 (insert-file-contents file-name nil)))
1030 (t (error "mail-recover cancelled")))))
1033 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1034 "Like `mail' command, but display mail buffer in another window."
1036 (let ((pop-up-windows t)
1037 (special-display-buffer-names nil)
1038 (special-display-regexps nil)
1039 (same-window-buffer-names nil)
1040 (same-window-regexps nil))
1041 (pop-to-buffer "*mail*"))
1042 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1045 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1046 "Like `mail' command, but display mail buffer in another frame."
1048 (let ((pop-up-frames t)
1049 (special-display-buffer-names nil)
1050 (special-display-regexps nil)
1051 (same-window-buffer-names nil)
1052 (same-window-regexps nil))
1053 (pop-to-buffer "*mail*"))
1054 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1056 ;;; Do not execute these when sendmail.el is loaded,
1057 ;;; only in loaddefs.el.
1058 ;;;###autoload (define-key ctl-x-map "m" 'mail)
1059 ;;;###autoload (define-key ctl-x-4-map "m" 'mail-other-window)
1060 ;;;###autoload (define-key ctl-x-5-map "m" 'mail-other-frame)
1062 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1064 ;;; Do not add anything but external entries on this page.
1068 ;;; sendmail.el ends here