]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
(news-reply-mode-map): Delete news-reply-signature.
[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 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 (setq paragraph-start (concat (regexp-quote mail-header-separator)
331 "$\\|[ \t]*[-_][-_][-_]+$\\|"
332 paragraph-start))
333 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
334 "$\\|[ \t]*[-_][-_][-_]+$\\|"
335 paragraph-separate))
336 (run-hooks 'text-mode-hook 'mail-mode-hook))
337 \f
338 ;;; Set up keymap.
339
340 (if mail-mode-map
341 nil
342 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
343 (define-key mail-mode-map "\C-c?" 'describe-mode)
344 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
345 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
346 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
347 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
348 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
349 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
350 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
351 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
352 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
353 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
354 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
355 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
356 (define-key mail-mode-map "\C-c\C-s" 'mail-send))
357
358 (define-key mail-mode-map [menu-bar mail]
359 (cons "Mail" (make-sparse-keymap "Mail")))
360
361 (define-key mail-mode-map [menu-bar mail fill]
362 '("Fill Citation" . mail-fill-yanked-message))
363
364 (define-key mail-mode-map [menu-bar mail yank]
365 '("Cite Original" . mail-yank-original))
366
367 (define-key mail-mode-map [menu-bar mail signature]
368 '("Insert Signature" . mail-signature))
369
370 (define-key mail-mode-map [menu-bar mail cancel]
371 '("Cancel" . mail-dont-send))
372
373 (define-key mail-mode-map [menu-bar mail send-stay]
374 '("Send, Keep Editing" . mail-send))
375
376 (define-key mail-mode-map [menu-bar mail send]
377 '("Send Message" . mail-send-and-exit))
378
379 (define-key mail-mode-map [menu-bar headers]
380 (cons "Headers" (make-sparse-keymap "Move to Header")))
381
382 (define-key mail-mode-map [menu-bar headers reply-to]
383 '("Reply-To" . mail-reply-to))
384
385 (define-key mail-mode-map [menu-bar headers sent-via]
386 '("Sent Via" . mail-sent-via))
387
388 (define-key mail-mode-map [menu-bar headers text]
389 '("Text" . mail-text))
390
391 (define-key mail-mode-map [menu-bar headers bcc]
392 '("Bcc" . mail-bcc))
393
394 (define-key mail-mode-map [menu-bar headers fcc]
395 '("Fcc" . mail-fcc))
396
397 (define-key mail-mode-map [menu-bar headers cc]
398 '("Cc" . mail-cc))
399
400 (define-key mail-mode-map [menu-bar headers subject]
401 '("Subject" . mail-subject))
402
403 (define-key mail-mode-map [menu-bar headers to]
404 '("To" . mail-to))
405 \f
406 (defun mail-send-and-exit (arg)
407 "Send message like `mail-send', then, if no errors, exit from mail buffer.
408 Prefix arg means don't delete this window."
409 (interactive "P")
410 (mail-send)
411 (mail-bury arg))
412
413 (defun mail-dont-send (arg)
414 "Don't send the message you have been editing.
415 Prefix arg means don't delete this window."
416 (interactive "P")
417 (mail-bury arg))
418
419 (defun mail-bury (arg)
420 "Bury this mail buffer."
421 (let ((newbuf (other-buffer (current-buffer))))
422 (bury-buffer (current-buffer))
423 (if (and (or (window-dedicated-p (frame-selected-window))
424 (assq 'mail-dedicated-frame (frame-parameters)))
425 (not (null (delq (selected-frame) (visible-frame-list)))))
426 (delete-frame (selected-frame))
427 (let (rmail-flag summary-buffer)
428 (and (not arg)
429 (not (one-window-p))
430 (save-excursion
431 (set-buffer (window-buffer (next-window (selected-window) 'not)))
432 (setq rmail-flag (eq major-mode 'rmail-mode))
433 (setq summary-buffer
434 (and mail-bury-selects-summary
435 (boundp 'rmail-summary-buffer)
436 rmail-summary-buffer
437 (buffer-name rmail-summary-buffer)
438 (not (get-buffer-window rmail-summary-buffer))
439 rmail-summary-buffer))))
440 (if rmail-flag
441 ;; If the Rmail buffer has a summary, show that.
442 (if summary-buffer (switch-to-buffer summary-buffer)
443 (delete-window))
444 (switch-to-buffer newbuf))))))
445
446 (defun mail-send ()
447 "Send the message in the current buffer.
448 If `mail-interactive' is non-nil, wait for success indication
449 or error messages, and inform user.
450 Otherwise any failure is reported in a message back to
451 the user from the mailer."
452 (interactive)
453 (if (if buffer-file-name
454 (y-or-n-p "Send buffer contents as mail message? ")
455 (or (buffer-modified-p)
456 (y-or-n-p "Message already sent; resend? ")))
457 (let ((inhibit-read-only t))
458 (run-hooks 'mail-send-hook)
459 (message "Sending...")
460 (funcall send-mail-function)
461 ;; Now perform actions on successful sending.
462 (while mail-send-actions
463 (condition-case nil
464 (apply (car (car mail-send-actions))
465 (cdr (car mail-send-actions)))
466 (error))
467 (setq mail-send-actions (cdr mail-send-actions)))
468 (message "Sending...done")
469 ;; If buffer has no file, mark it as unmodified and delete autosave.
470 (if (not buffer-file-name)
471 (progn
472 (set-buffer-modified-p nil)
473 (delete-auto-save-file-if-necessary t))))))
474
475 (defun sendmail-send-it ()
476 (require 'mail-utils)
477 (let ((errbuf (if mail-interactive
478 (generate-new-buffer " sendmail errors")
479 0))
480 (tembuf (generate-new-buffer " sendmail temp"))
481 (case-fold-search nil)
482 resend-to-addresses
483 delimline
484 (mailbuf (current-buffer)))
485 (unwind-protect
486 (save-excursion
487 (set-buffer tembuf)
488 (erase-buffer)
489 (insert-buffer-substring mailbuf)
490 (goto-char (point-max))
491 ;; require one newline at the end.
492 (or (= (preceding-char) ?\n)
493 (insert ?\n))
494 ;; Change header-delimiter to be what sendmail expects.
495 (goto-char (point-min))
496 (re-search-forward
497 (concat "^" (regexp-quote mail-header-separator) "\n"))
498 (replace-match "\n")
499 (backward-char 1)
500 (setq delimline (point-marker))
501 (sendmail-sync-aliases)
502 (if mail-aliases
503 (expand-mail-aliases (point-min) delimline))
504 (goto-char (point-min))
505 ;; ignore any blank lines in the header
506 (while (and (re-search-forward "\n\n\n*" delimline t)
507 (< (point) delimline))
508 (replace-match "\n"))
509 (let ((case-fold-search t))
510 (goto-char (point-min))
511 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
512 (setq resend-to-addresses
513 (save-restriction
514 (narrow-to-region (point)
515 (save-excursion
516 (end-of-line)
517 (point)))
518 (append (mail-parse-comma-list)
519 resend-to-addresses)))
520 ;; Delete Resent-BCC ourselves
521 (if (save-excursion (beginning-of-line)
522 (looking-at "resent-bcc"))
523 (delete-region (save-excursion (beginning-of-line) (point))
524 (save-excursion (end-of-line) (1+ (point))))))
525 ;;; Apparently this causes a duplicate Sender.
526 ;;; ;; If the From is different than current user, insert Sender.
527 ;;; (goto-char (point-min))
528 ;;; (and (re-search-forward "^From:" delimline t)
529 ;;; (progn
530 ;;; (require 'mail-utils)
531 ;;; (not (string-equal
532 ;;; (mail-strip-quoted-names
533 ;;; (save-restriction
534 ;;; (narrow-to-region (point-min) delimline)
535 ;;; (mail-fetch-field "From")))
536 ;;; (user-login-name))))
537 ;;; (progn
538 ;;; (forward-line 1)
539 ;;; (insert "Sender: " (user-login-name) "\n")))
540 ;; Don't send out a blank subject line
541 (goto-char (point-min))
542 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
543 (replace-match "")
544 ;; This one matches a Subject just before the header delimiter.
545 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
546 (= (match-end 0) delimline))
547 (replace-match "")))
548 ;; Put the "From:" field in unless for some odd reason
549 ;; they put one in themselves.
550 (goto-char (point-min))
551 (if (not (re-search-forward "^From:" delimline t))
552 (let* ((login user-mail-address)
553 (fullname (user-full-name)))
554 (cond ((eq mail-from-style 'angles)
555 (insert "From: " fullname)
556 (let ((fullname-start (+ (point-min) 6))
557 (fullname-end (point-marker)))
558 (goto-char fullname-start)
559 ;; Look for a character that cannot appear unquoted
560 ;; according to RFC 822.
561 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
562 fullname-end 1)
563 (progn
564 ;; Quote fullname, escaping specials.
565 (goto-char fullname-start)
566 (insert "\"")
567 (while (re-search-forward "[\"\\]"
568 fullname-end 1)
569 (replace-match "\\\\\\&" t))
570 (insert "\""))))
571 (insert " <" login ">\n"))
572 ((eq mail-from-style 'parens)
573 (insert "From: " login " (")
574 (let ((fullname-start (point)))
575 (insert fullname)
576 (let ((fullname-end (point-marker)))
577 (goto-char fullname-start)
578 ;; RFC 822 says \ and nonmatching parentheses
579 ;; must be escaped in comments.
580 ;; Escape every instance of ()\ ...
581 (while (re-search-forward "[()\\]" fullname-end 1)
582 (replace-match "\\\\\\&" t))
583 ;; ... then undo escaping of matching parentheses,
584 ;; including matching nested parentheses.
585 (goto-char fullname-start)
586 (while (re-search-forward
587 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
588 fullname-end 1)
589 (replace-match "\\1(\\3)" t)
590 (goto-char fullname-start))))
591 (insert ")\n"))
592 ((null mail-from-style)
593 (insert "From: " login "\n")))))
594 ;; Insert an extra newline if we need it to work around
595 ;; Sun's bug that swallows newlines.
596 (goto-char (1+ delimline))
597 (if (eval mail-mailer-swallows-blank-line)
598 (newline))
599 ;; Find and handle any FCC fields.
600 (goto-char (point-min))
601 (if (re-search-forward "^FCC:" delimline t)
602 (mail-do-fcc delimline))
603 (if mail-interactive
604 (save-excursion
605 (set-buffer errbuf)
606 (erase-buffer))))
607 (let ((default-directory "/"))
608 (apply 'call-process-region
609 (append (list (point-min) (point-max)
610 (if (boundp 'sendmail-program)
611 sendmail-program
612 "/usr/lib/sendmail")
613 nil errbuf nil "-oi")
614 ;; Always specify who from,
615 ;; since some systems have broken sendmails.
616 (list "-f" (user-login-name))
617 ;;; ;; Don't say "from root" if running under su.
618 ;;; (and (equal (user-real-login-name) "root")
619 ;;; (list "-f" (user-login-name)))
620 (and mail-alias-file
621 (list (concat "-oA" mail-alias-file)))
622 ;; These mean "report errors by mail"
623 ;; and "deliver in background".
624 (if (null mail-interactive) '("-oem" "-odb"))
625 ;; Get the addresses from the message
626 ;; unless this is a resend.
627 ;; We must not do that for a resend
628 ;; because we would find the original addresses.
629 ;; For a resend, include the specific addresses.
630 (or resend-to-addresses
631 '("-t")))))
632 (if mail-interactive
633 (save-excursion
634 (set-buffer errbuf)
635 (goto-char (point-min))
636 (while (re-search-forward "\n\n* *" nil t)
637 (replace-match "; "))
638 (if (not (zerop (buffer-size)))
639 (error "Sending...failed to %s"
640 (buffer-substring (point-min) (point-max)))))))
641 (kill-buffer tembuf)
642 (if (bufferp errbuf)
643 (kill-buffer errbuf)))))
644
645 (defun mail-do-fcc (header-end)
646 (let (fcc-list
647 (rmailbuf (current-buffer))
648 (time (current-time))
649 (tembuf (generate-new-buffer " rmail output"))
650 (case-fold-search t))
651 (save-excursion
652 (goto-char (point-min))
653 (while (re-search-forward "^FCC:[ \t]*" header-end t)
654 (setq fcc-list (cons (buffer-substring (point)
655 (progn
656 (end-of-line)
657 (skip-chars-backward " \t")
658 (point)))
659 fcc-list))
660 (delete-region (match-beginning 0)
661 (progn (forward-line 1) (point))))
662 (set-buffer tembuf)
663 (erase-buffer)
664 ;; This initial newline is written out if the fcc file already exists.
665 (insert "\nFrom " (user-login-name) " "
666 (current-time-string time) "\n")
667 ;; Insert the time zone before the year.
668 (forward-char -1)
669 (forward-word -1)
670 (require 'mail-utils)
671 (insert (mail-rfc822-time-zone time) " ")
672 (goto-char (point-max))
673 (insert-buffer-substring rmailbuf)
674 ;; Make sure messages are separated.
675 (goto-char (point-max))
676 (insert ?\n)
677 (goto-char 2)
678 ;; ``Quote'' "^From " as ">From "
679 ;; (note that this isn't really quoting, as there is no requirement
680 ;; that "^[>]+From " be quoted in the same transparent way.)
681 (let ((case-fold-search nil))
682 (while (search-forward "\nFrom " nil t)
683 (forward-char -5)
684 (insert ?>)))
685 (while fcc-list
686 (let* ((buffer (find-buffer-visiting (car fcc-list)))
687 (curbuf (current-buffer))
688 (beg (point-min)) (end (point-max))
689 (beg2 (save-excursion (goto-char (point-min))
690 (forward-line 2) (point))))
691 (if buffer
692 ;; File is present in a buffer => append to that buffer.
693 (save-excursion
694 (set-buffer buffer)
695 ;; Keep the end of the accessible portion at the same place
696 ;; unless it is the end of the buffer.
697 (let ((max (if (/= (1+ (buffer-size)) (point-max))
698 (point-max))))
699 (unwind-protect
700 ;; Code below lifted from rmailout.el
701 ;; function rmail-output-to-rmail-file:
702 (let ((buffer-read-only nil)
703 (msg (and (boundp 'rmail-current-message)
704 rmail-current-message)))
705 ;; If MSG is non-nil, buffer is in RMAIL mode.
706 (if msg
707 (progn
708 (rmail-maybe-set-message-counters)
709 (widen)
710 (narrow-to-region (point-max) (point-max))
711 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
712 "Date: " (mail-rfc822-date) "\n")
713 (insert-buffer-substring curbuf beg2 end)
714 (insert "\n\C-_")
715 (goto-char (point-min))
716 (widen)
717 (search-backward "\n\^_")
718 (narrow-to-region (point) (point-max))
719 (rmail-count-new-messages t)
720 (rmail-show-message msg)
721 (setq max nil))
722 ;; Output file not in rmail mode
723 ;; => just insert at the end.
724 (narrow-to-region (point-min) (1+ (buffer-size)))
725 (goto-char (point-max))
726 (insert-buffer-substring curbuf beg end)))
727 (if max (narrow-to-region (point-min) max)))))
728 ;; Else append to the file directly.
729 (if (and (file-exists-p (car fcc-list))
730 (mail-file-babyl-p (car fcc-list)))
731 ;; If the file is a Babyl file,
732 ;; convert the message to Babyl format.
733 (save-excursion
734 (set-buffer (get-buffer-create " mail-temp"))
735 (setq buffer-read-only nil)
736 (erase-buffer)
737 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
738 "Date: " (mail-rfc822-date) "\n")
739 (insert-buffer-substring curbuf beg2 end)
740 (insert "\n\C-_")
741 (write-region (point-min) (point-max) (car fcc-list) t)
742 (erase-buffer))
743 (write-region
744 (1+ (point-min)) (point-max) (car fcc-list) t))))
745 (setq fcc-list (cdr fcc-list))))
746 (kill-buffer tembuf)))
747
748 (defun mail-sent-via ()
749 "Make a Sent-via header line from each To or CC header line."
750 (interactive)
751 (save-excursion
752 (goto-char (point-min))
753 ;; find the header-separator
754 (search-forward (concat "\n" mail-header-separator "\n"))
755 (forward-line -1)
756 ;; put a marker at the end of the header
757 (let ((end (point-marker))
758 (case-fold-search t)
759 to-line)
760 (goto-char (point-min))
761 ;; search for the To: lines and make Sent-via: lines from them
762 ;; search for the next To: line
763 (while (re-search-forward "^\\(to\\|cc\\):" end t)
764 ;; Grab this line plus all its continuations, sans the `to:'.
765 (let ((to-line
766 (buffer-substring (point)
767 (progn
768 (if (re-search-forward "^[^ \t\n]" end t)
769 (backward-char 1)
770 (goto-char end))
771 (point)))))
772 ;; Insert a copy, with altered header field name.
773 (insert-before-markers "Sent-via:" to-line))))))
774 \f
775 (defun mail-to ()
776 "Move point to end of To-field."
777 (interactive)
778 (expand-abbrev)
779 (mail-position-on-field "To"))
780
781 (defun mail-subject ()
782 "Move point to end of Subject-field."
783 (interactive)
784 (expand-abbrev)
785 (mail-position-on-field "Subject"))
786
787 (defun mail-cc ()
788 "Move point to end of CC-field. Create a CC field if none."
789 (interactive)
790 (expand-abbrev)
791 (or (mail-position-on-field "cc" t)
792 (progn (mail-position-on-field "to")
793 (insert "\nCC: "))))
794
795 (defun mail-bcc ()
796 "Move point to end of BCC-field. Create a BCC field if none."
797 (interactive)
798 (expand-abbrev)
799 (or (mail-position-on-field "bcc" t)
800 (progn (mail-position-on-field "to")
801 (insert "\nBCC: "))))
802
803 (defun mail-fcc (folder)
804 "Add a new FCC field, with file name completion."
805 (interactive "FFolder carbon copy: ")
806 (expand-abbrev)
807 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
808 (mail-position-on-field "to"))
809 (insert "\nFCC: " folder))
810
811 (defun mail-reply-to ()
812 "Move point to end of Reply-To-field."
813 (interactive)
814 (expand-abbrev)
815 (mail-position-on-field "Reply-To"))
816
817 (defun mail-position-on-field (field &optional soft)
818 (let (end
819 (case-fold-search t))
820 (goto-char (point-min))
821 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
822 (setq end (match-beginning 0))
823 (goto-char (point-min))
824 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
825 (progn
826 (re-search-forward "^[^ \t]" nil 'move)
827 (beginning-of-line)
828 (skip-chars-backward "\n")
829 t)
830 (or soft
831 (progn (goto-char end)
832 (insert field ": \n")
833 (skip-chars-backward "\n")))
834 nil)))
835
836 (defun mail-text ()
837 "Move point to beginning of message text."
838 (interactive)
839 (expand-abbrev)
840 (goto-char (point-min))
841 (search-forward (concat "\n" mail-header-separator "\n")))
842 \f
843 (defun mail-signature (atpoint)
844 "Sign letter with contents of the file `mail-signature-file'.
845 Prefix arg means put contents at point."
846 (interactive "P")
847 (save-excursion
848 (or atpoint
849 (goto-char (point-max)))
850 (skip-chars-backward " \t\n")
851 (end-of-line)
852 (or atpoint
853 (delete-region (point) (point-max)))
854 (insert "\n\n-- \n")
855 (insert-file-contents (expand-file-name mail-signature-file))))
856
857 (defun mail-fill-yanked-message (&optional justifyp)
858 "Fill the paragraphs of a message yanked into this one.
859 Numeric argument means justify as well."
860 (interactive "P")
861 (save-excursion
862 (goto-char (point-min))
863 (search-forward (concat "\n" mail-header-separator "\n") nil t)
864 (fill-individual-paragraphs (point)
865 (point-max)
866 justifyp
867 t)))
868
869 (defun mail-indent-citation ()
870 "Modify text just inserted from a message to be cited.
871 The inserted text should be the region.
872 When this function returns, the region is again around the modified text.
873
874 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
875 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
876 (let ((start (point)))
877 (mail-yank-clear-headers start (mark t))
878 (if (null mail-yank-prefix)
879 (indent-rigidly start (mark t) mail-indentation-spaces)
880 (save-excursion
881 (goto-char start)
882 (while (< (point) (mark t))
883 (insert mail-yank-prefix)
884 (forward-line 1))))))
885
886 (defun mail-yank-original (arg)
887 "Insert the message being replied to, if any (in rmail).
888 Puts point after the text and mark before.
889 Normally, indents each nonblank line ARG spaces (default 3).
890 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
891
892 Just \\[universal-argument] as argument means don't indent, insert no prefix,
893 and don't delete any header fields."
894 (interactive "P")
895 (if mail-reply-buffer
896 (let ((start (point)))
897 ;; If the original message is in another window in the same frame,
898 ;; delete that window to save screen space.
899 ;; t means don't alter other frames.
900 (delete-windows-on mail-reply-buffer t)
901 (insert-buffer mail-reply-buffer)
902 (if (consp arg)
903 nil
904 (goto-char start)
905 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
906 mail-indentation-spaces)))
907 (if mail-citation-hook
908 (run-hooks 'mail-citation-hook)
909 (if mail-yank-hooks
910 (run-hooks 'mail-yank-hooks)
911 (mail-indent-citation)))))
912 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
913 ;; It is cleaner to avoid activation, even though the command
914 ;; loop would deactivate the mark because we inserted text.
915 (goto-char (prog1 (mark t)
916 (set-marker (mark-marker) (point) (current-buffer))))
917 (if (not (eolp)) (insert ?\n)))))
918
919 (defun mail-yank-clear-headers (start end)
920 (if mail-yank-ignored-headers
921 (save-excursion
922 (goto-char start)
923 (if (search-forward "\n\n" end t)
924 (save-restriction
925 (narrow-to-region start (point))
926 (goto-char start)
927 (while (let ((case-fold-search t))
928 (re-search-forward mail-yank-ignored-headers nil t))
929 (beginning-of-line)
930 (delete-region (point)
931 (progn (re-search-forward "\n[^ \t]")
932 (forward-char -1)
933 (point)))))))))
934 \f
935 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
936
937 ;;;###autoload
938 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
939 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
940 When this function returns, the buffer `*mail*' is selected.
941 The value is t if the message was newly initialized; otherwise, nil.
942
943 Optionally, the signature file `mail-signature-file' can be inserted at the
944 end; see the variable `mail-signature'.
945
946 \\<mail-mode-map>
947 While editing message, type \\[mail-send-and-exit] to send the message and exit.
948
949 Various special commands starting with C-c are available in sendmail mode
950 to move to message header fields:
951 \\{mail-mode-map}
952
953 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
954 when the message is initialized.
955
956 If `mail-default-reply-to' is non-nil, it should be an address (a string);
957 a Reply-to: field with that address is inserted.
958
959 If `mail-archive-file-name' is non-nil, an FCC field with that file name
960 is inserted.
961
962 The normal hook `mail-setup-hook' is run after the message is
963 initialized. It can add more default fields to the message.
964
965 When calling from a program, the first argument if non-nil says
966 not to erase the existing contents of the `*mail*' buffer.
967
968 The second through fifth arguments,
969 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
970 the initial contents of those header fields.
971 These arguments should not have final newlines.
972 The sixth argument REPLYBUFFER is a buffer whose contents
973 should be yanked if the user types C-c C-y.
974 The seventh argument ACTIONS is a list of actions to take
975 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
976 when the message is sent, we apply FUNCTION to ARGS.
977 This is how Rmail arranges to mark messages `answered'."
978 (interactive "P")
979 ;;; This is commented out because I found it was confusing in practice.
980 ;;; It is easy enough to rename *mail* by hand with rename-buffer
981 ;;; if you want to have multiple mail buffers.
982 ;;; And then you can control which messages to save. --rms.
983 ;;; (let ((index 1)
984 ;;; buffer)
985 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
986 ;;; (if noerase
987 ;;; (progn
988 ;;; (while (and (setq buffer
989 ;;; (get-buffer (if (= 1 index) "*mail*"
990 ;;; (format "*mail*<%d>" index))))
991 ;;; (not (buffer-modified-p buffer)))
992 ;;; (setq index (1+ index)))
993 ;;; (if buffer (switch-to-buffer buffer)
994 ;;; ;; If none exists, start a new message.
995 ;;; ;; This will never re-use an existing unmodified mail buffer
996 ;;; ;; (since index is not 1 anymore). Perhaps it should.
997 ;;; (setq noerase nil))))
998 ;;; ;; Unless we found a modified message and are happy, start a new message.
999 ;;; (if (not noerase)
1000 ;;; (progn
1001 ;;; ;; Look for existing unmodified mail buffer.
1002 ;;; (while (and (setq buffer
1003 ;;; (get-buffer (if (= 1 index) "*mail*"
1004 ;;; (format "*mail*<%d>" index))))
1005 ;;; (buffer-modified-p buffer))
1006 ;;; (setq index (1+ index)))
1007 ;;; ;; If none, make a new one.
1008 ;;; (or buffer
1009 ;;; (setq buffer (generate-new-buffer "*mail*")))
1010 ;;; ;; Go there and initialize it.
1011 ;;; (switch-to-buffer buffer)
1012 ;;; (erase-buffer)
1013 ;;; (setq default-directory (expand-file-name "~/"))
1014 ;;; (auto-save-mode auto-save-default)
1015 ;;; (mail-mode)
1016 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1017 ;;; (if (and buffer-auto-save-file-name
1018 ;;; (file-exists-p buffer-auto-save-file-name))
1019 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1020 ;;; t))
1021 (pop-to-buffer "*mail*")
1022 (auto-save-mode auto-save-default)
1023 (mail-mode)
1024 ;; Disconnect the buffer from its visited file
1025 ;; (in case the user has actually visited a file *mail*).
1026 ; (set-visited-file-name nil)
1027 (let (initialized)
1028 (and (not noerase)
1029 (or (not (buffer-modified-p))
1030 (y-or-n-p "Unsent message being composed; erase it? "))
1031 (let ((inhibit-read-only t))
1032 (erase-buffer)
1033 (mail-setup to subject in-reply-to cc replybuffer actions)
1034 (setq initialized t)))
1035 (if (and buffer-auto-save-file-name
1036 (file-exists-p buffer-auto-save-file-name))
1037 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1038 initialized))
1039
1040 (defun mail-recover ()
1041 "Reread contents of current buffer from its last auto-save file."
1042 (interactive)
1043 (let ((file-name (make-auto-save-file-name)))
1044 (cond ((save-window-excursion
1045 (if (not (eq system-type 'vax-vms))
1046 (with-output-to-temp-buffer "*Directory*"
1047 (buffer-disable-undo standard-output)
1048 (let ((default-directory "/"))
1049 (call-process
1050 "ls" nil standard-output nil "-l" file-name))))
1051 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1052 (let ((buffer-read-only nil))
1053 (erase-buffer)
1054 (insert-file-contents file-name nil)))
1055 (t (error "mail-recover cancelled")))))
1056
1057 ;;;###autoload
1058 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1059 "Like `mail' command, but display mail buffer in another window."
1060 (interactive "P")
1061 (let ((pop-up-windows 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 ;;;###autoload
1070 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1071 "Like `mail' command, but display mail buffer in another frame."
1072 (interactive "P")
1073 (let ((pop-up-frames t)
1074 (special-display-buffer-names nil)
1075 (special-display-regexps nil)
1076 (same-window-buffer-names nil)
1077 (same-window-regexps nil))
1078 (pop-to-buffer "*mail*"))
1079 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1080
1081 ;;; Do not execute these when sendmail.el is loaded,
1082 ;;; only in loaddefs.el.
1083 ;;;###autoload (define-key ctl-x-map "m" 'mail)
1084 ;;;###autoload (define-key ctl-x-4-map "m" 'mail-other-window)
1085 ;;;###autoload (define-key ctl-x-5-map "m" 'mail-other-frame)
1086
1087 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1088
1089 ;;; Do not add anything but external entries on this page.
1090
1091 (provide 'sendmail)
1092
1093 ;;; sendmail.el ends here