1 ;; Mail sending commands for Emacs.
2 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 (defconst mail-self-blind nil
"\
25 Non-nil means insert BCC to self in messages to be sent.
26 This is done when the message is initialized,
27 so you can remove or alter the BCC field to override the default.")
30 (defconst mail-interactive nil
"\
31 Non-nil means when sending a message wait for and display errors.
32 nil means let mailer mail back a message to report errors.")
35 (defconst mail-yank-ignored-headers
"^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
36 Delete these headers from old message when it's inserted in a reply.")
38 ;; Useful to set in site-init.el
40 (defconst send-mail-function
'sendmail-send-it
"\
41 Function to call to send the current buffer as mail.
42 The headers are be delimited by a line which is mail-header-separator.")
45 (defvar mail-header-separator
"--text follows this line--" "\
46 *Line used to separate headers from text in messages being composed.")
49 (defvar mail-archive-file-name nil
"\
50 *Name of file to write all outgoing messages in, or nil for none.
51 Do not use an rmail file here! Instead, use its inbox file.")
53 (defvar mail-default-reply-to nil
54 "*Address to insert as default Reply-to field of outgoing messages.")
56 (defvar mail-alias-file nil
57 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
58 This file defines aliases to be expanded by the mailer; this is a different
59 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
60 This variable has no effect unless your system uses sendmail as its mailer.")
62 (defvar mail-yank-prefix nil
63 "*Prefix insert on lines of yanked message being replied to.
64 nil means use indentation.")
66 (defvar mail-abbrevs-loaded nil
)
67 (defvar mail-mode-map nil
)
69 (defvar mail-signature nil
70 "*Text inserted at end of mail buffer when a message is initialized.
71 If t, it means to insert the contents of the file `~/.signature'.")
73 (defvar mail-reply-buffer nil
)
74 (defvar mail-send-actions nil
75 "A list of actions to be performed upon successful sending of a message.")
77 (defvar mail-default-headers nil
78 "*A string containing header lines, to be inserted in outgoing messages.
79 It is inserted before you edit the message,
80 so you can edit or delete these lines.")
82 (defvar mail-mode-syntax-table nil
83 "Syntax table used while in mail mode.")
85 (if (null mail-mode-syntax-table
)
87 (setq mail-mode-syntax-table
(copy-syntax-table text-mode-syntax-table
))
88 (modify-syntax-entry ?%
". " mail-mode-syntax-table
)))
90 (defun mail-setup (to subject in-reply-to cc replybuffer actions
)
91 (setq mail-send-actions actions
)
93 (setq mail-reply-buffer replybuffer
)
94 (goto-char (point-min))
100 ;;; Here removed code to extract names from within <...>
101 ;;; on the assumption that mail-strip-quoted-names
102 ;;; has been called and has done so.
103 (let ((fill-prefix "\t"))
104 (fill-region (point-min) (point-max))))
109 (insert "CC: " cc
"\n")
110 (fill-region-as-paragraph opos
(point-max))))
112 (insert "In-reply-to: " in-reply-to
"\n"))
113 (insert "Subject: " (or subject
"") "\n")
114 (if mail-default-headers
115 (insert mail-default-headers
))
116 (if mail-default-reply-to
117 (insert "Reply-to: " mail-default-reply-to
"\n"))
119 (insert "BCC: " (user-login-name) "\n"))
120 (if mail-archive-file-name
121 (insert "FCC: " mail-archive-file-name
"\n"))
122 (insert mail-header-separator
"\n")
123 ;; Read the .signature file if we haven't already done so
124 ;; (and if the user has not overridden it).
125 (cond ((eq mail-signature t
)
127 (insert-file-contents "~/.signature"))
129 (insert mail-signature
)))
130 (goto-char (point-max))
131 (or (bolp) (newline)))
132 (if to
(goto-char (point-max)))
133 (or to subject in-reply-to
134 (set-buffer-modified-p nil
))
135 (run-hooks 'mail-setup-hook
))
139 "Major mode for editing mail to be sent.
140 Like Text Mode but with these additional commands:
141 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
142 C-c C-f move to a header field (and create it if there isn't):
143 C-c C-f C-t move to To: C-c C-f C-s move to Subj:
144 C-c C-f C-b move to BCC: C-c C-f C-c move to CC:
145 C-c C-t move to message text.
146 C-c C-y mail-yank-original (insert current message, in Rmail).
147 C-c C-q mail-fill-yanked-message (fill what was yanked).
148 C-c C-v mail-sent-via (add a sent-via field for each To or CC)."
150 (kill-all-local-variables)
151 (make-local-variable 'mail-reply-buffer
)
152 (setq mail-reply-buffer nil
)
153 (make-local-variable 'mail-send-actions
)
154 (set-syntax-table mail-mode-syntax-table
)
155 (use-local-map mail-mode-map
)
156 (setq local-abbrev-table text-mode-abbrev-table
)
157 (setq major-mode
'mail-mode
)
158 (setq mode-name
"Mail")
159 (setq buffer-offer-save t
)
160 (make-local-variable 'paragraph-separate
)
161 (make-local-variable 'paragraph-start
)
162 (setq paragraph-start
(concat "^" mail-header-separator
163 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
165 (setq paragraph-separate
(concat "^" mail-header-separator
166 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
168 (run-hooks 'text-mode-hook
'mail-mode-hook
))
172 (setq mail-mode-map
(nconc (make-sparse-keymap) text-mode-map
))
173 (define-key mail-mode-map
"\C-c?" 'describe-mode
)
174 (define-key mail-mode-map
"\C-c\C-f\C-t" 'mail-to
)
175 (define-key mail-mode-map
"\C-c\C-f\C-b" 'mail-bcc
)
176 (define-key mail-mode-map
"\C-c\C-f\C-c" 'mail-cc
)
177 (define-key mail-mode-map
"\C-c\C-f\C-s" 'mail-subject
)
178 (define-key mail-mode-map
"\C-c\C-t" 'mail-text
)
179 (define-key mail-mode-map
"\C-c\C-y" 'mail-yank-original
)
180 (define-key mail-mode-map
"\C-c\C-q" 'mail-fill-yanked-message
)
181 (define-key mail-mode-map
"\C-c\C-w" 'mail-signature
)
182 (define-key mail-mode-map
"\C-c\C-v" 'mail-sent-via
)
183 (define-key mail-mode-map
"\C-c\C-c" 'mail-send-and-exit
)
184 (define-key mail-mode-map
"\C-c\C-s" 'mail-send
))
186 (defun mail-send-and-exit (arg)
187 "Send message like mail-send, then, if no errors, exit from mail buffer.
188 Prefix arg means don't delete this window."
191 (bury-buffer (current-buffer))
192 (if (or (one-window-p) arg
)
193 (switch-to-buffer (other-buffer (current-buffer)))
197 "Send the message in the current buffer.
198 If `mail-interactive' is non-nil, wait for success indication
199 or error messages, and inform user.
200 Otherwise any failure is reported in a message back to
201 the user from the mailer."
203 (if (or (buffer-modified-p)
204 (y-or-n-p "Message already sent; resend? "))
206 (message "Sending...")
207 (run-hooks 'mail-send-hook
)
208 (funcall send-mail-function
)
209 ;; Now perform actions on successful sending.
210 (while mail-send-actions
212 (apply (car (car mail-send-actions
)) (cdr (car mail-send-actions
)))
214 (setq mail-send-actions
(cdr mail-send-actions
)))
215 (set-buffer-modified-p nil
)
216 (delete-auto-save-file-if-necessary t
)
217 (message "Sending...done"))))
219 (defun sendmail-send-it ()
220 (let ((errbuf (if mail-interactive
221 (generate-new-buffer " sendmail errors")
223 (tembuf (generate-new-buffer " sendmail temp"))
224 (case-fold-search nil
)
226 (mailbuf (current-buffer)))
231 (insert-buffer-substring mailbuf
)
232 (goto-char (point-max))
233 ;; require one newline at the end.
234 (or (= (preceding-char) ?
\n)
236 ;; Change header-delimiter to be what sendmail expects.
237 (goto-char (point-min))
239 (concat "^" (regexp-quote mail-header-separator
) "\n"))
242 (setq delimline
(point-marker))
243 (goto-char (point-min))
244 ;; ignore any blank lines in the header
245 (while (and (re-search-forward "\n\n\n*" delimline t
)
246 (< (point) delimline
))
247 (replace-match "\n"))
248 (let ((case-fold-search t
))
249 (goto-char (point-min))
250 (if (re-search-forward "^Sender:" delimline t
)
251 (error "Sender may not be specified."))
252 ;; Find and handle any FCC fields.
253 (goto-char (point-min))
254 (if (re-search-forward "^FCC:" delimline t
)
255 (mail-do-fcc delimline
))
256 ;; If the From is different than current user, insert Sender.
257 (goto-char (point-min))
258 (and (re-search-forward "^From:" delimline t
)
260 (require 'mail-utils
)
262 (mail-strip-quoted-names
264 (narrow-to-region (point-min) delimline
)
265 (mail-fetch-field "From")))
269 (insert "Sender: " (user-login-name) "\n")))
270 ;; "S:" is an abbreviation for "Subject:".
271 (goto-char (point-min))
272 (if (re-search-forward "^S:" delimline t
)
273 (replace-match "Subject:"))
274 ;; Don't send out a blank subject line
275 (goto-char (point-min))
276 (if (re-search-forward "^Subject:[ \t]*\n" delimline t
)
282 (apply 'call-process-region
283 (append (list (point-min) (point-max)
284 (if (boundp 'sendmail-program
)
289 ;; Always specify who from,
290 ;; since some systems have broken sendmails.
291 (list "-f" (user-login-name))
292 ;;; ;; Don't say "from root" if running under su.
293 ;;; (and (equal (user-real-login-name) "root")
294 ;;; (list "-f" (user-login-name)))
296 (list (concat "-oA" mail-alias-file
)))
297 ;; These mean "report errors by mail"
298 ;; and "deliver in background".
299 (if (null mail-interactive
) '("-oem" "-odb"))))
303 (goto-char (point-min))
304 (while (re-search-forward "\n\n* *" nil t
)
305 (replace-match "; "))
306 (if (not (zerop (buffer-size)))
307 (error "Sending...failed to %s"
308 (buffer-substring (point-min) (point-max)))))))
311 (kill-buffer errbuf
)))))
313 (defun mail-do-fcc (header-end)
315 (rmailbuf (current-buffer))
316 (tembuf (generate-new-buffer " rmail output"))
317 (case-fold-search t
))
319 (goto-char (point-min))
320 (while (re-search-forward "^FCC:[ \t]*" header-end t
)
321 (setq fcc-list
(cons (buffer-substring (point)
324 (skip-chars-backward " \t")
327 (delete-region (match-beginning 0)
328 (progn (forward-line 1) (point))))
331 (insert "\nFrom " (user-login-name) " "
332 (current-time-string) "\n")
333 (insert-buffer-substring rmailbuf
)
334 ;; Make sure messages are separated.
335 (goto-char (point-max))
338 ;; ``Quote'' "^From " as ">From "
339 ;; (note that this isn't really quoting, as there is no requirement
340 ;; that "^[>]+From " be quoted in the same transparent way.)
341 (let ((case-fold-search nil
))
342 (while (search-forward "\nFrom " nil t
)
346 (let ((buffer (get-file-buffer (car fcc-list
))))
348 ;; File is present in a buffer => append to that buffer.
349 (let ((curbuf (current-buffer))
350 (beg (point-min)) (end (point-max)))
353 ;; Keep the end of the accessible portion at the same place
354 ;; unless it is the end of the buffer.
355 (let ((max (if (/= (1+ (buffer-size)) (point-max))
359 (narrow-to-region (point-min) (1+ (buffer-size)))
360 (goto-char (point-max))
361 (if (eq major-mode
'rmail-mode
)
362 ;; Append as a message to an RMAIL file
363 (let ((buffer-read-only nil
))
364 ;; This forces RMAIL's message counters to be
365 ;; recomputed when the next RMAIL operation is
366 ;; done on the buffer.
367 ;; See rmail-maybe-set-message-counters.
368 (setq rmail-total-messages nil
)
369 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
370 "From: " (user-login-name) "\n"
371 "Date: " (current-time-string) "\n")
372 (insert-buffer-substring curbuf beg end
)
374 (insert-buffer-substring curbuf beg end
)))
375 (if max
(narrow-to-region (point-min) max
))))))
376 ;; Else append to the file directly.
378 ;; Include a blank line before if file already exists.
379 (if (file-exists-p (car fcc-list
)) (point-min) (1+ (point-min)))
380 (point-max) (car fcc-list
) t
)))
381 (setq fcc-list
(cdr fcc-list
))))
382 (kill-buffer tembuf
)))
384 (defun mail-sent-via ()
385 "Make a Sent-via header line from each To or CC header line."
388 (goto-char (point-min))
389 ;; find the header-separator
390 (search-forward (concat "\n" mail-header-separator
"\n"))
392 ;; put a marker at the end of the header
393 (let ((end (point-marker))
396 (goto-char (point-min))
397 ;; search for the To: lines and make Sent-via: lines from them
398 ;; search for the next To: line
399 (while (re-search-forward "^\\(to\\|cc\\):" end t
)
400 ;; Grab this line plus all its continuations, sans the `to:'.
402 (buffer-substring (point)
404 (if (re-search-forward "^[^ \t\n]" end t
)
408 ;; Insert a copy, with altered header field name.
409 (insert-before-markers "Sent-via:" to-line
))))))
412 "Move point to end of To-field."
415 (mail-position-on-field "To"))
417 (defun mail-subject ()
418 "Move point to end of Subject-field."
421 (mail-position-on-field "Subject"))
424 "Move point to end of CC-field. Create a CC field if none."
427 (or (mail-position-on-field "cc" t
)
428 (progn (mail-position-on-field "to")
432 "Move point to end of BCC-field. Create a BCC field if none."
435 (or (mail-position-on-field "bcc" t
)
436 (progn (mail-position-on-field "to")
437 (insert "\nBCC: "))))
439 (defun mail-position-on-field (field &optional soft
)
441 (case-fold-search t
))
442 (goto-char (point-min))
443 (search-forward (concat "\n" mail-header-separator
"\n"))
444 (setq end
(match-beginning 0))
445 (goto-char (point-min))
446 (if (re-search-forward (concat "^" (regexp-quote field
) ":") end t
)
448 (re-search-forward "^[^ \t]" nil
'move
)
450 (skip-chars-backward "\n")
453 (progn (goto-char end
)
454 (skip-chars-backward "\n")
455 (insert "\n" field
": ")))
459 "Move point to beginning of text field."
461 (goto-char (point-min))
462 (search-forward (concat "\n" mail-header-separator
"\n")))
464 (defun mail-signature ()
465 "Sign letter with contents of ~/.signature file."
468 (goto-char (point-max))
469 (skip-chars-backward " \t\n")
471 (delete-region (point) (point-max))
473 (insert-file-contents (expand-file-name "~/.signature"))))
475 (defun mail-fill-yanked-message (&optional justifyp
)
476 "Fill the paragraphs of a message yanked into this one.
477 Numeric argument means justify as well."
480 (goto-char (point-min))
481 (search-forward (concat "\n" mail-header-separator
"\n") nil t
)
482 (fill-individual-paragraphs (point)
487 (defun mail-yank-original (arg)
488 "Insert the message being replied to, if any (in rmail).
489 Puts point before the text and mark after.
490 Normally, indents each nonblank line ARG spaces (default 3).
491 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
493 Just \\[universal-argument] as argument means don't indent, insert no prefix,
494 and don't delete any header fields."
496 (if mail-reply-buffer
497 (let ((start (point)))
498 (delete-windows-on mail-reply-buffer
)
499 (insert-buffer mail-reply-buffer
)
502 (mail-yank-clear-headers start
(mark))
503 (if (null mail-yank-prefix
)
504 (indent-rigidly start
(mark)
505 (if arg
(prefix-numeric-value arg
) 3))
508 (while (< (point) (mark))
509 (insert mail-yank-prefix
)
511 (exchange-point-and-mark)
512 (if (not (eolp)) (insert ?
\n)))))
514 (defun mail-yank-clear-headers (start end
)
517 (if (search-forward "\n\n" end t
)
519 (narrow-to-region start
(point))
521 (while (let ((case-fold-search t
))
522 (re-search-forward mail-yank-ignored-headers nil t
))
524 (delete-region (point)
525 (progn (re-search-forward "\n[^ \t]")
529 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
532 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions
)
533 "Edit a message to be sent. Argument means resume editing (don't erase).
534 Search for an existing mail buffer currently not in use and initialize it,
535 or make a new one if all existing mail buffers are busy.
536 With an argument, search for a busy existing mail buffer and re-select it.
538 Returns with message buffer selected; value t if message freshly initialized.
539 By default, the signature file `~/.signature' is inserted at the end;
540 see the variable `mail-signature'.
543 While editing message, type \\[mail-send-and-exit] to send the message and exit.
545 Various special commands starting with C-c are available in sendmail mode
546 to move to message header fields:
549 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
550 when the message is initialized.
552 If `mail-default-reply-to' is non-nil, it should be an address (a string);
553 a Reply-to: field with that address is inserted.
555 If `mail-archive-file-name' is non-nil, an FCC field with that file name
558 If `mail-setup-hook' is bound, its value is called with no arguments
559 after the message is initialized. It can add more default fields.
561 When calling from a program, the second through fifth arguments
562 TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
563 the initial contents of those header fields.
564 These arguments should not have final newlines.
565 The sixth argument REPLYBUFFER is a buffer whose contents
566 should be yanked if the user types C-c C-y.
567 The seventh argument ACTIONS is a list of actions to take
568 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
569 when the message is sent, we apply FUNCTION to ARGS.
570 This is how Rmail arranges to mark messages `answered'."
574 ;; If requested, look for a mail buffer that is modified and go to it.
577 (while (and (setq buffer
578 (get-buffer (if (= 1 index
) "*mail*"
579 (format "*mail*<%d>" index
))))
580 (not (buffer-modified-p buffer
)))
581 (setq index
(1+ index
)))
582 (if buffer
(switch-to-buffer buffer
)
583 ;; If none exists, start a new message.
584 ;; This will never re-use an existing unmodified mail buffer
585 ;; (since index is not 1 anymore). Perhaps it should.
586 (setq noerase nil
))))
587 ;; Unless we found a modified message and are happy, start a new message.
590 ;; Look for existing unmodified mail buffer.
591 (while (and (setq buffer
592 (get-buffer (if (= 1 index
) "*mail*"
593 (format "*mail*<%d>" index
))))
594 (buffer-modified-p buffer
))
595 (setq index
(1+ index
)))
596 ;; If none, make a new one.
598 (setq buffer
(generate-new-buffer "*mail*")))
599 ;; Go there and initialize it.
600 (switch-to-buffer buffer
)
602 (setq default-directory
(expand-file-name "~/"))
603 (auto-save-mode auto-save-default
)
605 (mail-setup to subject in-reply-to cc replybuffer actions
)
606 (if (and buffer-auto-save-file-name
607 (file-exists-p buffer-auto-save-file-name
))
608 (message "Auto save file for draft message exists; consider M-x mail-recover"))
612 (define-key ctl-x-map
"m" 'mail
)
614 (defun mail-recover ()
615 "Reread contents of current buffer from its last auto-save file."
617 (let ((file-name (make-auto-save-file-name)))
618 (cond ((save-window-excursion
619 (if (not (eq system-type
'vax-vms
))
620 (with-output-to-temp-buffer "*Directory*"
621 (buffer-disable-undo standard-output
)
622 (call-process "ls" nil standard-output nil
"-l" file-name
)))
623 (yes-or-no-p (format "Recover auto save file %s? " file-name
)))
624 (let ((buffer-read-only nil
))
626 (insert-file-contents file-name nil
)))
627 (t (error "mail-recover cancelled.")))))
630 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
631 "Like `mail' command, but display mail buffer in another window."
633 (let ((pop-up-windows t
))
634 (pop-to-buffer "*mail*"))
635 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
638 (defun mail-other-screen (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
639 "Like `mail' command, but display mail buffer in another screen."
641 (let ((pop-up-screens t
))
642 (pop-to-buffer "*mail*"))
643 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
647 (define-key ctl-x-4-map
"m" 'mail-other-window
)
650 (define-key ctl-x-3-map
"m" 'mail-other-screen
)
653 ;;; Do not add anything but external entries on this page.