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