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.")
68 ;; Set up mail-header-separator for use as a category text property.
69 (put 'mail-header-separator 'rear-nonsticky '(category))
70 (put 'mail-header-separator 'read-only t)
73 (defvar mail-archive-file-name nil "\
74 *Name of file to write all outgoing messages in, or nil for none.
75 This can be an inbox file or an Rmail file.")
78 (defvar mail-default-reply-to nil
79 "*Address to insert as default Reply-to field of outgoing messages.
80 If nil, it will be initialized from the REPLYTO environment variable
81 when you first send mail.")
84 (defvar mail-alias-file nil
85 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
86 This file defines aliases to be expanded by the mailer; this is a different
87 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
88 This variable has no effect unless your system uses sendmail as its mailer.")
91 (defvar mail-personal-alias-file "~/.mailrc"
92 "*If non-nil, the name of the user's personal mail alias file.
93 This file typically should be in same format as the `.mailrc' file used by
94 the `Mail' or `mailx' program.
95 This file need not actually exist.")
97 (defvar mail-setup-hook nil
98 "Normal hook, run each time a new outgoing mail message is initialized.
99 The function `mail-setup' runs this hook.")
101 (defvar mail-aliases t
102 "Alist of mail address aliases,
103 or t meaning should be initialized from your mail aliases file.
104 \(The file's name is normally `~/.mailrc', but your MAILRC environment
105 variable can override that name.)
106 The alias definitions in the file have this form:
107 alias ALIAS MEANING")
109 (defvar mail-alias-modtime nil
110 "The modification time of your mail alias file when it was last examined.")
112 (defvar mail-yank-prefix nil
113 "*Prefix insert on lines of yanked message being replied to.
114 nil means use indentation.")
115 (defvar mail-indentation-spaces 3
116 "*Number of spaces to insert at the beginning of each cited line.
117 Used by `mail-yank-original' via `mail-yank-cite'.")
118 (defvar mail-yank-hooks nil
119 "Obsolete hook for modifying a citation just inserted in the mail buffer.
120 Each hook function can find the citation between (point) and (mark t).
121 And each hook function should leave point and mark around the citation
124 This is a normal hook, misnamed for historical reasons.
125 It is semi-obsolete and mail agents should no longer use it.")
127 (defvar mail-citation-hook nil
128 "*Hook for modifying a citation just inserted in the mail buffer.
129 Each hook function can find the citation between (point) and (mark t).
130 And each hook function should leave point and mark around the citation
133 If this hook is entirely empty (nil), a default action is taken
134 instead of no action.")
136 (defvar mail-abbrevs-loaded nil)
137 (defvar mail-mode-map nil)
139 (autoload 'build-mail-aliases "mailalias"
140 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
143 (autoload 'expand-mail-aliases "mailalias"
144 "Expand all mail aliases in suitable header fields found between BEG and END.
145 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
146 Optional second arg EXCLUDE may be a regular expression defining text to be
147 removed from alias expansions."
151 (defvar mail-signature nil
152 "*Text inserted at end of mail buffer when a message is initialized.
153 If t, it means to insert the contents of the file `mail-signature-file'.")
155 (defvar mail-signature-file "~/.signature"
156 "*File containing the text inserted at end of mail buffer.")
158 (defvar mail-reply-buffer nil)
159 (defvar mail-send-actions nil
160 "A list of actions to be performed upon successful sending of a message.")
162 (defvar mail-default-headers nil
163 "*A string containing header lines, to be inserted in outgoing messages.
164 It is inserted before you edit the message,
165 so you can edit or delete these lines.")
167 (defvar mail-bury-selects-summary t
168 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
169 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
170 the RMAIL summary buffer before returning, if it exists and this variable
173 ;; Note: could use /usr/ucb/mail instead of sendmail;
174 ;; options -t, and -v if not interactive.
175 (defvar mail-mailer-swallows-blank-line
176 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
177 (file-readable-p "/etc/sendmail.cf")
178 (let ((buffer (get-buffer-create " *temp*")))
182 (insert-file-contents "/etc/sendmail.cf")
183 (goto-char (point-min))
184 (let ((case-fold-search nil))
185 (re-search-forward "^OR\\>" nil t)))
186 (kill-buffer buffer))))
187 ;; According to RFC822, "The field-name must be composed of printable
188 ;; ASCII characters (i.e. characters that have decimal values between
189 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
191 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
192 "Set this non-nil if the system's mailer runs the header and body together.
193 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
194 The value should be an expression to test whether the problem will
197 (defvar mail-mode-syntax-table nil
198 "Syntax table used while in mail mode.")
200 (if (not mail-mode-syntax-table)
202 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
203 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
205 (defvar mail-font-lock-keywords
206 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
207 (list '("^To:" . font-lock-function-name-face)
208 '("^B?CC:\\|^Reply-To:" . font-lock-keyword-face)
209 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
210 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
211 (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
212 1 'font-lock-comment-face)
213 (cons (concat "^[ \t]*"
214 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
216 'font-lock-reference-face)
217 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
218 . font-lock-string-face)))
219 "Additional expressions to highlight in Mail mode.")
221 (defvar mail-send-hook nil
222 "Normal hook run before sending mail, in Mail mode.")
224 (defun sendmail-synch-aliases ()
225 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
226 (or (equal mail-alias-modtime modtime)
227 (setq mail-alias-modtime modtime
230 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
231 (or mail-default-reply-to
232 (setq mail-default-reply-to (getenv "REPLYTO")))
233 (sendmail-synch-aliases)
234 (if (eq mail-aliases t)
236 (setq mail-aliases nil)
237 (if (file-exists-p mail-personal-alias-file)
238 (build-mail-aliases))))
239 (setq mail-send-actions actions)
240 (setq mail-reply-buffer replybuffer)
241 (goto-char (point-min))
245 ;; Here removed code to extract names from within <...>
246 ;; on the assumption that mail-strip-quoted-names
247 ;; has been called and has done so.
248 (let ((fill-prefix "\t")
249 (address-start (point)))
251 (fill-region-as-paragraph address-start (point-max)))
254 (let ((fill-prefix "\t")
255 (address-start (progn (insert "CC: ") (point))))
257 (fill-region-as-paragraph address-start (point-max))))
259 (let ((fill-prefix "\t")
261 (address-start (point)))
262 (insert "In-reply-to: " in-reply-to "\n")
263 (fill-region-as-paragraph address-start (point-max))))
264 (insert "Subject: " (or subject "") "\n")
265 (if mail-default-headers
266 (insert mail-default-headers))
267 (if mail-default-reply-to
268 (insert "Reply-to: " mail-default-reply-to "\n"))
270 (insert "BCC: " (user-login-name) "\n"))
271 (if mail-archive-file-name
272 (insert "FCC: " mail-archive-file-name "\n"))
273 (put-text-property (point)
275 (insert mail-header-separator "\n")
277 'category 'mail-header-separator)
278 ;; Insert the signature. But remember the beginning of the message.
279 (if to (setq to (point)))
280 (cond ((eq mail-signature t)
281 (if (file-exists-p mail-signature-file)
284 (insert-file-contents mail-signature-file))))
286 (insert mail-signature)))
287 (goto-char (point-max))
288 (or (bolp) (newline)))
289 (if to (goto-char to))
290 (or to subject in-reply-to
291 (set-buffer-modified-p nil))
292 (run-hooks 'mail-setup-hook))
296 "Major mode for editing mail to be sent.
297 Like Text Mode but with these additional commands:
298 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
299 C-c C-f move to a header field (and create it if there isn't):
300 C-c C-f C-t move to To: C-c C-f C-s move to Subject:
301 C-c C-f C-c move to CC: C-c C-f C-b move to BCC:
302 C-c C-f C-f move to FCC:
303 C-c C-t mail-text (move to beginning of message text).
304 C-c C-w mail-signature (insert `mail-signature-file' file).
305 C-c C-y mail-yank-original (insert current message, in Rmail).
306 C-c C-q mail-fill-yanked-message (fill what was yanked).
307 C-c C-v mail-sent-via (add a Sent-via field for each To or CC)."
309 (kill-all-local-variables)
310 (make-local-variable 'mail-reply-buffer)
311 (setq mail-reply-buffer nil)
312 (make-local-variable 'mail-send-actions)
313 (set-syntax-table mail-mode-syntax-table)
314 (use-local-map mail-mode-map)
315 (setq local-abbrev-table text-mode-abbrev-table)
316 (setq major-mode 'mail-mode)
317 (setq mode-name "Mail")
318 (setq buffer-offer-save t)
319 (make-local-variable 'font-lock-defaults)
320 (setq font-lock-defaults '(mail-font-lock-keywords t))
321 (make-local-variable 'paragraph-separate)
322 (make-local-variable 'paragraph-start)
323 (setq paragraph-start (concat (regexp-quote mail-header-separator)
324 "$\\|[ \t]*[-_][-_][-_]+$\\|"
326 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
327 "$\\|[ \t]*[-_][-_][-_]+$\\|"
329 (run-hooks 'text-mode-hook 'mail-mode-hook))
335 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
336 (define-key mail-mode-map "\C-c?" 'describe-mode)
337 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
338 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
339 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
340 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
341 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
342 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
343 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
344 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
345 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
346 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
347 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
348 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
349 (define-key mail-mode-map "\C-c\C-s" 'mail-send))
351 (define-key mail-mode-map [menu-bar mail]
352 (cons "Mail" (make-sparse-keymap "Mail")))
354 (define-key mail-mode-map [menu-bar mail fill]
355 '("Fill Citation" . mail-fill-yanked-message))
357 (define-key mail-mode-map [menu-bar mail yank]
358 '("Cite Original" . mail-yank-original))
360 (define-key mail-mode-map [menu-bar mail signature]
361 '("Insert Signature" . mail-signature))
363 (define-key mail-mode-map [menu-bar mail cancel]
364 '("Cancel" . mail-dont-send))
366 (define-key mail-mode-map [menu-bar mail send-stay]
367 '("Send, Keep Editing" . mail-send))
369 (define-key mail-mode-map [menu-bar mail send]
370 '("Send Message" . mail-send-and-exit))
372 (define-key mail-mode-map [menu-bar headers]
373 (cons "Headers" (make-sparse-keymap "Move to Header")))
375 (define-key mail-mode-map [menu-bar headers reply-to]
376 '("Reply-To" . mail-reply-to))
378 (define-key mail-mode-map [menu-bar headers sent-via]
379 '("Sent Via" . mail-sent-via))
381 (define-key mail-mode-map [menu-bar headers text]
382 '("Text" . mail-text))
384 (define-key mail-mode-map [menu-bar headers bcc]
387 (define-key mail-mode-map [menu-bar headers fcc]
390 (define-key mail-mode-map [menu-bar headers cc]
393 (define-key mail-mode-map [menu-bar headers subject]
394 '("Subject" . mail-subject))
396 (define-key mail-mode-map [menu-bar headers to]
399 (defun mail-send-and-exit (arg)
400 "Send message like `mail-send', then, if no errors, exit from mail buffer.
401 Prefix arg means don't delete this window."
406 (defun mail-dont-send (arg)
407 "Don't send the message you have been editing.
408 Prefix arg means don't delete this window."
412 (defun mail-bury (arg)
413 "Bury this mail buffer."
414 (let ((newbuf (other-buffer (current-buffer))))
415 (bury-buffer (current-buffer))
416 (if (and (window-dedicated-p (frame-selected-window))
417 (not (null (delq (selected-frame) (visible-frame-list)))))
418 (delete-frame (selected-frame))
419 (let (rmail-flag summary-buffer)
423 (set-buffer (window-buffer (next-window (selected-window) 'not)))
424 (setq rmail-flag (eq major-mode 'rmail-mode))
426 (and mail-bury-selects-summary
427 (boundp 'rmail-summary-buffer)
429 (buffer-name rmail-summary-buffer)
430 (not (get-buffer-window rmail-summary-buffer))
431 rmail-summary-buffer))))
433 ;; If the Rmail buffer has a summary, show that.
434 (if summary-buffer (switch-to-buffer summary-buffer)
436 (switch-to-buffer newbuf))))))
439 "Send the message in the current buffer.
440 If `mail-interactive' is non-nil, wait for success indication
441 or error messages, and inform user.
442 Otherwise any failure is reported in a message back to
443 the user from the mailer."
445 (if (if buffer-file-name
446 (y-or-n-p "Send buffer contents as mail message? ")
447 (or (buffer-modified-p)
448 (y-or-n-p "Message already sent; resend? ")))
449 (let ((inhibit-read-only t))
450 (run-hooks 'mail-send-hook)
451 (message "Sending...")
452 (funcall send-mail-function)
453 ;; Now perform actions on successful sending.
454 (while mail-send-actions
456 (apply (car (car mail-send-actions))
457 (cdr (car mail-send-actions)))
459 (setq mail-send-actions (cdr mail-send-actions)))
460 (message "Sending...done")
461 ;; If buffer has no file, mark it as unmodified and delete autosave.
462 (if (not buffer-file-name)
464 (set-buffer-modified-p nil)
465 (delete-auto-save-file-if-necessary t))))))
467 (defun sendmail-send-it ()
468 (require 'mail-utils)
469 (let ((errbuf (if mail-interactive
470 (generate-new-buffer " sendmail errors")
472 (tembuf (generate-new-buffer " sendmail temp"))
473 (case-fold-search nil)
476 (mailbuf (current-buffer)))
481 (insert-buffer-substring mailbuf)
482 (goto-char (point-max))
483 ;; require one newline at the end.
484 (or (= (preceding-char) ?\n)
486 ;; Change header-delimiter to be what sendmail expects.
487 (goto-char (point-min))
489 (concat "^" (regexp-quote mail-header-separator) "\n"))
492 (setq delimline (point-marker))
493 (sendmail-synch-aliases)
495 (expand-mail-aliases (point-min) delimline))
496 (goto-char (point-min))
497 ;; ignore any blank lines in the header
498 (while (and (re-search-forward "\n\n\n*" delimline t)
499 (< (point) delimline))
500 (replace-match "\n"))
501 (let ((case-fold-search t))
502 (goto-char (point-min))
503 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
504 (setq resend-to-addresses
506 (narrow-to-region (point)
510 (append (mail-parse-comma-list)
511 resend-to-addresses)))
512 ;; Delete Resent-BCC ourselves
513 (if (save-excursion (beginning-of-line)
514 (looking-at "resent-bcc"))
515 (delete-region (save-excursion (beginning-of-line) (point))
516 (save-excursion (end-of-line) (1+ (point))))))
517 ;;; Apparently this causes a duplicate Sender.
518 ;;; ;; If the From is different than current user, insert Sender.
519 ;;; (goto-char (point-min))
520 ;;; (and (re-search-forward "^From:" delimline t)
522 ;;; (require 'mail-utils)
523 ;;; (not (string-equal
524 ;;; (mail-strip-quoted-names
525 ;;; (save-restriction
526 ;;; (narrow-to-region (point-min) delimline)
527 ;;; (mail-fetch-field "From")))
528 ;;; (user-login-name))))
531 ;;; (insert "Sender: " (user-login-name) "\n")))
532 ;; Don't send out a blank subject line
533 (goto-char (point-min))
534 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
536 ;; Put the "From:" field in unless for some odd reason
537 ;; they put one in themselves.
538 (goto-char (point-min))
539 (if (not (re-search-forward "^From:" delimline t))
540 (let* ((login user-mail-address)
541 (fullname (user-full-name)))
542 (cond ((eq mail-from-style 'angles)
543 (insert "From: " fullname)
544 (let ((fullname-start (+ (point-min) 6))
545 (fullname-end (point-marker)))
546 (goto-char fullname-start)
547 ;; Look for a character that cannot appear unquoted
548 ;; according to RFC 822.
549 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
552 ;; Quote fullname, escaping specials.
553 (goto-char fullname-start)
555 (while (re-search-forward "[\"\\]"
557 (replace-match "\\\\\\&" t))
559 (insert " <" login ">\n"))
560 ((eq mail-from-style 'parens)
561 (insert "From: " login " (")
562 (let ((fullname-start (point)))
564 (let ((fullname-end (point-marker)))
565 (goto-char fullname-start)
566 ;; RFC 822 says \ and nonmatching parentheses
567 ;; must be escaped in comments.
568 ;; Escape every instance of ()\ ...
569 (while (re-search-forward "[()\\]" fullname-end 1)
570 (replace-match "\\\\\\&" t))
571 ;; ... then undo escaping of matching parentheses,
572 ;; including matching nested parentheses.
573 (goto-char fullname-start)
574 (while (re-search-forward
575 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
577 (replace-match "\\1(\\3)" t)
578 (goto-char fullname-start))))
580 ((null mail-from-style)
581 (insert "From: " login "\n")))))
582 ;; Insert an extra newline if we need it to work around
583 ;; Sun's bug that swallows newlines.
584 (goto-char (1+ delimline))
585 (if (eval mail-mailer-swallows-blank-line)
587 ;; Find and handle any FCC fields.
588 (goto-char (point-min))
589 (if (re-search-forward "^FCC:" delimline t)
590 (mail-do-fcc delimline))
595 (let ((default-directory "/"))
596 (apply 'call-process-region
597 (append (list (point-min) (point-max)
598 (if (boundp 'sendmail-program)
601 nil errbuf nil "-oi")
602 ;; Always specify who from,
603 ;; since some systems have broken sendmails.
604 (list "-f" (user-login-name))
605 ;;; ;; Don't say "from root" if running under su.
606 ;;; (and (equal (user-real-login-name) "root")
607 ;;; (list "-f" (user-login-name)))
609 (list (concat "-oA" mail-alias-file)))
610 ;; These mean "report errors by mail"
611 ;; and "deliver in background".
612 (if (null mail-interactive) '("-oem" "-odb"))
613 ;; Get the addresses from the message
614 ;; unless this is a resend.
615 ;; We must not do that for a resend
616 ;; because we would find the original addresses.
617 ;; For a resend, include the specific addresses.
618 (or resend-to-addresses
623 (goto-char (point-min))
624 (while (re-search-forward "\n\n* *" nil t)
625 (replace-match "; "))
626 (if (not (zerop (buffer-size)))
627 (error "Sending...failed to %s"
628 (buffer-substring (point-min) (point-max)))))))
631 (kill-buffer errbuf)))))
633 (defun mail-do-fcc (header-end)
635 (rmailbuf (current-buffer))
636 (time (current-time))
637 (tembuf (generate-new-buffer " rmail output"))
638 (case-fold-search t))
640 (goto-char (point-min))
641 (while (re-search-forward "^FCC:[ \t]*" header-end t)
642 (setq fcc-list (cons (buffer-substring (point)
645 (skip-chars-backward " \t")
648 (delete-region (match-beginning 0)
649 (progn (forward-line 1) (point))))
652 ;; This initial newline is written out if the fcc file already exists.
653 (insert "\nFrom " (user-login-name) " "
654 (current-time-string time) "\n")
655 ;; Insert the time zone before the year.
658 (require 'mail-utils)
659 (insert (mail-rfc822-time-zone time) " ")
660 (goto-char (point-max))
661 (insert-buffer-substring rmailbuf)
662 ;; Make sure messages are separated.
663 (goto-char (point-max))
666 ;; ``Quote'' "^From " as ">From "
667 ;; (note that this isn't really quoting, as there is no requirement
668 ;; that "^[>]+From " be quoted in the same transparent way.)
669 (let ((case-fold-search nil))
670 (while (search-forward "\nFrom " nil t)
674 (let* ((buffer (find-buffer-visiting (car fcc-list)))
675 (curbuf (current-buffer))
676 (beg (point-min)) (end (point-max))
677 (beg2 (save-excursion (goto-char (point-min))
678 (forward-line 2) (point))))
680 ;; File is present in a buffer => append to that buffer.
683 ;; Keep the end of the accessible portion at the same place
684 ;; unless it is the end of the buffer.
685 (let ((max (if (/= (1+ (buffer-size)) (point-max))
688 ;; Code below lifted from rmailout.el
689 ;; function rmail-output-to-rmail-file:
690 (let ((buffer-read-only nil)
691 (msg (and (boundp 'rmail-current-message)
692 rmail-current-message)))
693 ;; If MSG is non-nil, buffer is in RMAIL mode.
696 (rmail-maybe-set-message-counters)
698 (narrow-to-region (point-max) (point-max))
699 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
700 "Date: " (mail-rfc822-date) "\n")
701 (insert-buffer-substring curbuf beg2 end)
703 (goto-char (point-min))
705 (search-backward "\n\^_")
706 (narrow-to-region (point) (point-max))
707 (rmail-count-new-messages t)
708 (rmail-show-message msg)
710 ;; Output file not in rmail mode
711 ;; => just insert at the end.
712 (narrow-to-region (point-min) (1+ (buffer-size)))
713 (goto-char (point-max))
714 (insert-buffer-substring curbuf beg end)))
715 (if max (narrow-to-region (point-min) max)))))
716 ;; Else append to the file directly.
717 (if (and (file-exists-p (car fcc-list))
718 (mail-file-babyl-p (car fcc-list)))
719 ;; If the file is a Babyl file,
720 ;; convert the message to Babyl format.
722 (set-buffer (get-buffer-create " mail-temp"))
723 (setq buffer-read-only nil)
725 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
726 "Date: " (mail-rfc822-date) "\n")
727 (insert-buffer-substring curbuf beg2 end)
729 (write-region (point-min) (point-max) (car fcc-list) t)
732 (1+ (point-min)) (point-max) (car fcc-list) t))))
733 (setq fcc-list (cdr fcc-list))))
734 (kill-buffer tembuf)))
736 (defun mail-sent-via ()
737 "Make a Sent-via header line from each To or CC header line."
740 (goto-char (point-min))
741 ;; find the header-separator
742 (search-forward (concat "\n" mail-header-separator "\n"))
744 ;; put a marker at the end of the header
745 (let ((end (point-marker))
748 (goto-char (point-min))
749 ;; search for the To: lines and make Sent-via: lines from them
750 ;; search for the next To: line
751 (while (re-search-forward "^\\(to\\|cc\\):" end t)
752 ;; Grab this line plus all its continuations, sans the `to:'.
754 (buffer-substring (point)
756 (if (re-search-forward "^[^ \t\n]" end t)
760 ;; Insert a copy, with altered header field name.
761 (insert-before-markers "Sent-via:" to-line))))))
764 "Move point to end of To-field."
767 (mail-position-on-field "To"))
769 (defun mail-subject ()
770 "Move point to end of Subject-field."
773 (mail-position-on-field "Subject"))
776 "Move point to end of CC-field. Create a CC field if none."
779 (or (mail-position-on-field "cc" t)
780 (progn (mail-position-on-field "to")
784 "Move point to end of BCC-field. Create a BCC field if none."
787 (or (mail-position-on-field "bcc" t)
788 (progn (mail-position-on-field "to")
789 (insert "\nBCC: "))))
791 (defun mail-fcc (folder)
792 "Add a new FCC field, with file name completion."
793 (interactive "FFolder carbon copy: ")
795 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
796 (mail-position-on-field "to"))
797 (insert "\nFCC: " folder))
799 (defun mail-reply-to ()
800 "Move point to end of Reply-To-field."
803 (mail-position-on-field "Reply-To"))
805 (defun mail-position-on-field (field &optional soft)
807 (case-fold-search t))
808 (goto-char (point-min))
809 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
810 (setq end (match-beginning 0))
811 (goto-char (point-min))
812 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
814 (re-search-forward "^[^ \t]" nil 'move)
816 (skip-chars-backward "\n")
819 (progn (goto-char end)
820 (insert field ": \n")
821 (skip-chars-backward "\n")))
825 "Move point to beginning of message text."
828 (goto-char (point-min))
829 (search-forward (concat "\n" mail-header-separator "\n")))
831 (defun mail-signature (atpoint)
832 "Sign letter with contents of the file `mail-signature-file'.
833 Prefix arg means put contents at point."
837 (goto-char (point-max)))
838 (skip-chars-backward " \t\n")
841 (delete-region (point) (point-max)))
843 (insert-file-contents (expand-file-name mail-signature-file))))
845 (defun mail-fill-yanked-message (&optional justifyp)
846 "Fill the paragraphs of a message yanked into this one.
847 Numeric argument means justify as well."
850 (goto-char (point-min))
851 (search-forward (concat "\n" mail-header-separator "\n") nil t)
852 (fill-individual-paragraphs (point)
857 (defun mail-indent-citation ()
858 "Modify text just inserted from a message to be cited.
859 The inserted text should be the region.
860 When this function returns, the region is again around the modified text.
862 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
863 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
864 (let ((start (point)))
865 (mail-yank-clear-headers start (mark t))
866 (if (null mail-yank-prefix)
867 (indent-rigidly start (mark t) mail-indentation-spaces)
870 (while (< (point) (mark t))
871 (insert mail-yank-prefix)
872 (forward-line 1))))))
874 (defun mail-yank-original (arg)
875 "Insert the message being replied to, if any (in rmail).
876 Puts point before the text and mark after.
877 Normally, indents each nonblank line ARG spaces (default 3).
878 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
880 Just \\[universal-argument] as argument means don't indent, insert no prefix,
881 and don't delete any header fields."
883 (if mail-reply-buffer
884 (let ((start (point)))
885 ;; If the original message is in another window in the same frame,
886 ;; delete that window to save screen space.
887 ;; t means don't alter other frames.
888 (delete-windows-on mail-reply-buffer t)
889 (insert-buffer mail-reply-buffer)
893 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
894 mail-indentation-spaces)))
895 (if mail-citation-hook
896 (run-hooks 'mail-citation-hook)
898 (run-hooks 'mail-yank-hooks)
899 (mail-indent-citation)))))
900 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
901 ;; It is cleaner to avoid activation, even though the command
902 ;; loop would deactivate the mark because we inserted text.
903 (goto-char (prog1 (mark t)
904 (set-marker (mark-marker) (point) (current-buffer))))
905 (if (not (eolp)) (insert ?\n)))))
907 (defun mail-yank-clear-headers (start end)
908 (if mail-yank-ignored-headers
911 (if (search-forward "\n\n" end t)
913 (narrow-to-region start (point))
915 (while (let ((case-fold-search t))
916 (re-search-forward mail-yank-ignored-headers nil t))
918 (delete-region (point)
919 (progn (re-search-forward "\n[^ \t]")
923 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
926 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
927 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
928 When this function returns, the buffer `*mail*' is selected.
929 The value is t if the message was newly initialized; otherwise, nil.
931 Optionally, the signature file `mail-signature-file' can be inserted at the
932 end; see the variable `mail-signature'.
935 While editing message, type \\[mail-send-and-exit] to send the message and exit.
937 Various special commands starting with C-c are available in sendmail mode
938 to move to message header fields:
941 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
942 when the message is initialized.
944 If `mail-default-reply-to' is non-nil, it should be an address (a string);
945 a Reply-to: field with that address is inserted.
947 If `mail-archive-file-name' is non-nil, an FCC field with that file name
950 The normal hook `mail-setup-hook' is run after the message is
951 initialized. It can add more default fields to the message.
953 When calling from a program, the first argument if non-nil says
954 not to erase the existing contents of the `*mail*' buffer.
956 The second through fifth arguments,
957 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
958 the initial contents of those header fields.
959 These arguments should not have final newlines.
960 The sixth argument REPLYBUFFER is a buffer whose contents
961 should be yanked if the user types C-c C-y.
962 The seventh argument ACTIONS is a list of actions to take
963 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
964 when the message is sent, we apply FUNCTION to ARGS.
965 This is how Rmail arranges to mark messages `answered'."
967 ;;; This is commented out because I found it was confusing in practice.
968 ;;; It is easy enough to rename *mail* by hand with rename-buffer
969 ;;; if you want to have multiple mail buffers.
970 ;;; And then you can control which messages to save. --rms.
973 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
976 ;;; (while (and (setq buffer
977 ;;; (get-buffer (if (= 1 index) "*mail*"
978 ;;; (format "*mail*<%d>" index))))
979 ;;; (not (buffer-modified-p buffer)))
980 ;;; (setq index (1+ index)))
981 ;;; (if buffer (switch-to-buffer buffer)
982 ;;; ;; If none exists, start a new message.
983 ;;; ;; This will never re-use an existing unmodified mail buffer
984 ;;; ;; (since index is not 1 anymore). Perhaps it should.
985 ;;; (setq noerase nil))))
986 ;;; ;; Unless we found a modified message and are happy, start a new message.
987 ;;; (if (not noerase)
989 ;;; ;; Look for existing unmodified mail buffer.
990 ;;; (while (and (setq buffer
991 ;;; (get-buffer (if (= 1 index) "*mail*"
992 ;;; (format "*mail*<%d>" index))))
993 ;;; (buffer-modified-p buffer))
994 ;;; (setq index (1+ index)))
995 ;;; ;; If none, make a new one.
997 ;;; (setq buffer (generate-new-buffer "*mail*")))
998 ;;; ;; Go there and initialize it.
999 ;;; (switch-to-buffer buffer)
1001 ;;; (setq default-directory (expand-file-name "~/"))
1002 ;;; (auto-save-mode auto-save-default)
1004 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1005 ;;; (if (and buffer-auto-save-file-name
1006 ;;; (file-exists-p buffer-auto-save-file-name))
1007 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1009 (pop-to-buffer "*mail*")
1010 (auto-save-mode auto-save-default)
1012 ;; Disconnect the buffer from its visited file
1013 ;; (in case the user has actually visited a file *mail*).
1014 ; (set-visited-file-name nil)
1017 (or (not (buffer-modified-p))
1018 (y-or-n-p "Unsent message being composed; erase it? "))
1019 (let ((inhibit-read-only t))
1021 (mail-setup to subject in-reply-to cc replybuffer actions)
1022 (setq initialized t)))
1023 (if (and buffer-auto-save-file-name
1024 (file-exists-p buffer-auto-save-file-name))
1025 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1028 (defun mail-recover ()
1029 "Reread contents of current buffer from its last auto-save file."
1031 (let ((file-name (make-auto-save-file-name)))
1032 (cond ((save-window-excursion
1033 (if (not (eq system-type 'vax-vms))
1034 (with-output-to-temp-buffer "*Directory*"
1035 (buffer-disable-undo standard-output)
1036 (let ((default-directory "/"))
1038 "ls" nil standard-output nil "-l" file-name))))
1039 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1040 (let ((buffer-read-only nil))
1042 (insert-file-contents file-name nil)))
1043 (t (error "mail-recover cancelled")))))
1046 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1047 "Like `mail' command, but display mail buffer in another window."
1049 (let ((pop-up-windows t)
1050 (special-display-buffer-names nil)
1051 (special-display-regexps nil)
1052 (same-window-buffer-names nil)
1053 (same-window-regexps nil))
1054 (pop-to-buffer "*mail*"))
1055 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1058 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1059 "Like `mail' command, but display mail buffer in another frame."
1061 (let ((pop-up-frames t)
1062 (special-display-buffer-names nil)
1063 (special-display-regexps nil)
1064 (same-window-buffer-names nil)
1065 (same-window-regexps nil))
1066 (pop-to-buffer "*mail*"))
1067 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1069 ;;; Do not execute these when sendmail.el is loaded,
1070 ;;; only in loaddefs.el.
1071 ;;;###autoload (define-key ctl-x-map "m" 'mail)
1072 ;;;###autoload (define-key ctl-x-4-map "m" 'mail-other-window)
1073 ;;;###autoload (define-key ctl-x-5-map "m" 'mail-other-frame)
1075 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1077 ;;; Do not add anything but external entries on this page.
1081 ;;; sendmail.el ends here