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