]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
(sendmail-send-it): Call
[gnu-emacs] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs. -*- byte-compile-dynamic: t -*-
2
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 98, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This mode provides mail-sending facilities from within Emacs. It is
29 ;; documented in the Emacs user's manual.
30
31 ;;; Code:
32 (eval-when-compile
33 ;; Necessary to avoid recursive `require's.
34 (provide 'sendmail)
35 (require 'rmail)
36 (require 'mailalias))
37
38 (autoload 'rfc2047-encode-string "rfc2047")
39
40 (defgroup sendmail nil
41 "Mail sending commands for Emacs."
42 :prefix "mail-"
43 :group 'mail)
44
45 ;;;###autoload
46 (defcustom mail-from-style 'angles "\
47 *Specifies how \"From:\" fields look.
48
49 If `nil', they contain just the return address like:
50 king@grassland.com
51 If `parens', they look like:
52 king@grassland.com (Elvis Parsley)
53 If `angles', they look like:
54 Elvis Parsley <king@grassland.com>
55 If `system-default', allows the mailer to insert its default From field
56 derived from the envelope-from address.
57
58 In old versions of Emacs, the `system-default' setting also caused
59 Emacs to pass the proper email address from `user-mail-address'
60 to the mailer to specify the envelope-from address. But that is now
61 controlled by a separate variable, `mail-specify-envelope-from'."
62 :type '(choice (const nil) (const parens) (const angles)
63 (const system-default))
64 :version "20.3"
65 :group 'sendmail)
66
67 ;;;###autoload
68 (defcustom mail-specify-envelope-from nil
69 "*If non-nil, specify the envelope-from address when sending mail.
70 The value used to specify it is whatever is found in
71 `mail-envelope-from', with `user-mail-address' as fallback.
72
73 On most systems, specifying the envelope-from address
74 is a privileged operation."
75 :version "21.1"
76 :type 'boolean
77 :group 'sendmail)
78
79 (defcustom mail-envelope-from nil
80 "*If non-nil, designate the envelope-from address when sending mail.
81 If this is nil while `mail-specify-envelope-from' is non-nil, the
82 content of `user-mail-address' is used."
83 :version "21.1"
84 :type '(choice (string :tag "From-name")
85 (const :tag "Use From: header from message" header)
86 (const :tag "Use `user-mail-address'" nil))
87 :group 'sendmail)
88
89 ;;;###autoload
90 (defcustom mail-self-blind nil "\
91 *Non-nil means insert BCC to self in messages to be sent.
92 This is done when the message is initialized,
93 so you can remove or alter the BCC field to override the default."
94 :type 'boolean
95 :group 'sendmail)
96
97 ;;;###autoload
98 (defcustom mail-interactive nil "\
99 *Non-nil means when sending a message wait for and display errors.
100 nil means let mailer mail back a message to report errors."
101 :type 'boolean
102 :group 'sendmail)
103
104 ;;;###autoload
105 (defcustom mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
106 *Delete these headers from old message when it's inserted in a reply."
107 :type 'regexp
108 :group 'sendmail)
109
110 ;; Useful to set in site-init.el
111 ;;;###autoload
112 (defcustom send-mail-function 'sendmail-send-it
113 "Function to call to send the current buffer as mail.
114 The headers should be delimited by a line which is
115 not a valid RFC822 header or continuation line,
116 that matches the variable `mail-header-separator'.
117 This is used by the default mail-sending commands. See also
118 `message-send-mail-function' for use with the Message package."
119 :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
120 (function-item smtpmail-send-it :tag "Use SMTPmail package")
121 (function-item feedmail-send-it :tag "Use Feedmail package")
122 function)
123 :group 'sendmail)
124
125 ;;;###autoload
126 (defcustom mail-header-separator "--text follows this line--" "\
127 *Line used to separate headers from text in messages being composed."
128 :type 'string
129 :group 'sendmail)
130
131 ;; Set up mail-header-separator for use as a category text property.
132 (put 'mail-header-separator 'rear-nonsticky '(category))
133 ;;; This was a nice idea, for preventing accidental modification of
134 ;;; the separator. But I found it also prevented or obstructed
135 ;;; certain deliberate operations, such as copying the separator line
136 ;;; up to the top to send myself a copy of an already sent outgoing message
137 ;;; and other things. So I turned it off. --rms.
138 ;;;(put 'mail-header-separator 'read-only t)
139
140 ;;;###autoload
141 (defcustom mail-archive-file-name nil "\
142 *Name of file to write all outgoing messages in, or nil for none.
143 This can be an inbox file or an Rmail file."
144 :type '(choice file (const nil))
145 :group 'sendmail)
146
147 ;;;###autoload
148 (defcustom mail-default-reply-to nil
149 "*Address to insert as default Reply-to field of outgoing messages.
150 If nil, it will be initialized from the REPLYTO environment variable
151 when you first send mail."
152 :type '(choice (const nil) string)
153 :group 'sendmail)
154
155 ;;;###autoload
156 (defcustom mail-alias-file nil
157 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
158 This file defines aliases to be expanded by the mailer; this is a different
159 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
160 This variable has no effect unless your system uses sendmail as its mailer."
161 :type '(choice (const nil) file)
162 :group 'sendmail)
163
164 ;;;###autoload
165 (defcustom mail-personal-alias-file "~/.mailrc"
166 "*If non-nil, the name of the user's personal mail alias file.
167 This file typically should be in same format as the `.mailrc' file used by
168 the `Mail' or `mailx' program.
169 This file need not actually exist."
170 :type '(choice (const nil) file)
171 :group 'sendmail)
172
173 (defcustom mail-setup-hook nil
174 "Normal hook, run each time a new outgoing mail message is initialized.
175 The function `mail-setup' runs this hook."
176 :type 'hook
177 :options '(fortune-to-signature spook mail-abbrevs-setup)
178 :group 'sendmail)
179
180 (defvar mail-aliases t
181 "Alist of mail address aliases,
182 or t meaning should be initialized from your mail aliases file.
183 \(The file's name is normally `~/.mailrc', but your MAILRC environment
184 variable can override that name.)
185 The alias definitions in the file have this form:
186 alias ALIAS MEANING")
187
188 (defvar mail-alias-modtime nil
189 "The modification time of your mail alias file when it was last examined.")
190
191 (defcustom mail-yank-prefix nil
192 "*Prefix insert on lines of yanked message being replied to.
193 nil means use indentation."
194 :type '(choice (const nil) string)
195 :group 'sendmail)
196
197 (defcustom mail-indentation-spaces 3
198 "*Number of spaces to insert at the beginning of each cited line.
199 Used by `mail-yank-original' via `mail-indent-citation'."
200 :type 'integer
201 :group 'sendmail)
202 (defvar mail-yank-hooks nil
203 "Obsolete hook for modifying a citation just inserted in the mail buffer.
204 Each hook function can find the citation between (point) and (mark t).
205 And each hook function should leave point and mark around the citation
206 text as modified.
207
208 This is a normal hook, misnamed for historical reasons.
209 It is semi-obsolete and mail agents should no longer use it.")
210
211 (defcustom mail-citation-hook nil
212 "*Hook for modifying a citation just inserted in the mail buffer.
213 Each hook function can find the citation between (point) and (mark t),
214 and should leave point and mark around the citation text as modified.
215 The hook functions can find the header of the cited message
216 in the variable `mail-citation-header', whether or not this is included
217 in the cited portion of the message.
218
219 If this hook is entirely empty (nil), a default action is taken
220 instead of no action."
221 :type 'hook
222 :group 'sendmail)
223
224 (defvar mail-citation-header nil
225 "While running `mail-citation-hook', this variable holds the message header.
226 This enables the hook functions to see the whole message header
227 regardless of what part of it (if any) is included in the cited text.")
228
229 (defcustom mail-citation-prefix-regexp "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
230 "*Regular expression to match a citation prefix plus whitespace.
231 It should match whatever sort of citation prefixes you want to handle,
232 with whitespace before and after; it should also match just whitespace.
233 The default value matches citations like `foo-bar>' plus whitespace."
234 :type 'regexp
235 :group 'sendmail
236 :version "20.3")
237
238 (defvar mail-abbrevs-loaded nil)
239 (defvar mail-mode-map nil)
240
241 (autoload 'build-mail-aliases "mailalias"
242 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
243 nil)
244
245 (autoload 'expand-mail-aliases "mailalias"
246 "Expand all mail aliases in suitable header fields found between BEG and END.
247 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
248 Optional second arg EXCLUDE may be a regular expression defining text to be
249 removed from alias expansions."
250 nil)
251
252 ;;;###autoload
253 (defcustom mail-signature nil
254 "*Text inserted at end of mail buffer when a message is initialized.
255 If t, it means to insert the contents of the file `mail-signature-file'.
256 If a string, that string is inserted.
257 (To make a proper signature, the string should begin with \\n\\n-- \\n,
258 which is the standard way to delimit a signature in a message.)
259 Otherwise, it should be an expression; it is evaluated
260 and should insert whatever you want to insert."
261 :type '(choice (const :tag "None" nil)
262 (const :tag "Use `.signature' file" t)
263 (string :tag "String to insert")
264 (sexp :tag "Expression to evaluate"))
265 :group 'sendmail)
266 (put 'mail-signature 'risky-local-variable t)
267
268 (defcustom mail-signature-file "~/.signature"
269 "*File containing the text inserted at end of mail buffer."
270 :type 'file
271 :group 'sendmail)
272
273 ;;;###autoload
274 (defcustom mail-default-directory "~/"
275 "*Directory for mail buffers.
276 Value of `default-directory' for mail buffers.
277 This directory is used for auto-save files of mail buffers."
278 :type '(directory :tag "Directory")
279 :group 'sendmail)
280
281 (defvar mail-reply-action nil)
282 (defvar mail-send-actions nil
283 "A list of actions to be performed upon successful sending of a message.")
284 (put 'mail-reply-action 'permanent-local t)
285 (put 'mail-send-actions 'permanent-local t)
286
287 (defcustom mail-default-headers nil
288 "*A string containing header lines, to be inserted in outgoing messages.
289 It is inserted before you edit the message,
290 so you can edit or delete these lines."
291 :type '(choice (const nil) string)
292 :group 'sendmail)
293
294 (defcustom mail-bury-selects-summary t
295 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
296 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
297 the RMAIL summary buffer before returning, if it exists and this variable
298 is non-nil."
299 :type 'boolean
300 :group 'sendmail)
301
302 (defcustom mail-send-nonascii 'mime
303 "*Specify whether to allow sending non-ASCII characters in mail.
304 If t, that means do allow it. nil means don't allow it.
305 `query' means ask the user each time.
306 `mime' means add an appropriate MIME header if none already present.
307 The default is `mime'.
308 Including non-ASCII characters in a mail message can be problematical
309 for the recipient, who may not know how to decode them properly."
310 :type '(choice (const t) (const nil) (const query) (const mime))
311 :group 'sendmail)
312
313 (defcustom mail-use-dsn nil
314 "*Ask MTA for notification of failed, delayed or successful delivery.
315 Note that only some MTAs (currently only recent versions of Sendmail)
316 support Delivery Status Notification."
317 :group 'sendmail
318 :type '(repeat (radio (const :tag "Failure" failure)
319 (const :tag "Delay" delay)
320 (const :tag "Success" success)))
321 :version "21.4")
322
323 ;; Note: could use /usr/ucb/mail instead of sendmail;
324 ;; options -t, and -v if not interactive.
325 (defvar mail-mailer-swallows-blank-line
326 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
327 (file-readable-p "/etc/sendmail.cf")
328 (let ((buffer (get-buffer-create " *temp*")))
329 (unwind-protect
330 (save-excursion
331 (set-buffer buffer)
332 (insert-file-contents "/etc/sendmail.cf")
333 (goto-char (point-min))
334 (let ((case-fold-search nil))
335 (re-search-forward "^OR\\>" nil t)))
336 (kill-buffer buffer))))
337 ;; According to RFC822, "The field-name must be composed of printable
338 ;; ASCII characters (i.e. characters that have decimal values between
339 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
340 ;; space, or colon.
341 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
342 "Set this non-nil if the system's mailer runs the header and body together.
343 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
344 The value should be an expression to test whether the problem will
345 actually occur.")
346
347 (defvar mail-mode-syntax-table
348 (let ((st (make-syntax-table)))
349 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
350 (modify-syntax-entry ?% ". " st)
351 st)
352 "Syntax table used while in `mail-mode'.")
353
354 (defvar mail-font-lock-keywords
355 (eval-when-compile
356 (let* ((cite-chars "[>|}]")
357 (cite-prefix "[:alpha:]")
358 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
359 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
360 '("^\\(B?CC\\|Reply-to\\):" . font-lock-keyword-face)
361 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
362 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
363 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
364 '(eval .
365 (let ((separator (if (zerop (length mail-header-separator))
366 " \\`\\' "
367 (regexp-quote mail-header-separator))))
368 (cons (concat "^" separator "$") 'font-lock-warning-face)))
369 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
370 `(,cite-chars
371 (,(concat "\\=[ \t]*"
372 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
373 "\\(" cite-chars "[ \t]*\\)\\)+"
374 "\\(.*\\)")
375 (beginning-of-line) (end-of-line)
376 (2 font-lock-constant-face nil t)
377 (4 font-lock-comment-face nil t)))
378 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
379 . font-lock-string-face))))
380 "Additional expressions to highlight in Mail mode.")
381
382 \f
383 (defun sendmail-sync-aliases ()
384 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
385 (or (equal mail-alias-modtime modtime)
386 (setq mail-alias-modtime modtime
387 mail-aliases t))))
388
389 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
390 (or mail-default-reply-to
391 (setq mail-default-reply-to (getenv "REPLYTO")))
392 (sendmail-sync-aliases)
393 (if (eq mail-aliases t)
394 (progn
395 (setq mail-aliases nil)
396 (if (file-exists-p mail-personal-alias-file)
397 (build-mail-aliases))))
398 ;; Don't leave this around from a previous message.
399 (kill-local-variable 'buffer-file-coding-system)
400 ;; This doesn't work for enable-multibyte-characters.
401 ;; (kill-local-variable 'enable-multibyte-characters)
402 (set-buffer-multibyte default-enable-multibyte-characters)
403 (if current-input-method
404 (inactivate-input-method))
405 (setq mail-send-actions actions)
406 (setq mail-reply-action replybuffer)
407 (goto-char (point-min))
408 (insert "To: ")
409 (save-excursion
410 (if to
411 ;; Here removed code to extract names from within <...>
412 ;; on the assumption that mail-strip-quoted-names
413 ;; has been called and has done so.
414 (let ((fill-prefix "\t")
415 (address-start (point)))
416 (insert to "\n")
417 (fill-region-as-paragraph address-start (point-max))
418 (goto-char (point-max))
419 (unless (bolp)
420 (newline)))
421 (newline))
422 (if cc
423 (let ((fill-prefix "\t")
424 (address-start (progn (insert "CC: ") (point))))
425 (insert cc "\n")
426 (fill-region-as-paragraph address-start (point-max))
427 (goto-char (point-max))
428 (unless (bolp)
429 (newline))))
430 (if in-reply-to
431 (let ((fill-prefix "\t")
432 (fill-column 78)
433 (address-start (point)))
434 (insert "In-reply-to: " in-reply-to "\n")
435 (fill-region-as-paragraph address-start (point-max))
436 (goto-char (point-max))
437 (unless (bolp)
438 (newline))))
439 (insert "Subject: " (or subject "") "\n")
440 (if mail-default-headers
441 (insert mail-default-headers))
442 (if mail-default-reply-to
443 (insert "Reply-to: " mail-default-reply-to "\n"))
444 (if mail-self-blind
445 (insert "BCC: " user-mail-address "\n"))
446 (if mail-archive-file-name
447 (insert "FCC: " mail-archive-file-name "\n"))
448 (put-text-property (point)
449 (progn
450 (insert mail-header-separator "\n")
451 (1- (point)))
452 'category 'mail-header-separator)
453 ;; Insert the signature. But remember the beginning of the message.
454 (if to (setq to (point)))
455 (cond ((eq mail-signature t)
456 (if (file-exists-p mail-signature-file)
457 (progn
458 (insert "\n\n-- \n")
459 (insert-file-contents mail-signature-file))))
460 ((stringp mail-signature)
461 (insert mail-signature))
462 (t
463 (eval mail-signature)))
464 (goto-char (point-max))
465 (or (bolp) (newline)))
466 (if to (goto-char to))
467 (or to subject in-reply-to
468 (set-buffer-modified-p nil))
469 (run-hooks 'mail-setup-hook))
470 \f
471 (defcustom mail-mode-hook nil
472 "Hook run by Mail mode."
473 :group 'sendmail
474 :type 'hook
475 :options '(footnote-mode))
476
477 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
478 ;;;###autoload
479 (define-derived-mode mail-mode text-mode "Mail"
480 "Major mode for editing mail to be sent.
481 Like Text Mode but with these additional commands:
482 \\[mail-send] mail-send (send the message) \\[mail-send-and-exit] mail-send-and-exit
483 Here are commands that move to a header field (and create it if there isn't):
484 \\[mail-to] move to To: \\[mail-subject] move to Subject:
485 \\[mail-cc] move to CC: \\[mail-bcc] move to BCC:
486 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
487 \\[mail-text] mail-text (move to beginning of message text).
488 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
489 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
490 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
491 \\[mail-sent-via] mail-sent-via (add a Sent-via field for each To or CC).
492 Turning on Mail mode runs the normal hooks `text-mode-hook' and
493 `mail-mode-hook' (in that order)."
494 (make-local-variable 'mail-reply-action)
495 (make-local-variable 'mail-send-actions)
496 (setq buffer-offer-save t)
497 (make-local-variable 'font-lock-defaults)
498 (setq font-lock-defaults '(mail-font-lock-keywords t t))
499 (make-local-variable 'paragraph-separate)
500 (make-local-variable 'paragraph-start)
501 (make-local-variable 'normal-auto-fill-function)
502 (setq normal-auto-fill-function 'mail-mode-auto-fill)
503 (make-local-variable 'fill-paragraph-function)
504 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
505 ;; Allow using comment commands to add/remove quoting (this only does
506 ;; anything if mail-yank-prefix is set to a non-nil value).
507 (set (make-local-variable 'comment-start) mail-yank-prefix)
508 (make-local-variable 'adaptive-fill-regexp)
509 (setq adaptive-fill-regexp
510 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
511 adaptive-fill-regexp))
512 (make-local-variable 'adaptive-fill-first-line-regexp)
513 (setq adaptive-fill-first-line-regexp
514 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
515 adaptive-fill-first-line-regexp))
516 ;; `-- ' precedes the signature. `-----' appears at the start of the
517 ;; lines that delimit forwarded messages.
518 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
519 ;; are also sometimes used and should be separators.
520 (setq paragraph-start (concat (regexp-quote mail-header-separator)
521 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
522 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
523 "-- $\\|---+$\\|"
524 page-delimiter))
525 (setq paragraph-separate paragraph-start))
526
527
528 (defun mail-header-end ()
529 "Return the buffer location of the end of headers, as a number."
530 (save-restriction
531 (widen)
532 (save-excursion
533 (rfc822-goto-eoh)
534 (point))))
535
536 (defun mail-text-start ()
537 "Return the buffer location of the start of text, as a number."
538 (save-restriction
539 (widen)
540 (save-excursion
541 (rfc822-goto-eoh)
542 (forward-line 1)
543 (point))))
544
545 (defun mail-sendmail-delimit-header ()
546 "Set up whatever header delimiter convention sendmail will use.
547 Concretely: replace the first blank line in the header with the separator."
548 (rfc822-goto-eoh)
549 (insert mail-header-separator)
550 (point))
551
552 (defun mail-sendmail-undelimit-header ()
553 "Remove header separator to put the message in correct form for sendmail.
554 Leave point at the start of the delimiter line."
555 (rfc822-goto-eoh)
556 (delete-region (point) (progn (end-of-line) (point))))
557
558 (defun mail-mode-auto-fill ()
559 "Carry out Auto Fill for Mail mode.
560 If within the headers, this makes the new lines into continuation lines."
561 (if (< (point) (mail-header-end))
562 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
563 (if (do-auto-fill)
564 (save-excursion
565 (beginning-of-line)
566 (while (not (eq (point) old-line-start))
567 ;; Use insert-before-markers in case we're inserting
568 ;; before the saved value of point (which is common).
569 (insert-before-markers " ")
570 (forward-line -1))
571 t)))
572 (do-auto-fill)))
573
574 (defun mail-mode-fill-paragraph (arg)
575 ;; Do something special only if within the headers.
576 (if (< (point) (mail-header-end))
577 (let (beg end fieldname)
578 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
579 (setq beg (point)))
580 (setq fieldname
581 (downcase (buffer-substring beg (1- (match-end 0))))))
582 (forward-line 1)
583 ;; Find continuation lines and get rid of their continuation markers.
584 (while (looking-at "[ \t]")
585 (delete-horizontal-space)
586 (forward-line 1))
587 (setq end (point-marker))
588 (goto-char beg)
589 ;; If this field contains addresses,
590 ;; make sure we can fill after each address.
591 (if (member fieldname
592 '("to" "cc" "bcc" "from" "reply-to"
593 "resent-to" "resent-cc" "resent-bcc"
594 "resent-from" "resent-reply-to"))
595 (while (search-forward "," end t)
596 (or (looking-at "[ \t]")
597 (insert " "))))
598 (fill-region-as-paragraph beg end)
599 ;; Mark all lines except the first as continuations.
600 (goto-char beg)
601 (forward-line 1)
602 (while (< (point) end)
603 (insert " ")
604 (forward-line 1))
605 (move-marker end nil)
606 t)))
607 \f
608 ;;; Set up keymap.
609
610 (if mail-mode-map
611 nil
612 (setq mail-mode-map (make-sparse-keymap))
613 (define-key mail-mode-map "\M-\t" 'mail-complete)
614 (define-key mail-mode-map "\C-c?" 'describe-mode)
615 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
616 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
617 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
618 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
619 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
620 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
621 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
622 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
623 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
624 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
625 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
626 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
627 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
628 (define-key mail-mode-map "\C-c\C-s" 'mail-send)
629 (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
630
631 (define-key mail-mode-map [menu-bar mail]
632 (cons "Mail" (make-sparse-keymap "Mail")))
633
634 (define-key mail-mode-map [menu-bar mail fill]
635 '("Fill Citation" . mail-fill-yanked-message))
636
637 (define-key mail-mode-map [menu-bar mail yank]
638 '("Cite Original" . mail-yank-original))
639
640 (define-key mail-mode-map [menu-bar mail signature]
641 '("Insert Signature" . mail-signature))
642
643 (define-key mail-mode-map [menu-bar mail mail-sep]
644 '("--"))
645
646 (define-key mail-mode-map [menu-bar mail cancel]
647 '("Cancel" . mail-dont-send))
648
649 (define-key mail-mode-map [menu-bar mail send-stay]
650 '("Send, Keep Editing" . mail-send))
651
652 (define-key mail-mode-map [menu-bar mail send]
653 '("Send Message" . mail-send-and-exit))
654
655 (define-key mail-mode-map [menu-bar headers]
656 (cons "Headers" (make-sparse-keymap "Move to Header")))
657
658 (define-key mail-mode-map [menu-bar headers text]
659 '("Text" . mail-text))
660
661 (define-key mail-mode-map [menu-bar headers expand-aliases]
662 '("Expand Aliases" . expand-mail-aliases))
663
664 (define-key mail-mode-map [menu-bar headers sent-via]
665 '("Sent Via" . mail-sent-via))
666
667 (define-key mail-mode-map [menu-bar headers reply-to]
668 '("Reply-To" . mail-reply-to))
669
670 (define-key mail-mode-map [menu-bar headers bcc]
671 '("Bcc" . mail-bcc))
672
673 (define-key mail-mode-map [menu-bar headers fcc]
674 '("Fcc" . mail-fcc))
675
676 (define-key mail-mode-map [menu-bar headers cc]
677 '("Cc" . mail-cc))
678
679 (define-key mail-mode-map [menu-bar headers subject]
680 '("Subject" . mail-subject))
681
682 (define-key mail-mode-map [menu-bar headers to]
683 '("To" . mail-to))
684 \f
685 ;; User-level commands for sending.
686
687 (defun mail-send-and-exit (arg)
688 "Send message like `mail-send', then, if no errors, exit from mail buffer.
689 Prefix arg means don't delete this window."
690 (interactive "P")
691 (mail-send)
692 (mail-bury arg))
693
694 (defun mail-dont-send (arg)
695 "Don't send the message you have been editing.
696 Prefix arg means don't delete this window."
697 (interactive "P")
698 (mail-bury arg))
699
700 (defun mail-bury (arg)
701 "Bury this mail buffer."
702 (let ((newbuf (other-buffer (current-buffer))))
703 (bury-buffer (current-buffer))
704 (if (and (or (window-dedicated-p (frame-selected-window))
705 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
706 (not (null (delq (selected-frame) (visible-frame-list)))))
707 (delete-frame (selected-frame))
708 (let (rmail-flag summary-buffer)
709 (and (not arg)
710 (not (one-window-p))
711 (save-excursion
712 (set-buffer (window-buffer (next-window (selected-window) 'not)))
713 (setq rmail-flag (eq major-mode 'rmail-mode))
714 (setq summary-buffer
715 (and mail-bury-selects-summary
716 (boundp 'rmail-summary-buffer)
717 rmail-summary-buffer
718 (buffer-name rmail-summary-buffer)
719 (not (get-buffer-window rmail-summary-buffer))
720 rmail-summary-buffer))))
721 (if rmail-flag
722 ;; If the Rmail buffer has a summary, show that.
723 (if summary-buffer (switch-to-buffer summary-buffer)
724 (delete-window))
725 (switch-to-buffer newbuf))))))
726
727 (defcustom mail-send-hook nil
728 "Hook run just before sending mail with `mail-send'."
729 :type 'hook
730 :options '(flyspell-mode-off)
731 :group 'sendmail)
732
733 (defun mail-send ()
734 "Send the message in the current buffer.
735 If `mail-interactive' is non-nil, wait for success indication
736 or error messages, and inform user.
737 Otherwise any failure is reported in a message back to
738 the user from the mailer."
739 (interactive)
740 (if (if buffer-file-name
741 (y-or-n-p "Send buffer contents as mail message? ")
742 (or (buffer-modified-p)
743 (y-or-n-p "Message already sent; resend? ")))
744 (let ((inhibit-read-only t)
745 (opoint (point)))
746 (unless (memq mail-send-nonascii '(t mime))
747 (goto-char (point-min))
748 (skip-chars-forward "\0-\177")
749 (or (= (point) (point-max))
750 (if (eq mail-send-nonascii 'query)
751 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
752 (error "Aborted"))
753 (error "Message contains non-ASCII characters"))))
754 ;; Complain about any invalid line.
755 (goto-char (point-min))
756 (while (< (point) (mail-header-end))
757 (unless (looking-at "[ \t]\\|.*:\\|$")
758 (push-mark opoint)
759 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
760 (forward-line 1))
761 (goto-char opoint)
762 (run-hooks 'mail-send-hook)
763 (message "Sending...")
764 (funcall send-mail-function)
765 ;; Now perform actions on successful sending.
766 (while mail-send-actions
767 (condition-case nil
768 (apply (car (car mail-send-actions))
769 (cdr (car mail-send-actions)))
770 (error))
771 (setq mail-send-actions (cdr mail-send-actions)))
772 (message "Sending...done")
773 ;; If buffer has no file, mark it as unmodified and delete auto-save.
774 (if (not buffer-file-name)
775 (progn
776 (set-buffer-modified-p nil)
777 (delete-auto-save-file-if-necessary t))))))
778
779 (defun mail-envelope-from ()
780 "Return the envelope mail address to use when sending mail.
781 This function uses `mail-envelope-from'."
782 (if (eq mail-envelope-from 'header)
783 (nth 1 (mail-extract-address-components
784 (mail-fetch-field "From")))
785 mail-envelope-from))
786 \f
787 ;; This does the real work of sending a message via sendmail.
788 ;; It is called via the variable send-mail-function.
789
790 ;;;###autoload
791 (defvar sendmail-coding-system nil
792 "*Coding system for encoding the outgoing mail.
793 This has higher priority than `default-buffer-file-coding-system'
794 and `default-sendmail-coding-system',
795 but lower priority than the local value of `buffer-file-coding-system'.
796 See also the function `select-message-coding-system'.")
797
798 ;;;###autoload
799 (defvar default-sendmail-coding-system 'iso-latin-1
800 "Default coding system for encoding the outgoing mail.
801 This variable is used only when `sendmail-coding-system' is nil.
802
803 This variable is set/changed by the command set-language-environment.
804 User should not set this variable manually,
805 instead use sendmail-coding-system to get a constant encoding
806 of outgoing mails regardless of the current language environment.
807 See also the function `select-message-coding-system'.")
808
809 (defun sendmail-send-it ()
810 "Send the current mail buffer using the Sendmail package.
811 This is a suitable value for `send-mail-function'. It sends using the
812 external program defined by `sendmail-program'."
813 (require 'mail-utils)
814 (let ((errbuf (if mail-interactive
815 (generate-new-buffer " sendmail errors")
816 0))
817 (tembuf (generate-new-buffer " sendmail temp"))
818 (multibyte enable-multibyte-characters)
819 (case-fold-search nil)
820 (selected-coding (select-message-coding-system))
821 ;;; resend-to-addresses
822 delimline
823 fcc-was-found
824 (mailbuf (current-buffer))
825 (program (if (boundp 'sendmail-program)
826 sendmail-program
827 "/usr/lib/sendmail"))
828 ;; Examine these variables now, so that
829 ;; local binding in the mail buffer will take effect.
830 (envelope-from
831 (and mail-specify-envelope-from
832 (or (mail-envelope-from) user-mail-address))))
833 (unwind-protect
834 (save-excursion
835 (set-buffer tembuf)
836 (erase-buffer)
837 (unless multibyte
838 (set-buffer-multibyte nil))
839 (insert-buffer-substring mailbuf)
840 (goto-char (point-max))
841 ;; require one newline at the end.
842 (or (= (preceding-char) ?\n)
843 (insert ?\n))
844 ;; Change header-delimiter to be what sendmail expects.
845 (goto-char (mail-header-end))
846 (delete-region (point) (progn (end-of-line) (point)))
847 (setq delimline (point-marker))
848 (sendmail-sync-aliases)
849 (if mail-aliases
850 (expand-mail-aliases (point-min) delimline))
851 (goto-char (point-min))
852 ;; Ignore any blank lines in the header
853 (while (and (re-search-forward "\n\n\n*" delimline t)
854 (< (point) delimline))
855 (replace-match "\n"))
856 (goto-char (point-min))
857 (let ((case-fold-search t))
858 ;;; (goto-char (point-min))
859 ;;; (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
860 ;;; (setq resend-to-addresses
861 ;;; (save-restriction
862 ;;; (narrow-to-region (point)
863 ;;; (save-excursion
864 ;;; (forward-line 1)
865 ;;; (while (looking-at "^[ \t]")
866 ;;; (forward-line 1))
867 ;;; (point)))
868 ;;; (append (mail-parse-comma-list)
869 ;;; resend-to-addresses)))
870 ;;; ;; Delete Resent-BCC ourselves
871 ;;; (if (save-excursion (beginning-of-line)
872 ;;; (looking-at "resent-bcc"))
873 ;;; (delete-region (save-excursion (beginning-of-line) (point))
874 ;;; (save-excursion (end-of-line) (1+ (point))))))
875 ;;; Apparently this causes a duplicate Sender.
876 ;;; ;; If the From is different than current user, insert Sender.
877 ;;; (goto-char (point-min))
878 ;;; (and (re-search-forward "^From:" delimline t)
879 ;;; (progn
880 ;;; (require 'mail-utils)
881 ;;; (not (string-equal
882 ;;; (mail-strip-quoted-names
883 ;;; (save-restriction
884 ;;; (narrow-to-region (point-min) delimline)
885 ;;; (mail-fetch-field "From")))
886 ;;; (user-login-name))))
887 ;;; (progn
888 ;;; (forward-line 1)
889 ;;; (insert "Sender: " (user-login-name) "\n")))
890 ;; Don't send out a blank subject line
891 (goto-char (point-min))
892 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
893 (replace-match "")
894 ;; This one matches a Subject just before the header delimiter.
895 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
896 (= (match-end 0) delimline))
897 (replace-match "")))
898 ;; Put the "From:" field in unless for some odd reason
899 ;; they put one in themselves.
900 (goto-char (point-min))
901 (if (not (re-search-forward "^From:" delimline t))
902 (let* ((login user-mail-address)
903 (fullname (user-full-name))
904 (quote-fullname nil))
905 (if (string-match "[^\0-\177]" fullname)
906 (setq fullname (rfc2047-encode-string fullname)
907 quote-fullname t))
908 (cond ((eq mail-from-style 'angles)
909 (insert "From: " fullname)
910 (let ((fullname-start (+ (point-min) 6))
911 (fullname-end (point-marker)))
912 (goto-char fullname-start)
913 ;; Look for a character that cannot appear unquoted
914 ;; according to RFC 822.
915 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
916 fullname-end 1)
917 quote-fullname)
918 (progn
919 ;; Quote fullname, escaping specials.
920 (goto-char fullname-start)
921 (insert "\"")
922 (while (re-search-forward "[\"\\]"
923 fullname-end 1)
924 (replace-match "\\\\\\&" t))
925 (insert "\""))))
926 (insert " <" login ">\n"))
927 ((eq mail-from-style 'parens)
928 (insert "From: " login " (")
929 (let ((fullname-start (point)))
930 (if quote-fullname
931 (insert "\""))
932 (insert fullname)
933 (if quote-fullname
934 (insert "\""))
935 (let ((fullname-end (point-marker)))
936 (goto-char fullname-start)
937 ;; RFC 822 says \ and nonmatching parentheses
938 ;; must be escaped in comments.
939 ;; Escape every instance of ()\ ...
940 (while (re-search-forward "[()\\]" fullname-end 1)
941 (replace-match "\\\\\\&" t))
942 ;; ... then undo escaping of matching parentheses,
943 ;; including matching nested parentheses.
944 (goto-char fullname-start)
945 (while (re-search-forward
946 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
947 fullname-end 1)
948 (replace-match "\\1(\\3)" t)
949 (goto-char fullname-start))))
950 (insert ")\n"))
951 ((null mail-from-style)
952 (insert "From: " login "\n"))
953 ((eq mail-from-style 'system-default)
954 nil)
955 (t (error "Invalid value for `mail-from-style'")))))
956 ;; Possibly add a MIME header for the current coding system
957 (let (charset)
958 (goto-char (point-min))
959 (and (eq mail-send-nonascii 'mime)
960 (not (re-search-forward "^MIME-version:" delimline t))
961 (progn (skip-chars-forward "\0-\177")
962 (/= (point) (point-max)))
963 selected-coding
964 (setq charset
965 (coding-system-get selected-coding 'mime-charset))
966 (goto-char delimline)
967 (insert "MIME-version: 1.0\n"
968 "Content-type: text/plain; charset="
969 (symbol-name charset) "\n"
970 "Content-Transfer-Encoding: 8bit\n")))
971 ;; Insert an extra newline if we need it to work around
972 ;; Sun's bug that swallows newlines.
973 (goto-char (1+ delimline))
974 (if (eval mail-mailer-swallows-blank-line)
975 (newline))
976 ;; Find and handle any FCC fields.
977 (goto-char (point-min))
978 (if (re-search-forward "^FCC:" delimline t)
979 (progn
980 (setq fcc-was-found t)
981 (mail-do-fcc delimline)))
982 (if mail-interactive
983 (save-excursion
984 (set-buffer errbuf)
985 (erase-buffer))))
986 (goto-char (point-min))
987 (if (let ((case-fold-search t))
988 (re-search-forward "^To:\\|^cc:\\|^bcc:\\|^resent-to:\
989 \\|^resent-cc:\\|^resent-bcc:"
990 delimline t))
991 (let* ((default-directory "/")
992 (coding-system-for-write selected-coding)
993 (args
994 (append (list (point-min) (point-max)
995 program
996 nil errbuf nil "-oi")
997 (and envelope-from
998 (list "-f" envelope-from))
999 ;;; ;; Don't say "from root" if running under su.
1000 ;;; (and (equal (user-real-login-name) "root")
1001 ;;; (list "-f" (user-login-name)))
1002 (and mail-alias-file
1003 (list (concat "-oA" mail-alias-file)))
1004 (if mail-interactive
1005 ;; These mean "report errors to terminal"
1006 ;; and "deliver interactively"
1007 '("-oep" "-odi")
1008 ;; These mean "report errors by mail"
1009 ;; and "deliver in background".
1010 '("-oem" "-odb"))
1011 ;;; ;; Get the addresses from the message
1012 ;;; ;; unless this is a resend.
1013 ;;; ;; We must not do that for a resend
1014 ;;; ;; because we would find the original addresses.
1015 ;;; ;; For a resend, include the specific addresses.
1016 ;;; (or resend-to-addresses
1017 '("-t")
1018 ;;; )
1019 (if mail-use-dsn
1020 (list "-N" (mapconcat 'symbol-name
1021 mail-use-dsn ",")))
1022 )
1023 )
1024 (exit-value (apply 'call-process-region args)))
1025 (or (null exit-value) (zerop exit-value)
1026 (error "Sending...failed with exit value %d" exit-value)))
1027 (or fcc-was-found
1028 (error "No recipients")))
1029 (if mail-interactive
1030 (save-excursion
1031 (set-buffer errbuf)
1032 (goto-char (point-min))
1033 (while (re-search-forward "\n\n* *" nil t)
1034 (replace-match "; "))
1035 (if (not (zerop (buffer-size)))
1036 (error "Sending...failed to %s"
1037 (buffer-substring (point-min) (point-max)))))))
1038 (kill-buffer tembuf)
1039 (if (bufferp errbuf)
1040 (kill-buffer errbuf)))))
1041
1042 (defun mail-do-fcc (header-end)
1043 (let (fcc-list
1044 (rmailbuf (current-buffer))
1045 (time (current-time))
1046 (tembuf (generate-new-buffer " rmail output"))
1047 (case-fold-search t))
1048 (unless (markerp header-end)
1049 (error "Value of `header-end' must be a marker"))
1050 (save-excursion
1051 (goto-char (point-min))
1052 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1053 (setq fcc-list (cons (buffer-substring (point)
1054 (progn
1055 (end-of-line)
1056 (skip-chars-backward " \t")
1057 (point)))
1058 fcc-list))
1059 (delete-region (match-beginning 0)
1060 (progn (forward-line 1) (point))))
1061 (set-buffer tembuf)
1062 (erase-buffer)
1063 ;; This initial newline is written out if the fcc file already exists.
1064 (insert "\nFrom " (user-login-name) " "
1065 (current-time-string time) "\n")
1066 ;; Insert the time zone before the year.
1067 (forward-char -1)
1068 (forward-word -1)
1069 (require 'mail-utils)
1070 (insert (mail-rfc822-time-zone time) " ")
1071 (goto-char (point-max))
1072 (insert-buffer-substring rmailbuf)
1073 ;; Make sure messages are separated.
1074 (goto-char (point-max))
1075 (insert ?\n)
1076 (goto-char 2)
1077 ;; ``Quote'' "^From " as ">From "
1078 ;; (note that this isn't really quoting, as there is no requirement
1079 ;; that "^[>]+From " be quoted in the same transparent way.)
1080 (let ((case-fold-search nil))
1081 (while (search-forward "\nFrom " nil t)
1082 (forward-char -5)
1083 (insert ?>)))
1084 (while fcc-list
1085 (let* ((buffer (find-buffer-visiting (car fcc-list)))
1086 (curbuf (current-buffer))
1087 dont-write-the-file
1088 buffer-matches-file
1089 (beg (point-min)) (end (point-max))
1090 (beg2 (save-excursion (goto-char (point-min))
1091 (forward-line 2) (point))))
1092 (if buffer
1093 ;; File is present in a buffer => append to that buffer.
1094 (save-excursion
1095 (set-buffer buffer)
1096 (setq buffer-matches-file
1097 (and (not (buffer-modified-p))
1098 (verify-visited-file-modtime buffer)))
1099 ;; Keep the end of the accessible portion at the same place
1100 ;; unless it is the end of the buffer.
1101 (let ((max (if (/= (1+ (buffer-size)) (point-max))
1102 (point-max))))
1103 (unwind-protect
1104 ;; Code below lifted from rmailout.el
1105 ;; function rmail-output-to-rmail-file:
1106 (let ((buffer-read-only nil)
1107 (msg (and (boundp 'rmail-current-message)
1108 rmail-current-message)))
1109 ;; If MSG is non-nil, buffer is in RMAIL mode.
1110 (if msg
1111 (progn
1112 ;; Append to an ordinary buffer as a
1113 ;; Unix mail message.
1114 (rmail-maybe-set-message-counters)
1115 (widen)
1116 (narrow-to-region (point-max) (point-max))
1117 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1118 "Date: " (mail-rfc822-date) "\n")
1119 (insert-buffer-substring curbuf beg2 end)
1120 (insert "\n\C-_")
1121 (goto-char (point-min))
1122 (widen)
1123 (search-backward "\n\^_")
1124 (narrow-to-region (point) (point-max))
1125 (rmail-count-new-messages t)
1126 (rmail-show-message msg)
1127 (setq max nil))
1128 ;; Output file not in rmail mode
1129 ;; => just insert at the end.
1130 (narrow-to-region (point-min) (1+ (buffer-size)))
1131 (goto-char (point-max))
1132 (insert-buffer-substring curbuf beg end))
1133 (or buffer-matches-file
1134 (progn
1135 (if (y-or-n-p (format "Save file %s? "
1136 (car fcc-list)))
1137 (save-buffer))
1138 (setq dont-write-the-file t))))
1139 (if max (narrow-to-region (point-min) max))))))
1140 ;; Append to the file directly,
1141 ;; unless we've already taken care of it.
1142 (unless dont-write-the-file
1143 (if (and (file-exists-p (car fcc-list))
1144 ;; Check that the file isn't empty. We don't
1145 ;; want to insert a newline at the start of an
1146 ;; empty file.
1147 (not (zerop (nth 7 (file-attributes (car fcc-list)))))
1148 (mail-file-babyl-p (car fcc-list)))
1149 ;; If the file is a Babyl file,
1150 ;; convert the message to Babyl format.
1151 (let ((coding-system-for-write
1152 (or rmail-file-coding-system
1153 'emacs-mule)))
1154 (save-excursion
1155 (set-buffer (get-buffer-create " mail-temp"))
1156 (setq buffer-read-only nil)
1157 (erase-buffer)
1158 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1159 "Date: " (mail-rfc822-date) "\n")
1160 (insert-buffer-substring curbuf beg2 end)
1161 (insert "\n\C-_")
1162 (write-region (point-min) (point-max) (car fcc-list) t)
1163 (erase-buffer)))
1164 (write-region
1165 (1+ (point-min)) (point-max) (car fcc-list) t)))
1166 (and buffer (not dont-write-the-file)
1167 (with-current-buffer buffer
1168 (set-visited-file-modtime))))
1169 (setq fcc-list (cdr fcc-list))))
1170 (kill-buffer tembuf)))
1171
1172 (defun mail-sent-via ()
1173 "Make a Sent-via header line from each To or CC header line."
1174 (interactive)
1175 (save-excursion
1176 ;; put a marker at the end of the header
1177 (let ((end (copy-marker (mail-header-end)))
1178 (case-fold-search t)
1179 to-line)
1180 (goto-char (point-min))
1181 ;; search for the To: lines and make Sent-via: lines from them
1182 ;; search for the next To: line
1183 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1184 ;; Grab this line plus all its continuations, sans the `to:'.
1185 (let ((to-line
1186 (buffer-substring (point)
1187 (progn
1188 (if (re-search-forward "^[^ \t\n]" end t)
1189 (backward-char 1)
1190 (goto-char end))
1191 (point)))))
1192 ;; Insert a copy, with altered header field name.
1193 (insert-before-markers "Sent-via:" to-line))))))
1194 \f
1195 (defun mail-to ()
1196 "Move point to end of To-field."
1197 (interactive)
1198 (expand-abbrev)
1199 (mail-position-on-field "To"))
1200
1201 (defun mail-subject ()
1202 "Move point to end of Subject-field."
1203 (interactive)
1204 (expand-abbrev)
1205 (mail-position-on-field "Subject"))
1206
1207 (defun mail-cc ()
1208 "Move point to end of CC-field. Create a CC field if none."
1209 (interactive)
1210 (expand-abbrev)
1211 (or (mail-position-on-field "cc" t)
1212 (progn (mail-position-on-field "to")
1213 (insert "\nCC: "))))
1214
1215 (defun mail-bcc ()
1216 "Move point to end of BCC-field. Create a BCC field if none."
1217 (interactive)
1218 (expand-abbrev)
1219 (or (mail-position-on-field "bcc" t)
1220 (progn (mail-position-on-field "to")
1221 (insert "\nBCC: "))))
1222
1223 (defun mail-fcc (folder)
1224 "Add a new FCC field, with file name completion."
1225 (interactive "FFolder carbon copy: ")
1226 (expand-abbrev)
1227 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1228 (mail-position-on-field "to"))
1229 (insert "\nFCC: " folder))
1230
1231 (defun mail-reply-to ()
1232 "Move point to end of Reply-To-field. Create a Reply-To field if none."
1233 (interactive)
1234 (expand-abbrev)
1235 (mail-position-on-field "Reply-To"))
1236
1237 (defun mail-position-on-field (field &optional soft)
1238 (let (end
1239 (case-fold-search t))
1240 (setq end (mail-header-end))
1241 (goto-char (point-min))
1242 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1243 (progn
1244 (re-search-forward "^[^ \t]" nil 'move)
1245 (beginning-of-line)
1246 (skip-chars-backward "\n")
1247 t)
1248 (or soft
1249 (progn (goto-char end)
1250 (insert field ": \n")
1251 (skip-chars-backward "\n")))
1252 nil)))
1253
1254 (defun mail-text ()
1255 "Move point to beginning of message text."
1256 (interactive)
1257 (expand-abbrev)
1258 (goto-char (mail-text-start)))
1259 \f
1260 (defun mail-signature (&optional atpoint)
1261 "Sign letter with contents of the file `mail-signature-file'.
1262 Prefix arg means put contents at point."
1263 (interactive "P")
1264 (save-excursion
1265 (or atpoint
1266 (goto-char (point-max)))
1267 (skip-chars-backward " \t\n")
1268 (end-of-line)
1269 (or atpoint
1270 (delete-region (point) (point-max)))
1271 (insert "\n\n-- \n")
1272 (insert-file-contents (expand-file-name mail-signature-file))))
1273
1274 (defun mail-fill-yanked-message (&optional justifyp)
1275 "Fill the paragraphs of a message yanked into this one.
1276 Numeric argument means justify as well."
1277 (interactive "P")
1278 (save-excursion
1279 (goto-char (mail-text-start))
1280 (fill-individual-paragraphs (point)
1281 (point-max)
1282 justifyp
1283 mail-citation-prefix-regexp)))
1284
1285 (defun mail-indent-citation ()
1286 "Modify text just inserted from a message to be cited.
1287 The inserted text should be the region.
1288 When this function returns, the region is again around the modified text.
1289
1290 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1291 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1292 (mail-yank-clear-headers (region-beginning) (region-end))
1293 (if (null mail-yank-prefix)
1294 (indent-rigidly (region-beginning) (region-end)
1295 mail-indentation-spaces)
1296 (save-excursion
1297 (let ((end (set-marker (make-marker) (region-end))))
1298 (goto-char (region-beginning))
1299 (while (< (point) end)
1300 (insert mail-yank-prefix)
1301 (forward-line 1))))))
1302
1303 (defun mail-yank-original (arg)
1304 "Insert the message being replied to, if any (in rmail).
1305 Puts point after the text and mark before.
1306 Normally, indents each nonblank line ARG spaces (default 3).
1307 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1308
1309 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1310 and don't delete any header fields."
1311 (interactive "P")
1312 (if mail-reply-action
1313 (let ((start (point))
1314 (original mail-reply-action))
1315 (and (consp original) (eq (car original) 'insert-buffer)
1316 (setq original (nth 1 original)))
1317 (if (consp original)
1318 (apply (car original) (cdr original))
1319 ;; If the original message is in another window in the same frame,
1320 ;; delete that window to save screen space.
1321 ;; t means don't alter other frames.
1322 (delete-windows-on original t)
1323 (insert-buffer original)
1324 (set-text-properties (point) (mark t) nil))
1325 (if (consp arg)
1326 nil
1327 (goto-char start)
1328 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1329 mail-indentation-spaces))
1330 ;; Avoid error in Transient Mark mode
1331 ;; on account of mark's being inactive.
1332 (mark-even-if-inactive t))
1333 (cond (mail-citation-hook
1334 ;; Bind mail-citation-header to the inserted
1335 ;; message's header.
1336 (let ((mail-citation-header
1337 (buffer-substring-no-properties
1338 start
1339 (save-excursion
1340 (save-restriction
1341 (narrow-to-region start (point-max))
1342 (goto-char start)
1343 (rfc822-goto-eoh)
1344 (point))))))
1345 (run-hooks 'mail-citation-hook)))
1346 (mail-yank-hooks
1347 (run-hooks 'mail-yank-hooks))
1348 (t
1349 (mail-indent-citation)))))
1350 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1351 ;; It is cleaner to avoid activation, even though the command
1352 ;; loop would deactivate the mark because we inserted text.
1353 (goto-char (prog1 (mark t)
1354 (set-marker (mark-marker) (point) (current-buffer))))
1355 (if (not (eolp)) (insert ?\n)))))
1356
1357 (defun mail-yank-clear-headers (start end)
1358 (if (< end start)
1359 (let (temp)
1360 (setq temp start start end end temp)))
1361 (if mail-yank-ignored-headers
1362 (save-excursion
1363 (goto-char start)
1364 (if (search-forward "\n\n" end t)
1365 (save-restriction
1366 (narrow-to-region start (point))
1367 (goto-char start)
1368 (while (let ((case-fold-search t))
1369 (re-search-forward mail-yank-ignored-headers nil t))
1370 (beginning-of-line)
1371 (delete-region (point)
1372 (progn (re-search-forward "\n[^ \t]")
1373 (forward-char -1)
1374 (point)))))))))
1375
1376 (defun mail-yank-region (arg)
1377 "Insert the selected region from the message being replied to.
1378 Puts point after the text and mark before.
1379 Normally, indents each nonblank line ARG spaces (default 3).
1380 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1381
1382 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1383 and don't delete any header fields."
1384 (interactive "P")
1385 (and (consp mail-reply-action)
1386 (eq (car mail-reply-action) 'insert-buffer)
1387 (with-current-buffer (nth 1 mail-reply-action)
1388 (or (mark t)
1389 (error "No mark set: %S" (current-buffer))))
1390 (let ((buffer (nth 1 mail-reply-action))
1391 (start (point))
1392 ;; Avoid error in Transient Mark mode
1393 ;; on account of mark's being inactive.
1394 (mark-even-if-inactive t))
1395 ;; Insert the citation text.
1396 (insert (with-current-buffer buffer
1397 (buffer-substring-no-properties (point) (mark))))
1398 (push-mark start)
1399 ;; Indent or otherwise annotate the citation text.
1400 (if (consp arg)
1401 nil
1402 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1403 mail-indentation-spaces)))
1404 (if mail-citation-hook
1405 ;; Bind mail-citation-hook to the original message's header.
1406 (let ((mail-citation-header
1407 (with-current-buffer buffer
1408 (buffer-substring-no-properties
1409 (point-min)
1410 (save-excursion
1411 (goto-char (point-min))
1412 (rfc822-goto-eoh)
1413 (point))))))
1414 (run-hooks 'mail-citation-hook))
1415 (if mail-yank-hooks
1416 (run-hooks 'mail-yank-hooks)
1417 (mail-indent-citation))))))))
1418 \f
1419 (defun mail-attach-file (&optional file)
1420 "Insert a file at the end of the buffer, with separator lines around it."
1421 (interactive "fAttach file: ")
1422 (save-excursion
1423 (goto-char (point-max))
1424 (or (bolp) (newline))
1425 (newline)
1426 (let ((start (point))
1427 middle)
1428 (insert (format "===File %s===" file))
1429 (insert-char ?= (max 0 (- 60 (current-column))))
1430 (newline)
1431 (setq middle (point))
1432 (insert "============================================================\n")
1433 (push-mark)
1434 (goto-char middle)
1435 (insert-file-contents file)
1436 (or (bolp) (newline))
1437 (goto-char start))))
1438 \f
1439 ;; Put these commands last, to reduce chance of lossage from quitting
1440 ;; in middle of loading the file.
1441
1442 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1443
1444 ;;;###autoload
1445 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1446 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1447 When this function returns, the buffer `*mail*' is selected.
1448 The value is t if the message was newly initialized; otherwise, nil.
1449
1450 Optionally, the signature file `mail-signature-file' can be inserted at the
1451 end; see the variable `mail-signature'.
1452
1453 \\<mail-mode-map>
1454 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1455
1456 Various special commands starting with C-c are available in sendmail mode
1457 to move to message header fields:
1458 \\{mail-mode-map}
1459
1460 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1461 when the message is initialized.
1462
1463 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1464 a Reply-to: field with that address is inserted.
1465
1466 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1467 is inserted.
1468
1469 The normal hook `mail-setup-hook' is run after the message is
1470 initialized. It can add more default fields to the message.
1471
1472 When calling from a program, the first argument if non-nil says
1473 not to erase the existing contents of the `*mail*' buffer.
1474
1475 The second through fifth arguments,
1476 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1477 the initial contents of those header fields.
1478 These arguments should not have final newlines.
1479 The sixth argument REPLYBUFFER is a buffer which contains an
1480 original message being replied to, or else an action
1481 of the form (FUNCTION . ARGS) which says how to insert the original.
1482 Or it can be nil, if not replying to anything.
1483 The seventh argument ACTIONS is a list of actions to take
1484 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1485 when the message is sent, we apply FUNCTION to ARGS.
1486 This is how Rmail arranges to mark messages `answered'."
1487 (interactive "P")
1488 ;;; This is commented out because I found it was confusing in practice.
1489 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1490 ;;; if you want to have multiple mail buffers.
1491 ;;; And then you can control which messages to save. --rms.
1492 ;;; (let ((index 1)
1493 ;;; buffer)
1494 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
1495 ;;; (if noerase
1496 ;;; (progn
1497 ;;; (while (and (setq buffer
1498 ;;; (get-buffer (if (= 1 index) "*mail*"
1499 ;;; (format "*mail*<%d>" index))))
1500 ;;; (not (buffer-modified-p buffer)))
1501 ;;; (setq index (1+ index)))
1502 ;;; (if buffer (switch-to-buffer buffer)
1503 ;;; ;; If none exists, start a new message.
1504 ;;; ;; This will never re-use an existing unmodified mail buffer
1505 ;;; ;; (since index is not 1 anymore). Perhaps it should.
1506 ;;; (setq noerase nil))))
1507 ;;; ;; Unless we found a modified message and are happy, start a new message.
1508 ;;; (if (not noerase)
1509 ;;; (progn
1510 ;;; ;; Look for existing unmodified mail buffer.
1511 ;;; (while (and (setq buffer
1512 ;;; (get-buffer (if (= 1 index) "*mail*"
1513 ;;; (format "*mail*<%d>" index))))
1514 ;;; (buffer-modified-p buffer))
1515 ;;; (setq index (1+ index)))
1516 ;;; ;; If none, make a new one.
1517 ;;; (or buffer
1518 ;;; (setq buffer (generate-new-buffer "*mail*")))
1519 ;;; ;; Go there and initialize it.
1520 ;;; (switch-to-buffer buffer)
1521 ;;; (erase-buffer)
1522 ;;; (setq default-directory (expand-file-name "~/"))
1523 ;;; (auto-save-mode auto-save-default)
1524 ;;; (mail-mode)
1525 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1526 ;;; (if (and buffer-auto-save-file-name
1527 ;;; (file-exists-p buffer-auto-save-file-name))
1528 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1529 ;;; t))
1530 (pop-to-buffer "*mail*")
1531 ;; Avoid danger that the auto-save file can't be written.
1532 (let ((dir (expand-file-name
1533 (file-name-as-directory mail-default-directory))))
1534 (if (file-exists-p dir)
1535 (setq default-directory dir)))
1536 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1537 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1538 (and (not auto-save-default) buffer-auto-save-file-name))
1539 (auto-save-mode auto-save-default))
1540 (mail-mode)
1541 ;; Disconnect the buffer from its visited file
1542 ;; (in case the user has actually visited a file *mail*).
1543 ; (set-visited-file-name nil)
1544 (let (initialized)
1545 (and (not noerase)
1546 (if buffer-file-name
1547 (if (buffer-modified-p)
1548 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1549 (if (y-or-n-p "Disconnect buffer from visited file? ")
1550 (set-visited-file-name nil))
1551 t)
1552 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1553 (set-visited-file-name nil)
1554 t))
1555 ;; A non-file-visiting buffer.
1556 (if (buffer-modified-p)
1557 (y-or-n-p "Unsent message being composed; erase it? ")
1558 t))
1559 (let ((inhibit-read-only t))
1560 (erase-buffer)
1561 (mail-setup to subject in-reply-to cc replybuffer actions)
1562 (setq initialized t)))
1563 (if (and buffer-auto-save-file-name
1564 (file-exists-p buffer-auto-save-file-name))
1565 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1566 initialized))
1567
1568 (defun mail-recover-1 ()
1569 "Pop up a list of auto-saved draft messages so you can recover one of them."
1570 (interactive)
1571 (let ((file-name (make-auto-save-file-name))
1572 (ls-lisp-support-shell-wildcards t)
1573 non-random-len wildcard)
1574 ;; Remove the random part from the auto-save-file-name, and
1575 ;; create a wildcard which matches possible candidates.
1576 ;; Note: this knows that make-auto-save-file-name appends
1577 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1578 ;; is the result of (make-temp-name "").
1579 (setq non-random-len
1580 (- (length file-name) (length (make-temp-name "")) 1))
1581 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1582 (if (null (file-expand-wildcards wildcard))
1583 (message "There are no auto-saved drafts to recover")
1584 ;; Bind dired-trivial-filenames to t because all auto-save file
1585 ;; names are normally ``trivial'', so Dired will set point after
1586 ;; all the files, at buffer bottom. We want it on the first
1587 ;; file instead.
1588 (let ((dired-trivial-filenames t))
1589 (dired-other-window wildcard (concat dired-listing-switches "t")))
1590 (rename-buffer "*Auto-saved Drafts*" t)
1591 (save-excursion
1592 (goto-char (point-min))
1593 (or (looking-at " Move to the draft file you want to recover,")
1594 (let ((inhibit-read-only t))
1595 ;; Each line starts with a space so that Font Lock mode
1596 ;; won't highlight the first character.
1597 (insert "\
1598 Move to the draft file you want to recover, then type C-c C-c
1599 to recover text of message whose composition was interrupted.
1600 To browse text of a draft, type v on the draft file's line.
1601
1602 You can also delete some of these files;
1603 type d on a line to mark that file for deletion.
1604
1605 List of possible auto-save files for recovery:
1606
1607 "))))
1608 (use-local-map
1609 (let ((map (make-sparse-keymap)))
1610 (set-keymap-parent map (current-local-map))
1611 map))
1612 (define-key (current-local-map) "v"
1613 (lambda ()
1614 (interactive)
1615 (let ((coding-system-for-read 'emacs-mule-unix))
1616 (dired-view-file))))
1617 (define-key (current-local-map) "\C-c\C-c"
1618 (lambda ()
1619 (interactive)
1620 (let ((fname (dired-get-filename))
1621 ;; Auto-saved files are written in the internal
1622 ;; representation, so they should be read accordingly.
1623 (coding-system-for-read 'emacs-mule-unix))
1624 (switch-to-buffer-other-window "*mail*")
1625 (let ((buffer-read-only nil))
1626 (erase-buffer)
1627 (insert-file-contents fname nil)
1628 ;; insert-file-contents will set buffer-file-coding-system
1629 ;; to emacs-mule, which is probably not what they want to
1630 ;; use for sending the message. But we don't know what
1631 ;; was its value before the buffer was killed or Emacs
1632 ;; crashed. We therefore reset buffer-file-coding-system
1633 ;; to the default value, so that either the default does
1634 ;; TRT, or the user will get prompted for the right
1635 ;; encoding when they send the message.
1636 (setq buffer-file-coding-system
1637 default-buffer-file-coding-system))))))))
1638
1639 (defun mail-recover ()
1640 "Recover interrupted mail composition from auto-save files.
1641
1642 If the mail buffer has a current valid auto-save file,
1643 the command recovers that file. Otherwise, it displays a
1644 buffer showing the existing auto-saved draft messages;
1645 you can move to one of them and type C-c C-c to recover that one."
1646 (interactive)
1647 ;; In case they invoke us from some random buffer...
1648 (switch-to-buffer "*mail*")
1649 ;; If *mail* didn't exist, set its directory, so that auto-saved
1650 ;; drafts will be found.
1651 (let ((dir (expand-file-name
1652 (file-name-as-directory mail-default-directory))))
1653 (if (file-exists-p dir)
1654 (setq default-directory dir)))
1655 (or (eq major-mode 'mail-mode)
1656 (mail-mode))
1657 (let ((file-name buffer-auto-save-file-name))
1658 (cond ((and file-name (file-exists-p file-name))
1659 (let ((dispbuf
1660 ;; This used to invoke `ls' via call-process, but
1661 ;; dired-noselect is more portable to systems where
1662 ;; `ls' is not a standard program (it will use
1663 ;; ls-lisp instead).
1664 (dired-noselect file-name
1665 (concat dired-listing-switches "t"))))
1666 (save-selected-window
1667 (select-window (display-buffer dispbuf t))
1668 (goto-char (point-min))
1669 (forward-line 2)
1670 (dired-move-to-filename)
1671 (setq dispbuf (rename-buffer "*Directory*" t)))
1672 (if (not (yes-or-no-p
1673 (format "Recover mail draft from auto save file %s? "
1674 file-name)))
1675 (error "mail-recover cancelled")
1676 (let ((buffer-read-only nil)
1677 (buffer-coding buffer-file-coding-system)
1678 ;; Auto-save files are written in internal
1679 ;; representation of non-ASCII characters.
1680 (coding-system-for-read 'emacs-mule-unix))
1681 (erase-buffer)
1682 (insert-file-contents file-name nil)
1683 (setq buffer-file-coding-system buffer-coding)))))
1684 (t (mail-recover-1)))))
1685
1686 ;;;###autoload
1687 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1688 "Like `mail' command, but display mail buffer in another window."
1689 (interactive "P")
1690 (let ((pop-up-windows t)
1691 (special-display-buffer-names nil)
1692 (special-display-regexps nil)
1693 (same-window-buffer-names nil)
1694 (same-window-regexps nil))
1695 (pop-to-buffer "*mail*"))
1696 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1697
1698 ;;;###autoload
1699 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1700 "Like `mail' command, but display mail buffer in another frame."
1701 (interactive "P")
1702 (let ((pop-up-frames t)
1703 (special-display-buffer-names nil)
1704 (special-display-regexps nil)
1705 (same-window-buffer-names nil)
1706 (same-window-regexps nil))
1707 (pop-to-buffer "*mail*"))
1708 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1709
1710 ;;; Do not add anything but external entries on this page.
1711
1712 (provide 'sendmail)
1713
1714 ;;; sendmail.el ends here