]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
(sendmail-send-it): Parse folded
[gnu-emacs] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.
2
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This mode provides mail-sending facilities from within Emacs. It is
28 ;; documented in the Emacs user's manual.
29
30 ;;; Code:
31
32 ;;;###autoload
33 (defvar mail-from-style 'angles "\
34 *Specifies how \"From:\" fields look.
35
36 If `nil', they contain just the return address like:
37 king@grassland.com
38 If `parens', they look like:
39 king@grassland.com (Elvis Parsley)
40 If `angles', they look like:
41 Elvis Parsley <king@grassland.com>")
42
43 ;;;###autoload
44 (defvar mail-self-blind nil "\
45 *Non-nil means insert BCC to self in messages to be sent.
46 This is done when the message is initialized,
47 so you can remove or alter the BCC field to override the default.")
48
49 ;;;###autoload
50 (defvar mail-interactive nil "\
51 *Non-nil means when sending a message wait for and display errors.
52 nil means let mailer mail back a message to report errors.")
53
54 ;;;###autoload
55 (defvar mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
56 *Delete these headers from old message when it's inserted in a reply.")
57
58 ;; Useful to set in site-init.el
59 ;;;###autoload
60 (defvar send-mail-function 'sendmail-send-it "\
61 Function to call to send the current buffer as mail.
62 The headers should be delimited by a line whose contents
63 match the variable `mail-header-separator'.")
64
65 ;;;###autoload
66 (defvar mail-header-separator "--text follows this line--" "\
67 *Line used to separate headers from text in messages being composed.")
68
69 ;; Set up mail-header-separator for use as a category text property.
70 (put 'mail-header-separator 'rear-nonsticky '(category))
71 ;;; This was a nice idea, for preventing accidental modification of
72 ;;; the separator. But I found it also prevented or obstructed
73 ;;; certain deliberate operations, such as copying the separator line
74 ;;; up to the top to send myself a copy of an already sent outgoing message
75 ;;; and other things. So I turned it off. --rms.
76 ;;;(put 'mail-header-separator 'read-only t)
77
78 ;;;###autoload
79 (defvar mail-archive-file-name nil "\
80 *Name of file to write all outgoing messages in, or nil for none.
81 This can be an inbox file or an Rmail file.")
82
83 ;;;###autoload
84 (defvar mail-default-reply-to nil
85 "*Address to insert as default Reply-to field of outgoing messages.
86 If nil, it will be initialized from the REPLYTO environment variable
87 when you first send mail.")
88
89 ;;;###autoload
90 (defvar mail-alias-file nil
91 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
92 This file defines aliases to be expanded by the mailer; this is a different
93 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
94 This variable has no effect unless your system uses sendmail as its mailer.")
95
96 ;;;###autoload
97 (defvar mail-personal-alias-file "~/.mailrc"
98 "*If non-nil, the name of the user's personal mail alias file.
99 This file typically should be in same format as the `.mailrc' file used by
100 the `Mail' or `mailx' program.
101 This file need not actually exist.")
102
103 (defvar mail-setup-hook nil
104 "Normal hook, run each time a new outgoing mail message is initialized.
105 The function `mail-setup' runs this hook.")
106
107 (defvar mail-aliases t
108 "Alist of mail address aliases,
109 or t meaning should be initialized from your mail aliases file.
110 \(The file's name is normally `~/.mailrc', but your MAILRC environment
111 variable can override that name.)
112 The alias definitions in the file have this form:
113 alias ALIAS MEANING")
114
115 (defvar mail-alias-modtime nil
116 "The modification time of your mail alias file when it was last examined.")
117
118 (defvar mail-yank-prefix nil
119 "*Prefix insert on lines of yanked message being replied to.
120 nil means use indentation.")
121 (defvar mail-indentation-spaces 3
122 "*Number of spaces to insert at the beginning of each cited line.
123 Used by `mail-yank-original' via `mail-indent-citation'.")
124 (defvar mail-yank-hooks nil
125 "Obsolete hook for modifying a citation just inserted in the mail buffer.
126 Each hook function can find the citation between (point) and (mark t).
127 And each hook function should leave point and mark around the citation
128 text as modified.
129
130 This is a normal hook, misnamed for historical reasons.
131 It is semi-obsolete and mail agents should no longer use it.")
132
133 (defvar mail-citation-hook nil
134 "*Hook for modifying a citation just inserted in the mail buffer.
135 Each hook function can find the citation between (point) and (mark t).
136 And each hook function should leave point and mark around the citation
137 text as modified.
138
139 If this hook is entirely empty (nil), a default action is taken
140 instead of no action.")
141
142 (defvar mail-abbrevs-loaded nil)
143 (defvar mail-mode-map nil)
144
145 (autoload 'build-mail-aliases "mailalias"
146 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
147 nil)
148
149 (autoload 'expand-mail-aliases "mailalias"
150 "Expand all mail aliases in suitable header fields found between BEG and END.
151 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
152 Optional second arg EXCLUDE may be a regular expression defining text to be
153 removed from alias expansions."
154 nil)
155
156 ;;;###autoload
157 (defvar mail-signature nil
158 "*Text inserted at end of mail buffer when a message is initialized.
159 If t, it means to insert the contents of the file `mail-signature-file'.")
160
161 (defvar mail-signature-file "~/.signature"
162 "*File containing the text inserted at end of mail buffer.")
163
164 (defvar mail-reply-action nil)
165 (defvar mail-send-actions nil
166 "A list of actions to be performed upon successful sending of a message.")
167 (put 'mail-reply-action 'permanent-local t)
168 (put 'mail-send-actions 'permanent-local t)
169
170 (defvar mail-default-headers nil
171 "*A string containing header lines, to be inserted in outgoing messages.
172 It is inserted before you edit the message,
173 so you can edit or delete these lines.")
174
175 (defvar mail-bury-selects-summary t
176 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
177 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
178 the RMAIL summary buffer before returning, if it exists and this variable
179 is non-nil.")
180
181 ;; I find that this happens so often, for innocent reasons,
182 ;; that it is not acceptable to bother the user about it -- rms.
183 (defvar mail-send-nonascii t
184 "*Specify whether to allow sending non-ASCII characters in mail.
185 If t, that means do allow it. nil means don't allow it.
186 `query' means ask the user each time.
187 Including non-ASCII characters in a mail message can be problematical
188 for the recipient, who may not know how to decode them properly.")
189
190 ;; Note: could use /usr/ucb/mail instead of sendmail;
191 ;; options -t, and -v if not interactive.
192 (defvar mail-mailer-swallows-blank-line
193 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
194 (file-readable-p "/etc/sendmail.cf")
195 (let ((buffer (get-buffer-create " *temp*")))
196 (unwind-protect
197 (save-excursion
198 (set-buffer buffer)
199 (insert-file-contents "/etc/sendmail.cf")
200 (goto-char (point-min))
201 (let ((case-fold-search nil))
202 (re-search-forward "^OR\\>" nil t)))
203 (kill-buffer buffer))))
204 ;; According to RFC822, "The field-name must be composed of printable
205 ;; ASCII characters (i.e. characters that have decimal values between
206 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
207 ;; space, or colon.
208 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
209 "Set this non-nil if the system's mailer runs the header and body together.
210 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
211 The value should be an expression to test whether the problem will
212 actually occur.")
213
214 (defvar mail-mode-syntax-table nil
215 "Syntax table used while in mail mode.")
216
217 (if (not mail-mode-syntax-table)
218 (progn
219 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
220 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
221
222 (defvar mail-font-lock-keywords
223 (eval-when-compile
224 (let* ((cite-chars "[>|}]")
225 (cite-prefix "A-Za-z")
226 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
227 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
228 '("^\\(B?CC\\|Reply-to\\):" . font-lock-keyword-face)
229 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
230 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
231 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
232 '(eval .
233 (cons (concat "^" (regexp-quote mail-header-separator) "$")
234 'font-lock-warning-face))
235 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
236 `(,cite-chars
237 (,(concat "\\=[ \t]*"
238 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
239 "\\(" cite-chars "[ \t]*\\)\\)+"
240 "\\(.*\\)")
241 (beginning-of-line) (end-of-line)
242 (2 font-lock-reference-face nil t)
243 (4 font-lock-comment-face nil t)))
244 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
245 . font-lock-string-face))))
246 "Additional expressions to highlight in Mail mode.")
247
248 (defvar mail-send-hook nil
249 "Normal hook run before sending mail, in Mail mode.")
250 \f
251 (defun sendmail-sync-aliases ()
252 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
253 (or (equal mail-alias-modtime modtime)
254 (setq mail-alias-modtime modtime
255 mail-aliases t))))
256
257 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
258 (or mail-default-reply-to
259 (setq mail-default-reply-to (getenv "REPLYTO")))
260 (sendmail-sync-aliases)
261 (if (eq mail-aliases t)
262 (progn
263 (setq mail-aliases nil)
264 (if (file-exists-p mail-personal-alias-file)
265 (build-mail-aliases))))
266 ;; Don't leave this around from a previous message.
267 (kill-local-variable 'buffer-file-coding-system)
268 (kill-local-variable 'enable-multibyte-characters)
269 (if current-input-method
270 (inactivate-input-method))
271 (setq mail-send-actions actions)
272 (setq mail-reply-action replybuffer)
273 (goto-char (point-min))
274 (insert "To: ")
275 (save-excursion
276 (if to
277 ;; Here removed code to extract names from within <...>
278 ;; on the assumption that mail-strip-quoted-names
279 ;; has been called and has done so.
280 (let ((fill-prefix "\t")
281 (address-start (point)))
282 (insert to "\n")
283 (fill-region-as-paragraph address-start (point-max)))
284 (newline))
285 (if cc
286 (let ((fill-prefix "\t")
287 (address-start (progn (insert "CC: ") (point))))
288 (insert cc "\n")
289 (fill-region-as-paragraph address-start (point-max))))
290 (if in-reply-to
291 (let ((fill-prefix "\t")
292 (fill-column 78)
293 (address-start (point)))
294 (insert "In-reply-to: " in-reply-to "\n")
295 (fill-region-as-paragraph address-start (point-max))))
296 (insert "Subject: " (or subject "") "\n")
297 (if mail-default-headers
298 (insert mail-default-headers))
299 (if mail-default-reply-to
300 (insert "Reply-to: " mail-default-reply-to "\n"))
301 (if mail-self-blind
302 (insert "BCC: " user-mail-address "\n"))
303 (if mail-archive-file-name
304 (insert "FCC: " mail-archive-file-name "\n"))
305 (put-text-property (point)
306 (progn
307 (insert mail-header-separator "\n")
308 (1- (point)))
309 'category 'mail-header-separator)
310 ;; Insert the signature. But remember the beginning of the message.
311 (if to (setq to (point)))
312 (cond ((eq mail-signature t)
313 (if (file-exists-p mail-signature-file)
314 (progn
315 (insert "\n\n-- \n")
316 (insert-file-contents mail-signature-file))))
317 (mail-signature
318 (insert mail-signature)))
319 (goto-char (point-max))
320 (or (bolp) (newline)))
321 (if to (goto-char to))
322 (or to subject in-reply-to
323 (set-buffer-modified-p nil))
324 (run-hooks 'mail-setup-hook))
325 \f
326 ;;;###autoload
327 (defun mail-mode ()
328 "Major mode for editing mail to be sent.
329 Like Text Mode but with these additional commands:
330 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
331 C-c C-f move to a header field (and create it if there isn't):
332 C-c C-f C-t move to To: C-c C-f C-s move to Subject:
333 C-c C-f C-c move to CC: C-c C-f C-b move to BCC:
334 C-c C-f C-f move to FCC:
335 C-c C-t mail-text (move to beginning of message text).
336 C-c C-w mail-signature (insert `mail-signature-file' file).
337 C-c C-y mail-yank-original (insert current message, in Rmail).
338 C-c C-q mail-fill-yanked-message (fill what was yanked).
339 C-c C-v mail-sent-via (add a Sent-via field for each To or CC)."
340 (interactive)
341 (kill-all-local-variables)
342 (make-local-variable 'mail-reply-action)
343 (make-local-variable 'mail-send-actions)
344 (set-syntax-table mail-mode-syntax-table)
345 (use-local-map mail-mode-map)
346 (setq local-abbrev-table text-mode-abbrev-table)
347 (setq major-mode 'mail-mode)
348 (setq mode-name "Mail")
349 (setq buffer-offer-save t)
350 (make-local-variable 'font-lock-defaults)
351 (setq font-lock-defaults '(mail-font-lock-keywords t))
352 (make-local-variable 'paragraph-separate)
353 (make-local-variable 'paragraph-start)
354 (make-local-variable 'normal-auto-fill-function)
355 (setq normal-auto-fill-function 'mail-mode-auto-fill)
356 (make-local-variable 'fill-paragraph-function)
357 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
358 (make-local-variable 'adaptive-fill-regexp)
359 (setq adaptive-fill-regexp
360 (concat "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|" adaptive-fill-regexp))
361 (make-local-variable 'adaptive-fill-first-line-regexp)
362 (setq adaptive-fill-first-line-regexp
363 (concat "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|" adaptive-fill-first-line-regexp))
364 ;; `-- ' precedes the signature. `-----' appears at the start of the
365 ;; lines that delimit forwarded messages.
366 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
367 ;; are also sometimes used and should be separators.
368 (setq paragraph-start (concat (regexp-quote mail-header-separator)
369 "$\\|[ \t]*[a-z0-9A-Z]*>+[ \t]*$\\|[ \t]*$\\|"
370 page-delimiter))
371 (setq paragraph-separate paragraph-start)
372 (run-hooks 'text-mode-hook 'mail-mode-hook))
373
374 (defun mail-mode-auto-fill ()
375 "Carry out Auto Fill for Mail mode.
376 If within the headers, this makes the new lines into continuation lines."
377 (if (< (point)
378 (save-excursion
379 (goto-char (point-min))
380 (if (re-search-forward
381 (concat "^" (regexp-quote mail-header-separator) "$")
382 nil t)
383 (point)
384 0)))
385 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
386 (if (do-auto-fill)
387 (save-excursion
388 (beginning-of-line)
389 (while (not (eq (point) old-line-start))
390 ;; Use insert-before-markers in case we're inserting
391 ;; before the saved value of point (which is common).
392 (insert-before-markers " ")
393 (forward-line -1))
394 t)))
395 (do-auto-fill)))
396
397 (defun mail-mode-fill-paragraph (arg)
398 ;; Do something special only if within the headers.
399 (if (< (point)
400 (save-excursion
401 (goto-char (point-min))
402 (if (re-search-forward
403 (concat "^" (regexp-quote mail-header-separator) "$")
404 nil t)
405 (point)
406 0)))
407 (let (beg end fieldname)
408 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
409 (setq beg (point))
410 (setq fieldname
411 (downcase (buffer-substring beg (1- (match-end 0)))))
412 (forward-line 1)
413 ;; Find continuation lines and get rid of their continuation markers.
414 (while (looking-at "[ \t]")
415 (delete-horizontal-space)
416 (forward-line 1))
417 (setq end (point-marker))
418 (goto-char beg)
419 ;; If this field contains addresses,
420 ;; make sure we can fill after each address.
421 (if (member fieldname
422 '("to" "cc" "bcc" "from" "reply-to"
423 "resent-to" "resent-cc" "resent-bcc"
424 "resent-from" "resent-reply-to"))
425 (while (search-forward "," end t)
426 (or (looking-at "[ \t]")
427 (insert " "))))
428 (fill-region-as-paragraph beg end)
429 ;; Mark all lines except the first as continuations.
430 (goto-char beg)
431 (forward-line 1)
432 (while (< (point) end)
433 (insert " ")
434 (forward-line 1))
435 (move-marker end nil)
436 t)))
437 \f
438 ;;; Set up keymap.
439
440 (if mail-mode-map
441 nil
442 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
443 (define-key mail-mode-map "\M-\t" 'mail-complete)
444 (define-key mail-mode-map "\C-c?" 'describe-mode)
445 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
446 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
447 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
448 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
449 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
450 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
451 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
452 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
453 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
454 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
455 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
456 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
457 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
458 (define-key mail-mode-map "\C-c\C-s" 'mail-send)
459 (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
460
461 (define-key mail-mode-map [menu-bar mail]
462 (cons "Mail" (make-sparse-keymap "Mail")))
463
464 (define-key mail-mode-map [menu-bar mail fill]
465 '("Fill Citation" . mail-fill-yanked-message))
466
467 (define-key mail-mode-map [menu-bar mail yank]
468 '("Cite Original" . mail-yank-original))
469
470 (define-key mail-mode-map [menu-bar mail signature]
471 '("Insert Signature" . mail-signature))
472
473 (define-key mail-mode-map [menu-bar mail mail-sep]
474 '("--"))
475
476 (define-key mail-mode-map [menu-bar mail cancel]
477 '("Cancel" . mail-dont-send))
478
479 (define-key mail-mode-map [menu-bar mail send-stay]
480 '("Send, Keep Editing" . mail-send))
481
482 (define-key mail-mode-map [menu-bar mail send]
483 '("Send Message" . mail-send-and-exit))
484
485 (define-key mail-mode-map [menu-bar headers]
486 (cons "Headers" (make-sparse-keymap "Move to Header")))
487
488 (define-key mail-mode-map [menu-bar headers text]
489 '("Text" . mail-text))
490
491 (define-key mail-mode-map [menu-bar headers expand-aliases]
492 '("Expand Aliases" . expand-mail-aliases))
493
494 (define-key mail-mode-map [menu-bar headers sent-via]
495 '("Sent Via" . mail-sent-via))
496
497 (define-key mail-mode-map [menu-bar headers reply-to]
498 '("Reply-To" . mail-reply-to))
499
500 (define-key mail-mode-map [menu-bar headers bcc]
501 '("Bcc" . mail-bcc))
502
503 (define-key mail-mode-map [menu-bar headers fcc]
504 '("Fcc" . mail-fcc))
505
506 (define-key mail-mode-map [menu-bar headers cc]
507 '("Cc" . mail-cc))
508
509 (define-key mail-mode-map [menu-bar headers subject]
510 '("Subject" . mail-subject))
511
512 (define-key mail-mode-map [menu-bar headers to]
513 '("To" . mail-to))
514 \f
515 ;; User-level commands for sending.
516
517 (defun mail-send-and-exit (arg)
518 "Send message like `mail-send', then, if no errors, exit from mail buffer.
519 Prefix arg means don't delete this window."
520 (interactive "P")
521 (mail-send)
522 (mail-bury arg))
523
524 (defun mail-dont-send (arg)
525 "Don't send the message you have been editing.
526 Prefix arg means don't delete this window."
527 (interactive "P")
528 (mail-bury arg))
529
530 (defun mail-bury (arg)
531 "Bury this mail buffer."
532 (let ((newbuf (other-buffer (current-buffer))))
533 (bury-buffer (current-buffer))
534 (if (and (or (window-dedicated-p (frame-selected-window))
535 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
536 (not (null (delq (selected-frame) (visible-frame-list)))))
537 (delete-frame (selected-frame))
538 (let (rmail-flag summary-buffer)
539 (and (not arg)
540 (not (one-window-p))
541 (save-excursion
542 (set-buffer (window-buffer (next-window (selected-window) 'not)))
543 (setq rmail-flag (eq major-mode 'rmail-mode))
544 (setq summary-buffer
545 (and mail-bury-selects-summary
546 (boundp 'rmail-summary-buffer)
547 rmail-summary-buffer
548 (buffer-name rmail-summary-buffer)
549 (not (get-buffer-window rmail-summary-buffer))
550 rmail-summary-buffer))))
551 (if rmail-flag
552 ;; If the Rmail buffer has a summary, show that.
553 (if summary-buffer (switch-to-buffer summary-buffer)
554 (delete-window))
555 (switch-to-buffer newbuf))))))
556
557 (defun mail-send ()
558 "Send the message in the current buffer.
559 If `mail-interactive' is non-nil, wait for success indication
560 or error messages, and inform user.
561 Otherwise any failure is reported in a message back to
562 the user from the mailer."
563 (interactive)
564 (if (if buffer-file-name
565 (y-or-n-p "Send buffer contents as mail message? ")
566 (or (buffer-modified-p)
567 (y-or-n-p "Message already sent; resend? ")))
568 (let ((inhibit-read-only t)
569 (opoint (point)))
570 (when (and enable-multibyte-characters
571 (not (eq mail-send-nonascii t)))
572 (goto-char (point-min))
573 (skip-chars-forward "\0-\177")
574 (or (= (point) (point-max))
575 (if (eq mail-send-nonascii 'query)
576 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
577 (error "Aborted"))
578 (error "Message contains non-ASCII characters"))))
579 ;; Complain about any invalid line.
580 (goto-char (point-min))
581 (while (not (looking-at (regexp-quote mail-header-separator)))
582 (unless (looking-at "[ \t]\\|.*:\\|$")
583 (push-mark opoint)
584 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
585 (forward-line 1))
586 (goto-char opoint)
587 (run-hooks 'mail-send-hook)
588 (message "Sending...")
589 (funcall send-mail-function)
590 ;; Now perform actions on successful sending.
591 (while mail-send-actions
592 (condition-case nil
593 (apply (car (car mail-send-actions))
594 (cdr (car mail-send-actions)))
595 (error))
596 (setq mail-send-actions (cdr mail-send-actions)))
597 (message "Sending...done")
598 ;; If buffer has no file, mark it as unmodified and delete autosave.
599 (if (not buffer-file-name)
600 (progn
601 (set-buffer-modified-p nil)
602 (delete-auto-save-file-if-necessary t))))))
603 \f
604 ;; This does the real work of sending a message via sendmail.
605 ;; It is called via the variable send-mail-function.
606
607 ;;;###autoload
608 (defvar sendmail-coding-system nil
609 "Coding system to which to encode the mail.")
610
611 (defun sendmail-send-it ()
612 (require 'mail-utils)
613 (let ((errbuf (if mail-interactive
614 (generate-new-buffer " sendmail errors")
615 0))
616 (tembuf (generate-new-buffer " sendmail temp"))
617 (case-fold-search nil)
618 resend-to-addresses
619 delimline
620 fcc-was-found
621 (mailbuf (current-buffer))
622 (sendmail-coding-system
623 (if (local-variable-p 'buffer-file-coding-system)
624 buffer-file-coding-system
625 (or sendmail-coding-system
626 default-buffer-file-coding-system
627 'iso-latin-1))))
628 (unwind-protect
629 (save-excursion
630 (set-buffer tembuf)
631 (erase-buffer)
632 (insert-buffer-substring mailbuf)
633 (goto-char (point-max))
634 ;; require one newline at the end.
635 (or (= (preceding-char) ?\n)
636 (insert ?\n))
637 ;; Change header-delimiter to be what sendmail expects.
638 (goto-char (point-min))
639 (re-search-forward
640 (concat "^" (regexp-quote mail-header-separator) "\n"))
641 (replace-match "\n")
642 (backward-char 1)
643 (setq delimline (point-marker))
644 (sendmail-sync-aliases)
645 (if mail-aliases
646 (expand-mail-aliases (point-min) delimline))
647 (goto-char (point-min))
648 ;; Ignore any blank lines in the header
649 (while (and (re-search-forward "\n\n\n*" delimline t)
650 (< (point) delimline))
651 (replace-match "\n"))
652 (goto-char (point-min))
653 (let ((case-fold-search t))
654 (goto-char (point-min))
655 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
656 (setq resend-to-addresses
657 (save-restriction
658 (narrow-to-region (point)
659 (save-excursion
660 (forward-line 1)
661 (while (looking-at "^[ \t]")
662 (forward-line 1))
663 (point)))
664 (append (mail-parse-comma-list)
665 resend-to-addresses)))
666 ;; Delete Resent-BCC ourselves
667 (if (save-excursion (beginning-of-line)
668 (looking-at "resent-bcc"))
669 (delete-region (save-excursion (beginning-of-line) (point))
670 (save-excursion (end-of-line) (1+ (point))))))
671 ;;; Apparently this causes a duplicate Sender.
672 ;;; ;; If the From is different than current user, insert Sender.
673 ;;; (goto-char (point-min))
674 ;;; (and (re-search-forward "^From:" delimline t)
675 ;;; (progn
676 ;;; (require 'mail-utils)
677 ;;; (not (string-equal
678 ;;; (mail-strip-quoted-names
679 ;;; (save-restriction
680 ;;; (narrow-to-region (point-min) delimline)
681 ;;; (mail-fetch-field "From")))
682 ;;; (user-login-name))))
683 ;;; (progn
684 ;;; (forward-line 1)
685 ;;; (insert "Sender: " (user-login-name) "\n")))
686 ;; Don't send out a blank subject line
687 (goto-char (point-min))
688 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
689 (replace-match "")
690 ;; This one matches a Subject just before the header delimiter.
691 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
692 (= (match-end 0) delimline))
693 (replace-match "")))
694 ;; Put the "From:" field in unless for some odd reason
695 ;; they put one in themselves.
696 (goto-char (point-min))
697 (if (not (re-search-forward "^From:" delimline t))
698 (let* ((login user-mail-address)
699 (fullname (user-full-name))
700 (quote-fullname nil))
701 (if (string-match "[\200-\377]" fullname)
702 (setq fullname (mail-quote-printable fullname t)
703 quote-fullname t))
704 (cond ((eq mail-from-style 'angles)
705 (insert "From: " fullname)
706 (let ((fullname-start (+ (point-min) 6))
707 (fullname-end (point-marker)))
708 (goto-char fullname-start)
709 ;; Look for a character that cannot appear unquoted
710 ;; according to RFC 822.
711 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
712 fullname-end 1)
713 quote-fullname)
714 (progn
715 ;; Quote fullname, escaping specials.
716 (goto-char fullname-start)
717 (insert "\"")
718 (while (re-search-forward "[\"\\]"
719 fullname-end 1)
720 (replace-match "\\\\\\&" t))
721 (insert "\""))))
722 (insert " <" login ">\n"))
723 ((eq mail-from-style 'parens)
724 (insert "From: " login " (")
725 (let ((fullname-start (point)))
726 (if quote-fullname
727 (insert "\""))
728 (insert fullname)
729 (if quote-fullname
730 (insert "\""))
731 (let ((fullname-end (point-marker)))
732 (goto-char fullname-start)
733 ;; RFC 822 says \ and nonmatching parentheses
734 ;; must be escaped in comments.
735 ;; Escape every instance of ()\ ...
736 (while (re-search-forward "[()\\]" fullname-end 1)
737 (replace-match "\\\\\\&" t))
738 ;; ... then undo escaping of matching parentheses,
739 ;; including matching nested parentheses.
740 (goto-char fullname-start)
741 (while (re-search-forward
742 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
743 fullname-end 1)
744 (replace-match "\\1(\\3)" t)
745 (goto-char fullname-start))))
746 (insert ")\n"))
747 ((null mail-from-style)
748 (insert "From: " login "\n")))))
749 ;; Insert an extra newline if we need it to work around
750 ;; Sun's bug that swallows newlines.
751 (goto-char (1+ delimline))
752 (if (eval mail-mailer-swallows-blank-line)
753 (newline))
754 ;; Find and handle any FCC fields.
755 (goto-char (point-min))
756 (if (re-search-forward "^FCC:" delimline t)
757 (progn
758 (setq fcc-was-found t)
759 (mail-do-fcc delimline)))
760 (if mail-interactive
761 (save-excursion
762 (set-buffer errbuf)
763 (erase-buffer))))
764 (goto-char (point-min))
765 (if (let ((case-fold-search t))
766 (re-search-forward "^To:\\|^cc:\\|^bcc:\\|^resent-to:\
767 \\|^resent-cc:\\|^resent-bcc:"
768 delimline t))
769 (let ((default-directory "/")
770 (coding-system-for-write sendmail-coding-system))
771 (apply 'call-process-region
772 (append (list (point-min) (point-max)
773 (if (boundp 'sendmail-program)
774 sendmail-program
775 "/usr/lib/sendmail")
776 nil errbuf nil "-oi")
777 ;; Always specify who from,
778 ;; since some systems have broken sendmails.
779 (list "-f" (user-login-name))
780 ;;; ;; Don't say "from root" if running under su.
781 ;;; (and (equal (user-real-login-name) "root")
782 ;;; (list "-f" (user-login-name)))
783 (and mail-alias-file
784 (list (concat "-oA" mail-alias-file)))
785 (if mail-interactive
786 ;; These mean "report errors to terminal"
787 ;; and "deliver interactively"
788 '("-oep" "-odi")
789 ;; These mean "report errors by mail"
790 ;; and "deliver in background".
791 '("-oem" "-odb"))
792 ;; Get the addresses from the message
793 ;; unless this is a resend.
794 ;; We must not do that for a resend
795 ;; because we would find the original addresses.
796 ;; For a resend, include the specific addresses.
797 (or resend-to-addresses
798 '("-t")))))
799 (or fcc-was-found
800 (error "No recipients")))
801 (if mail-interactive
802 (save-excursion
803 (set-buffer errbuf)
804 (goto-char (point-min))
805 (while (re-search-forward "\n\n* *" nil t)
806 (replace-match "; "))
807 (if (not (zerop (buffer-size)))
808 (error "Sending...failed to %s"
809 (buffer-substring (point-min) (point-max)))))))
810 (kill-buffer tembuf)
811 (if (bufferp errbuf)
812 (kill-buffer errbuf)))))
813
814 (defun mail-do-fcc (header-end)
815 (let (fcc-list
816 (rmailbuf (current-buffer))
817 (time (current-time))
818 (tembuf (generate-new-buffer " rmail output"))
819 (case-fold-search t))
820 (save-excursion
821 (goto-char (point-min))
822 (while (re-search-forward "^FCC:[ \t]*" header-end t)
823 (setq fcc-list (cons (buffer-substring (point)
824 (progn
825 (end-of-line)
826 (skip-chars-backward " \t")
827 (point)))
828 fcc-list))
829 (delete-region (match-beginning 0)
830 (progn (forward-line 1) (point))))
831 (set-buffer tembuf)
832 (erase-buffer)
833 ;; This initial newline is written out if the fcc file already exists.
834 (insert "\nFrom " (user-login-name) " "
835 (current-time-string time) "\n")
836 ;; Insert the time zone before the year.
837 (forward-char -1)
838 (forward-word -1)
839 (require 'mail-utils)
840 (insert (mail-rfc822-time-zone time) " ")
841 (goto-char (point-max))
842 (insert-buffer-substring rmailbuf)
843 ;; Make sure messages are separated.
844 (goto-char (point-max))
845 (insert ?\n)
846 (goto-char 2)
847 ;; ``Quote'' "^From " as ">From "
848 ;; (note that this isn't really quoting, as there is no requirement
849 ;; that "^[>]+From " be quoted in the same transparent way.)
850 (let ((case-fold-search nil))
851 (while (search-forward "\nFrom " nil t)
852 (forward-char -5)
853 (insert ?>)))
854 (while fcc-list
855 (let* ((buffer (find-buffer-visiting (car fcc-list)))
856 (curbuf (current-buffer))
857 dont-write-the-file
858 buffer-matches-file
859 (beg (point-min)) (end (point-max))
860 (beg2 (save-excursion (goto-char (point-min))
861 (forward-line 2) (point))))
862 (if buffer
863 ;; File is present in a buffer => append to that buffer.
864 (save-excursion
865 (set-buffer buffer)
866 (setq buffer-matches-file
867 (and (not (buffer-modified-p))
868 (verify-visited-file-modtime buffer)))
869 ;; Keep the end of the accessible portion at the same place
870 ;; unless it is the end of the buffer.
871 (let ((max (if (/= (1+ (buffer-size)) (point-max))
872 (point-max))))
873 (unwind-protect
874 ;; Code below lifted from rmailout.el
875 ;; function rmail-output-to-rmail-file:
876 (let ((buffer-read-only nil)
877 (msg (and (boundp 'rmail-current-message)
878 rmail-current-message)))
879 ;; If MSG is non-nil, buffer is in RMAIL mode.
880 (if msg
881 (progn
882 (rmail-maybe-set-message-counters)
883 (widen)
884 (narrow-to-region (point-max) (point-max))
885 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
886 "Date: " (mail-rfc822-date) "\n")
887 (insert-buffer-substring curbuf beg2 end)
888 (insert "\n\C-_")
889 (goto-char (point-min))
890 (widen)
891 (search-backward "\n\^_")
892 (narrow-to-region (point) (point-max))
893 (rmail-count-new-messages t)
894 (rmail-show-message msg)
895 (setq max nil))
896 ;; Output file not in rmail mode
897 ;; => just insert at the end.
898 (narrow-to-region (point-min) (1+ (buffer-size)))
899 (goto-char (point-max))
900 (insert-buffer-substring curbuf beg end))
901 (or buffer-matches-file
902 (progn
903 (if (y-or-n-p (format "Save file %s? "
904 (car fcc-list)))
905 (save-buffer))
906 (setq dont-write-the-file t))))
907 (if max (narrow-to-region (point-min) max))))))
908 ;; Append to the file directly,
909 ;; unless we've already taken care of it.
910 (unless dont-write-the-file
911 (if (and (file-exists-p (car fcc-list))
912 (mail-file-babyl-p (car fcc-list)))
913 ;; If the file is a Babyl file,
914 ;; convert the message to Babyl format.
915 (let ((coding-system-for-write
916 (or rmail-file-coding-system
917 'emacs-mule)))
918 (save-excursion
919 (set-buffer (get-buffer-create " mail-temp"))
920 (setq buffer-read-only nil)
921 (erase-buffer)
922 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
923 "Date: " (mail-rfc822-date) "\n")
924 (insert-buffer-substring curbuf beg2 end)
925 (insert "\n\C-_")
926 (write-region (point-min) (point-max) (car fcc-list) t)
927 (erase-buffer)))
928 (write-region
929 (1+ (point-min)) (point-max) (car fcc-list) t)))
930 (and buffer (not dont-write-the-file)
931 (with-current-buffer buffer
932 (set-visited-file-modtime))))
933 (setq fcc-list (cdr fcc-list))))
934 (kill-buffer tembuf)))
935
936 (defun mail-sent-via ()
937 "Make a Sent-via header line from each To or CC header line."
938 (interactive)
939 (save-excursion
940 (goto-char (point-min))
941 ;; find the header-separator
942 (search-forward (concat "\n" mail-header-separator "\n"))
943 (forward-line -1)
944 ;; put a marker at the end of the header
945 (let ((end (point-marker))
946 (case-fold-search t)
947 to-line)
948 (goto-char (point-min))
949 ;; search for the To: lines and make Sent-via: lines from them
950 ;; search for the next To: line
951 (while (re-search-forward "^\\(to\\|cc\\):" end t)
952 ;; Grab this line plus all its continuations, sans the `to:'.
953 (let ((to-line
954 (buffer-substring (point)
955 (progn
956 (if (re-search-forward "^[^ \t\n]" end t)
957 (backward-char 1)
958 (goto-char end))
959 (point)))))
960 ;; Insert a copy, with altered header field name.
961 (insert-before-markers "Sent-via:" to-line))))))
962 \f
963 (defun mail-to ()
964 "Move point to end of To-field."
965 (interactive)
966 (expand-abbrev)
967 (mail-position-on-field "To"))
968
969 (defun mail-subject ()
970 "Move point to end of Subject-field."
971 (interactive)
972 (expand-abbrev)
973 (mail-position-on-field "Subject"))
974
975 (defun mail-cc ()
976 "Move point to end of CC-field. Create a CC field if none."
977 (interactive)
978 (expand-abbrev)
979 (or (mail-position-on-field "cc" t)
980 (progn (mail-position-on-field "to")
981 (insert "\nCC: "))))
982
983 (defun mail-bcc ()
984 "Move point to end of BCC-field. Create a BCC field if none."
985 (interactive)
986 (expand-abbrev)
987 (or (mail-position-on-field "bcc" t)
988 (progn (mail-position-on-field "to")
989 (insert "\nBCC: "))))
990
991 (defun mail-fcc (folder)
992 "Add a new FCC field, with file name completion."
993 (interactive "FFolder carbon copy: ")
994 (expand-abbrev)
995 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
996 (mail-position-on-field "to"))
997 (insert "\nFCC: " folder))
998
999 (defun mail-reply-to ()
1000 "Move point to end of Reply-To-field."
1001 (interactive)
1002 (expand-abbrev)
1003 (mail-position-on-field "Reply-To"))
1004
1005 (defun mail-position-on-field (field &optional soft)
1006 (let (end
1007 (case-fold-search t))
1008 (goto-char (point-min))
1009 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1010 (setq end (match-beginning 0))
1011 (goto-char (point-min))
1012 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1013 (progn
1014 (re-search-forward "^[^ \t]" nil 'move)
1015 (beginning-of-line)
1016 (skip-chars-backward "\n")
1017 t)
1018 (or soft
1019 (progn (goto-char end)
1020 (insert field ": \n")
1021 (skip-chars-backward "\n")))
1022 nil)))
1023
1024 (defun mail-text ()
1025 "Move point to beginning of message text."
1026 (interactive)
1027 (expand-abbrev)
1028 (goto-char (point-min))
1029 (search-forward (concat "\n" mail-header-separator "\n")))
1030 \f
1031 (defun mail-signature (atpoint)
1032 "Sign letter with contents of the file `mail-signature-file'.
1033 Prefix arg means put contents at point."
1034 (interactive "P")
1035 (save-excursion
1036 (or atpoint
1037 (goto-char (point-max)))
1038 (skip-chars-backward " \t\n")
1039 (end-of-line)
1040 (or atpoint
1041 (delete-region (point) (point-max)))
1042 (insert "\n\n-- \n")
1043 (insert-file-contents (expand-file-name mail-signature-file))))
1044
1045 (defun mail-fill-yanked-message (&optional justifyp)
1046 "Fill the paragraphs of a message yanked into this one.
1047 Numeric argument means justify as well."
1048 (interactive "P")
1049 (save-excursion
1050 (goto-char (point-min))
1051 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1052 (fill-individual-paragraphs (point)
1053 (point-max)
1054 justifyp
1055 t)))
1056
1057 (defun mail-indent-citation ()
1058 "Modify text just inserted from a message to be cited.
1059 The inserted text should be the region.
1060 When this function returns, the region is again around the modified text.
1061
1062 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1063 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1064 (mail-yank-clear-headers (region-beginning) (region-end))
1065 (if (null mail-yank-prefix)
1066 (indent-rigidly (region-beginning) (region-end)
1067 mail-indentation-spaces)
1068 (save-excursion
1069 (let ((end (set-marker (make-marker) (region-end))))
1070 (goto-char (region-beginning))
1071 (while (< (point) end)
1072 (insert mail-yank-prefix)
1073 (forward-line 1))))))
1074
1075 (defun mail-yank-original (arg)
1076 "Insert the message being replied to, if any (in rmail).
1077 Puts point after the text and mark before.
1078 Normally, indents each nonblank line ARG spaces (default 3).
1079 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1080
1081 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1082 and don't delete any header fields."
1083 (interactive "P")
1084 (if mail-reply-action
1085 (let ((start (point))
1086 (original mail-reply-action))
1087 (and (consp original) (eq (car original) 'insert-buffer)
1088 (setq original (nth 1 original)))
1089 (if (consp original)
1090 (apply (car original) (cdr original))
1091 ;; If the original message is in another window in the same frame,
1092 ;; delete that window to save screen space.
1093 ;; t means don't alter other frames.
1094 (delete-windows-on original t)
1095 (insert-buffer original))
1096 (if (consp arg)
1097 nil
1098 (goto-char start)
1099 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1100 mail-indentation-spaces))
1101 (mark-even-if-inactive t))
1102 (if mail-citation-hook
1103 (run-hooks 'mail-citation-hook)
1104 (if mail-yank-hooks
1105 (run-hooks 'mail-yank-hooks)
1106 (mail-indent-citation)))))
1107 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1108 ;; It is cleaner to avoid activation, even though the command
1109 ;; loop would deactivate the mark because we inserted text.
1110 (goto-char (prog1 (mark t)
1111 (set-marker (mark-marker) (point) (current-buffer))))
1112 (if (not (eolp)) (insert ?\n)))))
1113
1114 (defun mail-yank-clear-headers (start end)
1115 (if (< end start)
1116 (let (temp)
1117 (setq temp start start end end temp)))
1118 (if mail-yank-ignored-headers
1119 (save-excursion
1120 (goto-char start)
1121 (if (search-forward "\n\n" end t)
1122 (save-restriction
1123 (narrow-to-region start (point))
1124 (goto-char start)
1125 (while (let ((case-fold-search t))
1126 (re-search-forward mail-yank-ignored-headers nil t))
1127 (beginning-of-line)
1128 (delete-region (point)
1129 (progn (re-search-forward "\n[^ \t]")
1130 (forward-char -1)
1131 (point)))))))))
1132
1133 (defun mail-yank-region (arg)
1134 "Insert the selected region from the message being replied to.
1135 Puts point after the text and mark before.
1136 Normally, indents each nonblank line ARG spaces (default 3).
1137 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1138
1139 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1140 and don't delete any header fields."
1141 (interactive "P")
1142 (and (consp mail-reply-action)
1143 (eq (car mail-reply-action) 'insert-buffer)
1144 (let ((buffer (nth 1 mail-reply-action))
1145 (start (point)))
1146 ;; Insert the citation text.
1147 (insert (with-current-buffer buffer
1148 (buffer-substring (point) (mark))))
1149 (push-mark start)
1150 ;; Indent or otherwise annotate the citation text.
1151 (if (consp arg)
1152 nil
1153 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1154 mail-indentation-spaces)))
1155 (if mail-citation-hook
1156 (run-hooks 'mail-citation-hook)
1157 (if mail-yank-hooks
1158 (run-hooks 'mail-yank-hooks)
1159 (mail-indent-citation))))))))
1160 \f
1161 (defun mail-attach-file (&optional file)
1162 "Insert a file at the end of the buffer, with separator lines around it."
1163 (interactive "fAttach file: ")
1164 (save-excursion
1165 (goto-char (point-max))
1166 (or (bolp) (newline))
1167 (newline)
1168 (let ((start (point))
1169 middle)
1170 (insert (format "===File %s===" file))
1171 (insert-char ?= (max 0 (- 60 (current-column))))
1172 (newline)
1173 (setq middle (point))
1174 (insert "============================================================\n")
1175 (push-mark)
1176 (goto-char middle)
1177 (insert-file-contents file)
1178 (or (bolp) (newline))
1179 (goto-char start))))
1180 \f
1181 ;; Put these commands last, to reduce chance of lossage from quitting
1182 ;; in middle of loading the file.
1183
1184 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1185
1186 ;;;###autoload
1187 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1188 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1189 When this function returns, the buffer `*mail*' is selected.
1190 The value is t if the message was newly initialized; otherwise, nil.
1191
1192 Optionally, the signature file `mail-signature-file' can be inserted at the
1193 end; see the variable `mail-signature'.
1194
1195 \\<mail-mode-map>
1196 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1197
1198 Various special commands starting with C-c are available in sendmail mode
1199 to move to message header fields:
1200 \\{mail-mode-map}
1201
1202 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1203 when the message is initialized.
1204
1205 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1206 a Reply-to: field with that address is inserted.
1207
1208 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1209 is inserted.
1210
1211 The normal hook `mail-setup-hook' is run after the message is
1212 initialized. It can add more default fields to the message.
1213
1214 When calling from a program, the first argument if non-nil says
1215 not to erase the existing contents of the `*mail*' buffer.
1216
1217 The second through fifth arguments,
1218 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1219 the initial contents of those header fields.
1220 These arguments should not have final newlines.
1221 The sixth argument REPLYBUFFER is a buffer which contains an
1222 original message being replied to, or else an action
1223 of the form (FUNCTION . ARGS) which says how to insert the original.
1224 Or it can be nil, if not replying to anything.
1225 The seventh argument ACTIONS is a list of actions to take
1226 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1227 when the message is sent, we apply FUNCTION to ARGS.
1228 This is how Rmail arranges to mark messages `answered'."
1229 (interactive "P")
1230 ;;; This is commented out because I found it was confusing in practice.
1231 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1232 ;;; if you want to have multiple mail buffers.
1233 ;;; And then you can control which messages to save. --rms.
1234 ;;; (let ((index 1)
1235 ;;; buffer)
1236 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
1237 ;;; (if noerase
1238 ;;; (progn
1239 ;;; (while (and (setq buffer
1240 ;;; (get-buffer (if (= 1 index) "*mail*"
1241 ;;; (format "*mail*<%d>" index))))
1242 ;;; (not (buffer-modified-p buffer)))
1243 ;;; (setq index (1+ index)))
1244 ;;; (if buffer (switch-to-buffer buffer)
1245 ;;; ;; If none exists, start a new message.
1246 ;;; ;; This will never re-use an existing unmodified mail buffer
1247 ;;; ;; (since index is not 1 anymore). Perhaps it should.
1248 ;;; (setq noerase nil))))
1249 ;;; ;; Unless we found a modified message and are happy, start a new message.
1250 ;;; (if (not noerase)
1251 ;;; (progn
1252 ;;; ;; Look for existing unmodified mail buffer.
1253 ;;; (while (and (setq buffer
1254 ;;; (get-buffer (if (= 1 index) "*mail*"
1255 ;;; (format "*mail*<%d>" index))))
1256 ;;; (buffer-modified-p buffer))
1257 ;;; (setq index (1+ index)))
1258 ;;; ;; If none, make a new one.
1259 ;;; (or buffer
1260 ;;; (setq buffer (generate-new-buffer "*mail*")))
1261 ;;; ;; Go there and initialize it.
1262 ;;; (switch-to-buffer buffer)
1263 ;;; (erase-buffer)
1264 ;;; (setq default-directory (expand-file-name "~/"))
1265 ;;; (auto-save-mode auto-save-default)
1266 ;;; (mail-mode)
1267 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1268 ;;; (if (and buffer-auto-save-file-name
1269 ;;; (file-exists-p buffer-auto-save-file-name))
1270 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1271 ;;; t))
1272 (pop-to-buffer "*mail*")
1273 ;; Put the auto-save file in the home dir
1274 ;; to avoid any danger that it can't be written.
1275 (if (file-exists-p (expand-file-name "~/"))
1276 (setq default-directory (expand-file-name "~/")))
1277 (auto-save-mode auto-save-default)
1278 (mail-mode)
1279 ;; Disconnect the buffer from its visited file
1280 ;; (in case the user has actually visited a file *mail*).
1281 ; (set-visited-file-name nil)
1282 (let (initialized)
1283 (and (not noerase)
1284 (if buffer-file-name
1285 (if (buffer-modified-p)
1286 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1287 (if (y-or-n-p "Disconnect buffer from visited file? ")
1288 (set-visited-file-name nil))
1289 t)
1290 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1291 (set-visited-file-name nil)
1292 t))
1293 ;; A non-file-visiting buffer.
1294 (if (buffer-modified-p)
1295 (y-or-n-p "Unsent message being composed; erase it? ")
1296 t))
1297 (let ((inhibit-read-only t))
1298 (erase-buffer)
1299 (mail-setup to subject in-reply-to cc replybuffer actions)
1300 (setq initialized t)))
1301 (if (and buffer-auto-save-file-name
1302 (file-exists-p buffer-auto-save-file-name))
1303 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1304 initialized))
1305
1306 (defun mail-recover ()
1307 "Reread contents of current buffer from its last auto-save file."
1308 (interactive)
1309 (let ((file-name (make-auto-save-file-name)))
1310 (cond ((save-window-excursion
1311 (if (not (eq system-type 'vax-vms))
1312 (with-output-to-temp-buffer "*Directory*"
1313 (buffer-disable-undo standard-output)
1314 (let ((default-directory "/"))
1315 (call-process
1316 "ls" nil standard-output nil "-l" file-name))))
1317 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1318 (let ((buffer-read-only nil))
1319 (erase-buffer)
1320 (insert-file-contents file-name nil)))
1321 (t (error "mail-recover cancelled")))))
1322
1323 ;;;###autoload
1324 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1325 "Like `mail' command, but display mail buffer in another window."
1326 (interactive "P")
1327 (let ((pop-up-windows t)
1328 (special-display-buffer-names nil)
1329 (special-display-regexps nil)
1330 (same-window-buffer-names nil)
1331 (same-window-regexps nil))
1332 (pop-to-buffer "*mail*"))
1333 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1334
1335 ;;;###autoload
1336 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1337 "Like `mail' command, but display mail buffer in another frame."
1338 (interactive "P")
1339 (let ((pop-up-frames t)
1340 (special-display-buffer-names nil)
1341 (special-display-regexps nil)
1342 (same-window-buffer-names nil)
1343 (same-window-regexps nil))
1344 (pop-to-buffer "*mail*"))
1345 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1346
1347 ;;; Do not add anything but external entries on this page.
1348
1349 (provide 'sendmail)
1350
1351 ;;; sendmail.el ends here