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