(mail-setup-hook): New defvar.
[emacs.git] / lisp / mail / sendmail.el
blob5c33f370e939c9d3a19813fcd1a6e8e6cf7168f5
1 ;;; sendmail.el --- mail sending commands for Emacs.
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: mail
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)
13 ;; any later version.
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.
24 ;;; Commentary:
26 ;; This mode provides mail-sending facilities from within Emacs. It is
27 ;; documented in the Emacs user's manual.
29 ;;; Code:
31 ;;;###autoload
32 (defvar mail-from-style 'angles "\
33 *Specifies how \"From:\" fields look.
35 If `nil', they contain just the return address like:
36 king@grassland.com
37 If `parens', they look like:
38 king@grassland.com (Elvis Parsley)
39 If `angles', they look like:
40 Elvis Parsley <king@grassland.com>")
42 ;;;###autoload
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.")
48 ;;;###autoload
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.")
53 ;;;###autoload
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
58 ;;;###autoload
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'.")
64 ;;;###autoload
65 (defvar mail-header-separator "--text follows this line--" "\
66 *Line used to separate headers from text in messages being composed.")
68 ;;;###autoload
69 (defvar mail-archive-file-name nil "\
70 *Name of file to write all outgoing messages in, or nil for none.
71 This can be an inbox file or an Rmail file.")
73 ;;;###autoload
74 (defvar mail-default-reply-to nil
75 "*Address to insert as default Reply-to field of outgoing messages.")
77 ;;;###autoload
78 (defvar mail-alias-file nil
79 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
80 This file defines aliases to be expanded by the mailer; this is a different
81 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
82 This variable has no effect unless your system uses sendmail as its mailer.")
84 ;;;###autoload
85 (defvar mail-personal-alias-file "~/.mailrc"
86 "*If non-nil, the name of the user's personal mail alias file.
87 This file typically should be in same format as the `.mailrc' file used by
88 the `Mail' or `mailx' program.
89 This file need not actually exist.")
91 (defvar mail-setup-hook nil
92 "Normal hook, run each time a new outgoing mail message is initialized.
93 The function `mail-setup' runs this hook.")
95 (defvar mail-aliases t
96 "Alist of mail address aliases,
97 or t meaning should be initialized from your mail aliases file.
98 \(The file's name is normally `~/.mailrc', but your MAILRC environment
99 variable can override that name.)
100 The alias definitions in the file have this form:
101 alias ALIAS MEANING")
103 (defvar mail-alias-modtime nil
104 "The modification time of your mail alias file when it was last examined.")
106 (defvar mail-yank-prefix nil
107 "*Prefix insert on lines of yanked message being replied to.
108 nil means use indentation.")
109 (defvar mail-indentation-spaces 3
110 "*Number of spaces to insert at the beginning of each cited line.
111 Used by `mail-yank-original' via `mail-yank-cite'.")
112 (defvar mail-yank-hooks nil
113 "Obsolete hook for modifying a citation just inserted in the mail buffer.
114 Each hook function can find the citation between (point) and (mark t).
115 And each hook function should leave point and mark around the citation
116 text as modified.
118 This is a normal hook, misnamed for historical reasons.
119 It is semi-obsolete and mail agents should no longer use it.")
121 (defvar mail-citation-hook nil
122 "*Hook for modifying a citation just inserted in the mail buffer.
123 Each hook function can find the citation between (point) and (mark t).
124 And each hook function should leave point and mark around the citation
125 text as modified.
127 If this hook is entirely empty (nil), a default action is taken
128 instead of no action.")
130 (defvar mail-abbrevs-loaded nil)
131 (defvar mail-mode-map nil)
133 (autoload 'build-mail-aliases "mailalias"
134 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
135 nil)
137 (autoload 'expand-mail-aliases "mailalias"
138 "Expand all mail aliases in suitable header fields found between BEG and END.
139 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
140 Optional second arg EXCLUDE may be a regular expression defining text to be
141 removed from alias expansions."
142 nil)
144 ;;;###autoload
145 (defvar mail-signature nil
146 "*Text inserted at end of mail buffer when a message is initialized.
147 If t, it means to insert the contents of the file `~/.signature'.")
149 (defvar mail-reply-buffer nil)
150 (defvar mail-send-actions nil
151 "A list of actions to be performed upon successful sending of a message.")
153 (defvar mail-default-headers nil
154 "*A string containing header lines, to be inserted in outgoing messages.
155 It is inserted before you edit the message,
156 so you can edit or delete these lines.")
158 (defvar mail-bury-selects-summary t
159 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
160 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
161 the RMAIL summary buffer before returning, if it exists and this variable
162 is non-nil.")
164 ;; Note: could use /usr/ucb/mail instead of sendmail;
165 ;; options -t, and -v if not interactive.
166 (defvar mail-mailer-swallows-blank-line
167 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
168 (file-readable-p "/etc/sendmail.cf")
169 (let ((buffer (get-buffer-create " *temp*")))
170 (unwind-protect
171 (save-excursion
172 (set-buffer buffer)
173 (insert-file-contents "/etc/sendmail.cf")
174 (goto-char (point-min))
175 (let ((case-fold-search nil))
176 (re-search-forward "^OR\\>" nil t)))
177 (kill-buffer buffer))))
178 ;; According to RFC822, "The field-name must be composed of printable
179 ;; ASCII characters (i.e. characters that have decimal values between
180 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
181 ;; space, or colon.
182 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
183 "Set this non-nil if the system's mailer runs the header and body together.
184 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
185 The value should be an expression to test whether the problem will
186 actually occur.")
188 (defvar mail-mode-syntax-table nil
189 "Syntax table used while in mail mode.")
191 (if (not mail-mode-syntax-table)
192 (progn
193 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
194 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
196 (defvar mail-font-lock-keywords
197 (list '("^To:" . font-lock-function-name-face)
198 '("^B?CC:\\|^Reply-To:" . font-lock-keyword-face)
199 '("^Subject:" . font-lock-comment-face)
200 '("^Subject:\\s *\\(.+\\)" 1 font-lock-type-face)
201 (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$") 1
202 'font-lock-comment-face)
203 '("^[ \t]*\\sw*[>|}].*" . font-lock-reference-face) ; Citation.
204 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*" . font-lock-string-face))
205 "Additional expressions to highlight in Mail mode.")
207 (defvar mail-send-hook nil
208 "Normal hook run before sending mail, in Mail mode.")
210 (defun sendmail-synch-aliases ()
211 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
212 (or (equal mail-alias-modtime modtime)
213 (setq mail-alias-modtime modtime
214 mail-aliases t))))
216 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
217 (or mail-default-reply-to
218 (setq mail-default-reply-to (getenv "REPLYTO")))
219 (sendmail-synch-aliases)
220 (if (eq mail-aliases t)
221 (progn
222 (setq mail-aliases nil)
223 (if (file-exists-p mail-personal-alias-file)
224 (build-mail-aliases))))
225 (setq mail-send-actions actions)
226 (setq mail-reply-buffer replybuffer)
227 (goto-char (point-min))
228 (insert "To: ")
229 (save-excursion
230 (if to
231 ;; Here removed code to extract names from within <...>
232 ;; on the assumption that mail-strip-quoted-names
233 ;; has been called and has done so.
234 (let ((fill-prefix "\t")
235 (address-start (point)))
236 (insert to "\n")
237 (fill-region-as-paragraph address-start (point-max)))
238 (newline))
239 (if cc
240 (let ((fill-prefix "\t")
241 (address-start (progn (insert "CC: ") (point))))
242 (insert cc "\n")
243 (fill-region-as-paragraph address-start (point-max))))
244 (if in-reply-to
245 (let ((fill-prefix "\t")
246 (fill-column 78)
247 (address-start (point)))
248 (insert "In-reply-to: " in-reply-to "\n")
249 (fill-region-as-paragraph address-start (point-max))))
250 (insert "Subject: " (or subject "") "\n")
251 (if mail-default-headers
252 (insert mail-default-headers))
253 (if mail-default-reply-to
254 (insert "Reply-to: " mail-default-reply-to "\n"))
255 (if mail-self-blind
256 (insert "BCC: " (user-login-name) "\n"))
257 (if mail-archive-file-name
258 (insert "FCC: " mail-archive-file-name "\n"))
259 (insert mail-header-separator "\n")
260 ;; Insert the signature. But remember the beginning of the message.
261 (if to (setq to (point)))
262 (cond ((eq mail-signature t)
263 (if (file-exists-p "~/.signature")
264 (progn
265 (insert "\n\n-- \n")
266 (insert-file-contents "~/.signature"))))
267 (mail-signature
268 (insert mail-signature)))
269 (goto-char (point-max))
270 (or (bolp) (newline)))
271 (if to (goto-char to))
272 (or to subject in-reply-to
273 (set-buffer-modified-p nil))
274 (run-hooks 'mail-setup-hook))
276 ;;;###autoload
277 (defun mail-mode ()
278 "Major mode for editing mail to be sent.
279 Like Text Mode but with these additional commands:
280 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
281 C-c C-f move to a header field (and create it if there isn't):
282 C-c C-f C-t move to To: C-c C-f C-s move to Subject:
283 C-c C-f C-c move to CC: C-c C-f C-b move to BCC:
284 C-c C-f C-f move to FCC:
285 C-c C-t mail-text (move to beginning of message text).
286 C-c C-w mail-signature (insert `~/.signature' file).
287 C-c C-y mail-yank-original (insert current message, in Rmail).
288 C-c C-q mail-fill-yanked-message (fill what was yanked).
289 C-c C-v mail-sent-via (add a Sent-via field for each To or CC)."
290 (interactive)
291 (kill-all-local-variables)
292 (make-local-variable 'mail-reply-buffer)
293 (setq mail-reply-buffer nil)
294 (make-local-variable 'mail-send-actions)
295 (set-syntax-table mail-mode-syntax-table)
296 (use-local-map mail-mode-map)
297 (setq local-abbrev-table text-mode-abbrev-table)
298 (setq major-mode 'mail-mode)
299 (setq mode-name "Mail")
300 (setq buffer-offer-save t)
301 (make-local-variable 'font-lock-defaults)
302 (setq font-lock-defaults '(mail-font-lock-keywords t))
303 (make-local-variable 'paragraph-separate)
304 (make-local-variable 'paragraph-start)
305 (setq paragraph-start (concat (regexp-quote mail-header-separator)
306 "$\\|[ \t]*[-_][-_][-_]+$\\|"
307 paragraph-start))
308 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
309 "$\\|[ \t]*[-_][-_][-_]+$\\|"
310 paragraph-separate))
311 (run-hooks 'text-mode-hook 'mail-mode-hook))
313 ;;; Set up keymap.
315 (if mail-mode-map
317 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
318 (define-key mail-mode-map "\C-c?" 'describe-mode)
319 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
320 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
321 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
322 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
323 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
324 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
325 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
326 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
327 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
328 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
329 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
330 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
331 (define-key mail-mode-map "\C-c\C-s" 'mail-send))
333 (define-key mail-mode-map [menu-bar mail]
334 (cons "Mail" (make-sparse-keymap "Mail")))
336 (define-key mail-mode-map [menu-bar mail fill]
337 '("Fill Citation" . mail-fill-yanked-message))
339 (define-key mail-mode-map [menu-bar mail yank]
340 '("Cite Original" . mail-yank-original))
342 (define-key mail-mode-map [menu-bar mail signature]
343 '("Insert Signature" . mail-signature))
345 (define-key mail-mode-map [menu-bar mail cancel]
346 '("Cancel" . mail-dont-send))
348 (define-key mail-mode-map [menu-bar mail send-stay]
349 '("Send, Keep Editing" . mail-send))
351 (define-key mail-mode-map [menu-bar mail send]
352 '("Send Message" . mail-send-and-exit))
354 (define-key mail-mode-map [menu-bar headers]
355 (cons "Headers" (make-sparse-keymap "Move to Header")))
357 (define-key mail-mode-map [menu-bar headers reply-to]
358 '("Reply-To" . mail-reply-to))
360 (define-key mail-mode-map [menu-bar headers sent-via]
361 '("Sent Via" . mail-sent-via))
363 (define-key mail-mode-map [menu-bar headers text]
364 '("Text" . mail-text))
366 (define-key mail-mode-map [menu-bar headers bcc]
367 '("Bcc" . mail-bcc))
369 (define-key mail-mode-map [menu-bar headers fcc]
370 '("Fcc" . mail-fcc))
372 (define-key mail-mode-map [menu-bar headers cc]
373 '("Cc" . mail-cc))
375 (define-key mail-mode-map [menu-bar headers subject]
376 '("Subject" . mail-subject))
378 (define-key mail-mode-map [menu-bar headers to]
379 '("To" . mail-to))
381 (defun mail-send-and-exit (arg)
382 "Send message like `mail-send', then, if no errors, exit from mail buffer.
383 Prefix arg means don't delete this window."
384 (interactive "P")
385 (mail-send)
386 (mail-bury arg))
388 (defun mail-dont-send (arg)
389 "Don't send the message you have been editing.
390 Prefix arg means don't delete this window."
391 (interactive "P")
392 (mail-bury arg))
394 (defun mail-bury (arg)
395 "Bury this mail buffer."
396 (let ((newbuf (other-buffer (current-buffer))))
397 (bury-buffer (current-buffer))
398 (if (and (fboundp 'frame-parameters)
399 (cdr (assq 'dedicated (frame-parameters)))
400 (not (null (delq (selected-frame) (visible-frame-list)))))
401 (delete-frame (selected-frame))
402 (let (rmail-flag summary-buffer)
403 (and (not arg)
404 (not (one-window-p))
405 (save-excursion
406 (set-buffer (window-buffer (next-window (selected-window) 'not)))
407 (setq rmail-flag (eq major-mode 'rmail-mode))
408 (setq summary-buffer
409 (and mail-bury-selects-summary
410 (boundp 'rmail-summary-buffer)
411 rmail-summary-buffer
412 (buffer-name rmail-summary-buffer)
413 (not (get-buffer-window rmail-summary-buffer))
414 rmail-summary-buffer))))
415 (if rmail-flag
416 ;; If the Rmail buffer has a summary, show that.
417 (if summary-buffer (switch-to-buffer summary-buffer)
418 (delete-window))
419 (switch-to-buffer newbuf))))))
421 (defun mail-send ()
422 "Send the message in the current buffer.
423 If `mail-interactive' is non-nil, wait for success indication
424 or error messages, and inform user.
425 Otherwise any failure is reported in a message back to
426 the user from the mailer."
427 (interactive)
428 (if (if buffer-file-name
429 (y-or-n-p "Send buffer contents as mail message? ")
430 (or (buffer-modified-p)
431 (y-or-n-p "Message already sent; resend? ")))
432 (progn
433 (run-hooks 'mail-send-hook)
434 (message "Sending...")
435 (funcall send-mail-function)
436 ;; Now perform actions on successful sending.
437 (while mail-send-actions
438 (condition-case nil
439 (apply (car (car mail-send-actions))
440 (cdr (car mail-send-actions)))
441 (error))
442 (setq mail-send-actions (cdr mail-send-actions)))
443 (message "Sending...done")
444 ;; If buffer has no file, mark it as unmodified and delete autosave.
445 (if (not buffer-file-name)
446 (progn
447 (set-buffer-modified-p nil)
448 (delete-auto-save-file-if-necessary t))))))
450 (defun sendmail-send-it ()
451 (let ((errbuf (if mail-interactive
452 (generate-new-buffer " sendmail errors")
454 (tembuf (generate-new-buffer " sendmail temp"))
455 (case-fold-search nil)
456 resend-to-addresses
457 delimline
458 (mailbuf (current-buffer)))
459 (unwind-protect
460 (save-excursion
461 (set-buffer tembuf)
462 (erase-buffer)
463 (insert-buffer-substring mailbuf)
464 (goto-char (point-max))
465 ;; require one newline at the end.
466 (or (= (preceding-char) ?\n)
467 (insert ?\n))
468 ;; Change header-delimiter to be what sendmail expects.
469 (goto-char (point-min))
470 (re-search-forward
471 (concat "^" (regexp-quote mail-header-separator) "\n"))
472 (replace-match "\n")
473 (backward-char 1)
474 (setq delimline (point-marker))
475 (sendmail-synch-aliases)
476 (if mail-aliases
477 (expand-mail-aliases (point-min) delimline))
478 (goto-char (point-min))
479 ;; ignore any blank lines in the header
480 (while (and (re-search-forward "\n\n\n*" delimline t)
481 (< (point) delimline))
482 (replace-match "\n"))
483 (let ((case-fold-search t))
484 (goto-char (point-min))
485 ;; Find and handle any FCC fields.
486 (goto-char (point-min))
487 (if (re-search-forward "^FCC:" delimline t)
488 (mail-do-fcc delimline))
489 (goto-char (point-min))
490 (require 'mail-utils)
491 (while (re-search-forward "^Resent-to:" delimline t)
492 (setq resend-to-addresses
493 (save-restriction
494 (narrow-to-region (point)
495 (save-excursion
496 (end-of-line)
497 (point)))
498 (append (mail-parse-comma-list)
499 resend-to-addresses))))
500 ;;; Apparently this causes a duplicate Sender.
501 ;;; ;; If the From is different than current user, insert Sender.
502 ;;; (goto-char (point-min))
503 ;;; (and (re-search-forward "^From:" delimline t)
504 ;;; (progn
505 ;;; (require 'mail-utils)
506 ;;; (not (string-equal
507 ;;; (mail-strip-quoted-names
508 ;;; (save-restriction
509 ;;; (narrow-to-region (point-min) delimline)
510 ;;; (mail-fetch-field "From")))
511 ;;; (user-login-name))))
512 ;;; (progn
513 ;;; (forward-line 1)
514 ;;; (insert "Sender: " (user-login-name) "\n")))
515 ;; Don't send out a blank subject line
516 (goto-char (point-min))
517 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
518 (replace-match ""))
519 ;; Put the "From:" field in unless for some odd reason
520 ;; they put one in themselves.
521 (goto-char (point-min))
522 (if (not (re-search-forward "^From:" delimline t))
523 (let* ((login (user-login-name))
524 (fullname (user-full-name)))
525 (cond ((eq mail-from-style 'angles)
526 (insert "From: " fullname)
527 (let ((fullname-start (+ (point-min) 6))
528 (fullname-end (point-marker)))
529 (goto-char fullname-start)
530 ;; Look for a character that cannot appear unquoted
531 ;; according to RFC 822.
532 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
533 fullname-end 1)
534 (progn
535 ;; Quote fullname, escaping specials.
536 (goto-char fullname-start)
537 (insert "\"")
538 (while (re-search-forward "[\"\\]"
539 fullname-end 1)
540 (replace-match "\\\\\\&" t))
541 (insert "\""))))
542 (insert " <" login ">\n"))
543 ((eq mail-from-style 'parens)
544 (insert "From: " login " (")
545 (let ((fullname-start (point)))
546 (insert fullname)
547 (let ((fullname-end (point-marker)))
548 (goto-char fullname-start)
549 ;; RFC 822 says \ and nonmatching parentheses
550 ;; must be escaped in comments.
551 ;; Escape every instance of ()\ ...
552 (while (re-search-forward "[()\\]" fullname-end 1)
553 (replace-match "\\\\\\&" t))
554 ;; ... then undo escaping of matching parentheses,
555 ;; including matching nested parentheses.
556 (goto-char fullname-start)
557 (while (re-search-forward
558 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
559 fullname-end 1)
560 (replace-match "\\1(\\3)" t)
561 (goto-char fullname-start))))
562 (insert ")\n"))
563 ((null mail-from-style)
564 (insert "From: " login "\n")))))
565 ;; Insert an extra newline if we need it to work around
566 ;; Sun's bug that swallows newlines.
567 (goto-char (1+ delimline))
568 (if (eval mail-mailer-swallows-blank-line)
569 (newline))
570 (if mail-interactive
571 (save-excursion
572 (set-buffer errbuf)
573 (erase-buffer))))
574 (apply 'call-process-region
575 (append (list (point-min) (point-max)
576 (if (boundp 'sendmail-program)
577 sendmail-program
578 "/usr/lib/sendmail")
579 nil errbuf nil "-oi")
580 ;; Always specify who from,
581 ;; since some systems have broken sendmails.
582 (list "-f" (user-login-name))
583 ;;; ;; Don't say "from root" if running under su.
584 ;;; (and (equal (user-real-login-name) "root")
585 ;;; (list "-f" (user-login-name)))
586 (and mail-alias-file
587 (list (concat "-oA" mail-alias-file)))
588 ;; These mean "report errors by mail"
589 ;; and "deliver in background".
590 (if (null mail-interactive) '("-oem" "-odb"))
591 ;; Get the addresses from the message
592 ;; unless this is a resend.
593 ;; We must not do that for a resend
594 ;; because we would find the original addresses.
595 ;; For a resend, include the specific addresses.
596 (or resend-to-addresses
597 '("-t"))))
598 (if mail-interactive
599 (save-excursion
600 (set-buffer errbuf)
601 (goto-char (point-min))
602 (while (re-search-forward "\n\n* *" nil t)
603 (replace-match "; "))
604 (if (not (zerop (buffer-size)))
605 (error "Sending...failed to %s"
606 (buffer-substring (point-min) (point-max)))))))
607 (kill-buffer tembuf)
608 (if (bufferp errbuf)
609 (kill-buffer errbuf)))))
611 ;; Return non-nil if file FILE is an Rmail file.
612 (defun mail-file-babyl-p (file)
613 (unwind-protect
614 (save-excursion
615 (set-buffer (get-buffer-create " mail-temp"))
616 (erase-buffer)
617 (insert-file-contents file nil 0 20)
618 (looking-at "BABYL OPTIONS:"))
619 (kill-buffer " mail-temp")))
621 (defun mail-do-fcc (header-end)
622 (let (fcc-list
623 (rmailbuf (current-buffer))
624 (time (current-time))
625 (tembuf (generate-new-buffer " rmail output"))
626 (case-fold-search t))
627 (save-excursion
628 (goto-char (point-min))
629 (while (re-search-forward "^FCC:[ \t]*" header-end t)
630 (setq fcc-list (cons (buffer-substring (point)
631 (progn
632 (end-of-line)
633 (skip-chars-backward " \t")
634 (point)))
635 fcc-list))
636 (delete-region (match-beginning 0)
637 (progn (forward-line 1) (point))))
638 (set-buffer tembuf)
639 (erase-buffer)
640 ;; This initial newline is written out if the fcc file already exists.
641 (insert "\nFrom " (user-login-name) " "
642 (current-time-string time) "\n")
643 ;; Insert the time zone before the year.
644 (forward-char -1)
645 (forward-word -1)
646 (require 'mail-utils)
647 (insert (mail-rfc822-time-zone time) " ")
648 (goto-char (point-max))
649 (insert-buffer-substring rmailbuf)
650 ;; Make sure messages are separated.
651 (goto-char (point-max))
652 (insert ?\n)
653 (goto-char 2)
654 ;; ``Quote'' "^From " as ">From "
655 ;; (note that this isn't really quoting, as there is no requirement
656 ;; that "^[>]+From " be quoted in the same transparent way.)
657 (let ((case-fold-search nil))
658 (while (search-forward "\nFrom " nil t)
659 (forward-char -5)
660 (insert ?>)))
661 (while fcc-list
662 (let* ((buffer (find-buffer-visiting (car fcc-list)))
663 (curbuf (current-buffer))
664 (beg (point-min)) (end (point-max))
665 (beg2 (save-excursion (goto-char (point-min))
666 (forward-line 2) (point))))
667 (if buffer
668 ;; File is present in a buffer => append to that buffer.
669 (save-excursion
670 (set-buffer buffer)
671 ;; Keep the end of the accessible portion at the same place
672 ;; unless it is the end of the buffer.
673 (let ((max (if (/= (1+ (buffer-size)) (point-max))
674 (point-max))))
675 (unwind-protect
676 ;; Code below lifted from rmailout.el
677 ;; function rmail-output-to-rmail-file:
678 (let ((buffer-read-only nil)
679 (msg (and (boundp 'rmail-current-message)
680 rmail-current-message)))
681 ;; If MSG is non-nil, buffer is in RMAIL mode.
682 (if msg
683 (progn
684 (rmail-maybe-set-message-counters)
685 (widen)
686 (narrow-to-region (point-max) (point-max))
687 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
688 "From: " (user-login-name) "\n"
689 "Date: " (mail-rfc822-date) "\n")
690 (insert-buffer-substring curbuf beg2 end)
691 (insert "\n\C-_")
692 (goto-char (point-min))
693 (widen)
694 (search-backward "\n\^_")
695 (narrow-to-region (point) (point-max))
696 (rmail-count-new-messages t)
697 (rmail-show-message msg)
698 (setq max nil))
699 ;; Output file not in rmail mode
700 ;; => just insert at the end.
701 (narrow-to-region (point-min) (1+ (buffer-size)))
702 (goto-char (point-max))
703 (insert-buffer-substring curbuf beg end)))
704 (if max (narrow-to-region (point-min) max)))))
705 ;; Else append to the file directly.
706 (if (and (file-exists-p (car fcc-list))
707 (mail-file-babyl-p (car fcc-list)))
708 ;; If the file is a Babyl file,
709 ;; convert the message to Babyl format.
710 (save-excursion
711 (set-buffer (get-buffer-create " mail-temp"))
712 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
713 "From: " (user-login-name) "\n"
714 "Date: " (mail-rfc822-date) "\n")
715 (insert-buffer-substring curbuf beg2 end)
716 (insert "\n\C-_")
717 (write-region (point-min) (point-max) (car fcc-list) t))
718 (write-region
719 (1+ (point-min)) (point-max) (car fcc-list) t))))
720 (setq fcc-list (cdr fcc-list))))
721 (kill-buffer tembuf)))
723 (defun mail-sent-via ()
724 "Make a Sent-via header line from each To or CC header line."
725 (interactive)
726 (save-excursion
727 (goto-char (point-min))
728 ;; find the header-separator
729 (search-forward (concat "\n" mail-header-separator "\n"))
730 (forward-line -1)
731 ;; put a marker at the end of the header
732 (let ((end (point-marker))
733 (case-fold-search t)
734 to-line)
735 (goto-char (point-min))
736 ;; search for the To: lines and make Sent-via: lines from them
737 ;; search for the next To: line
738 (while (re-search-forward "^\\(to\\|cc\\):" end t)
739 ;; Grab this line plus all its continuations, sans the `to:'.
740 (let ((to-line
741 (buffer-substring (point)
742 (progn
743 (if (re-search-forward "^[^ \t\n]" end t)
744 (backward-char 1)
745 (goto-char end))
746 (point)))))
747 ;; Insert a copy, with altered header field name.
748 (insert-before-markers "Sent-via:" to-line))))))
750 (defun mail-to ()
751 "Move point to end of To-field."
752 (interactive)
753 (expand-abbrev)
754 (mail-position-on-field "To"))
756 (defun mail-subject ()
757 "Move point to end of Subject-field."
758 (interactive)
759 (expand-abbrev)
760 (mail-position-on-field "Subject"))
762 (defun mail-cc ()
763 "Move point to end of CC-field. Create a CC field if none."
764 (interactive)
765 (expand-abbrev)
766 (or (mail-position-on-field "cc" t)
767 (progn (mail-position-on-field "to")
768 (insert "\nCC: "))))
770 (defun mail-bcc ()
771 "Move point to end of BCC-field. Create a BCC field if none."
772 (interactive)
773 (expand-abbrev)
774 (or (mail-position-on-field "bcc" t)
775 (progn (mail-position-on-field "to")
776 (insert "\nBCC: "))))
778 (defun mail-fcc (folder)
779 "Add a new FCC field, with file name completion."
780 (interactive "FFolder carbon copy: ")
781 (expand-abbrev)
782 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
783 (mail-position-on-field "to"))
784 (insert "\nFCC: " folder))
786 (defun mail-reply-to ()
787 "Move point to end of Reply-To-field."
788 (interactive)
789 (expand-abbrev)
790 (mail-position-on-field "Reply-To"))
792 (defun mail-position-on-field (field &optional soft)
793 (let (end
794 (case-fold-search t))
795 (goto-char (point-min))
796 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
797 (setq end (match-beginning 0))
798 (goto-char (point-min))
799 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
800 (progn
801 (re-search-forward "^[^ \t]" nil 'move)
802 (beginning-of-line)
803 (skip-chars-backward "\n")
805 (or soft
806 (progn (goto-char end)
807 (insert field ": \n")
808 (skip-chars-backward "\n")))
809 nil)))
811 (defun mail-text ()
812 "Move point to beginning of message text."
813 (interactive)
814 (expand-abbrev)
815 (goto-char (point-min))
816 (search-forward (concat "\n" mail-header-separator "\n")))
818 (defun mail-signature (atpoint)
819 "Sign letter with contents of the file `~/.signature'.
820 Prefix arg means put contents at point."
821 (interactive "P")
822 (save-excursion
823 (or atpoint
824 (goto-char (point-max)))
825 (skip-chars-backward " \t\n")
826 (end-of-line)
827 (or atpoint
828 (delete-region (point) (point-max)))
829 (insert "\n\n-- \n")
830 (insert-file-contents (expand-file-name "~/.signature"))))
832 (defun mail-fill-yanked-message (&optional justifyp)
833 "Fill the paragraphs of a message yanked into this one.
834 Numeric argument means justify as well."
835 (interactive "P")
836 (save-excursion
837 (goto-char (point-min))
838 (search-forward (concat "\n" mail-header-separator "\n") nil t)
839 (fill-individual-paragraphs (point)
840 (point-max)
841 justifyp
842 t)))
844 (defun mail-indent-citation ()
845 "Modify text just inserted from a message to be cited.
846 The inserted text should be the region.
847 When this function returns, the region is again around the modified text.
849 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
850 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
851 (let ((start (point)))
852 (mail-yank-clear-headers start (mark t))
853 (if (null mail-yank-prefix)
854 (indent-rigidly start (mark t) mail-indentation-spaces)
855 (save-excursion
856 (goto-char start)
857 (while (< (point) (mark t))
858 (insert mail-yank-prefix)
859 (forward-line 1))))))
861 (defun mail-yank-original (arg)
862 "Insert the message being replied to, if any (in rmail).
863 Puts point before the text and mark after.
864 Normally, indents each nonblank line ARG spaces (default 3).
865 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
867 Just \\[universal-argument] as argument means don't indent, insert no prefix,
868 and don't delete any header fields."
869 (interactive "P")
870 (if mail-reply-buffer
871 (let ((start (point)))
872 ;; If the original message is in another window in the same frame,
873 ;; delete that window to save screen space.
874 ;; t means don't alter other frames.
875 (delete-windows-on mail-reply-buffer t)
876 (insert-buffer mail-reply-buffer)
877 (if (consp arg)
879 (goto-char start)
880 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
881 mail-indentation-spaces)))
882 (if mail-citation-hook
883 (run-hooks 'mail-citation-hook)
884 (if mail-yank-hooks
885 (run-hooks 'mail-yank-hooks)
886 (mail-indent-citation)))))
887 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
888 ;; It is cleaner to avoid activation, even though the command
889 ;; loop would deactivate the mark because we inserted text.
890 (goto-char (prog1 (mark t)
891 (set-marker (mark-marker) (point) (current-buffer))))
892 (if (not (eolp)) (insert ?\n)))))
894 (defun mail-yank-clear-headers (start end)
895 (save-excursion
896 (goto-char start)
897 (if (search-forward "\n\n" end t)
898 (save-restriction
899 (narrow-to-region start (point))
900 (goto-char start)
901 (while (let ((case-fold-search t))
902 (re-search-forward mail-yank-ignored-headers nil t))
903 (beginning-of-line)
904 (delete-region (point)
905 (progn (re-search-forward "\n[^ \t]")
906 (forward-char -1)
907 (point))))))))
909 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
911 ;;;###autoload
912 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
913 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
914 When this function returns, the buffer `*mail*' is selected.
915 The value is t if the message was newly initialized; otherwise, nil.
917 Optionally, the signature file `~/.signature' can be inserted at the end;
918 see the variable `mail-signature'.
920 \\<mail-mode-map>
921 While editing message, type \\[mail-send-and-exit] to send the message and exit.
923 Various special commands starting with C-c are available in sendmail mode
924 to move to message header fields:
925 \\{mail-mode-map}
927 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
928 when the message is initialized.
930 If `mail-default-reply-to' is non-nil, it should be an address (a string);
931 a Reply-to: field with that address is inserted.
933 If `mail-archive-file-name' is non-nil, an FCC field with that file name
934 is inserted.
936 The normal hook `mail-setup-hook' is run after the message is
937 initialized. It can add more default fields to the message.
939 When calling from a program, the first argument if non-nil says
940 not to erase the existing contents of the `*mail*' buffer.
942 The second through fifth arguments,
943 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
944 the initial contents of those header fields.
945 These arguments should not have final newlines.
946 The sixth argument REPLYBUFFER is a buffer whose contents
947 should be yanked if the user types C-c C-y.
948 The seventh argument ACTIONS is a list of actions to take
949 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
950 when the message is sent, we apply FUNCTION to ARGS.
951 This is how Rmail arranges to mark messages `answered'."
952 (interactive "P")
953 ;;; This is commented out because I found it was confusing in practice.
954 ;;; It is easy enough to rename *mail* by hand with rename-buffer
955 ;;; if you want to have multiple mail buffers.
956 ;;; And then you can control which messages to save. --rms.
957 ;;; (let ((index 1)
958 ;;; buffer)
959 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
960 ;;; (if noerase
961 ;;; (progn
962 ;;; (while (and (setq buffer
963 ;;; (get-buffer (if (= 1 index) "*mail*"
964 ;;; (format "*mail*<%d>" index))))
965 ;;; (not (buffer-modified-p buffer)))
966 ;;; (setq index (1+ index)))
967 ;;; (if buffer (switch-to-buffer buffer)
968 ;;; ;; If none exists, start a new message.
969 ;;; ;; This will never re-use an existing unmodified mail buffer
970 ;;; ;; (since index is not 1 anymore). Perhaps it should.
971 ;;; (setq noerase nil))))
972 ;;; ;; Unless we found a modified message and are happy, start a new message.
973 ;;; (if (not noerase)
974 ;;; (progn
975 ;;; ;; Look for existing unmodified mail buffer.
976 ;;; (while (and (setq buffer
977 ;;; (get-buffer (if (= 1 index) "*mail*"
978 ;;; (format "*mail*<%d>" index))))
979 ;;; (buffer-modified-p buffer))
980 ;;; (setq index (1+ index)))
981 ;;; ;; If none, make a new one.
982 ;;; (or buffer
983 ;;; (setq buffer (generate-new-buffer "*mail*")))
984 ;;; ;; Go there and initialize it.
985 ;;; (switch-to-buffer buffer)
986 ;;; (erase-buffer)
987 ;;; (setq default-directory (expand-file-name "~/"))
988 ;;; (auto-save-mode auto-save-default)
989 ;;; (mail-mode)
990 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
991 ;;; (if (and buffer-auto-save-file-name
992 ;;; (file-exists-p buffer-auto-save-file-name))
993 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
994 ;;; t))
995 (pop-to-buffer "*mail*")
996 (if (file-exists-p (expand-file-name "~/"))
997 (setq default-directory (expand-file-name "~/")))
998 (auto-save-mode auto-save-default)
999 (mail-mode)
1000 ;; Disconnect the buffer from its visited file
1001 ;; (in case the user has actually visited a file *mail*).
1002 ; (set-visited-file-name nil)
1003 (let (initialized)
1004 (and (not noerase)
1005 (or (not (buffer-modified-p))
1006 (y-or-n-p "Unsent message being composed; erase it? "))
1007 (progn (erase-buffer)
1008 (mail-setup to subject in-reply-to cc replybuffer actions)
1009 (setq initialized t)))
1010 (if (and buffer-auto-save-file-name
1011 (file-exists-p buffer-auto-save-file-name))
1012 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1013 initialized))
1015 (defun mail-recover ()
1016 "Reread contents of current buffer from its last auto-save file."
1017 (interactive)
1018 (let ((file-name (make-auto-save-file-name)))
1019 (cond ((save-window-excursion
1020 (if (not (eq system-type 'vax-vms))
1021 (with-output-to-temp-buffer "*Directory*"
1022 (buffer-disable-undo standard-output)
1023 (call-process "ls" nil standard-output nil "-l" file-name)))
1024 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1025 (let ((buffer-read-only nil))
1026 (erase-buffer)
1027 (insert-file-contents file-name nil)))
1028 (t (error "mail-recover cancelled")))))
1030 ;;;###autoload
1031 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1032 "Like `mail' command, but display mail buffer in another window."
1033 (interactive "P")
1034 (let ((pop-up-windows t)
1035 (special-display-buffer-names nil)
1036 (special-display-regexps nil)
1037 (same-window-buffer-names nil)
1038 (same-window-regexps nil))
1039 (pop-to-buffer "*mail*"))
1040 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1042 ;;;###autoload
1043 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1044 "Like `mail' command, but display mail buffer in another frame."
1045 (interactive "P")
1046 (let ((pop-up-frames t)
1047 (special-display-buffer-names nil)
1048 (special-display-regexps nil)
1049 (same-window-buffer-names nil)
1050 (same-window-regexps nil))
1051 (pop-to-buffer "*mail*"))
1052 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1054 ;;; Do not execute these when sendmail.el is loaded,
1055 ;;; only in loaddefs.el.
1056 ;;;###autoload (define-key ctl-x-map "m" 'mail)
1057 ;;;###autoload (define-key ctl-x-4-map "m" 'mail-other-window)
1058 ;;;###autoload (define-key ctl-x-5-map "m" 'mail-other-frame)
1060 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1062 ;;; Do not add anything but external entries on this page.
1064 (provide 'sendmail)
1066 ;;; sendmail.el ends here