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