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