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