* admin/gitmerge.el (gitmerge-skip-regexp): Be stricter in batch mode.
[emacs.git] / lisp / mail / sendmail.el
blobcd8021152760a543abd14749433bbf55fe72ab43
1 ;;; sendmail.el --- mail sending commands for Emacs
3 ;; Copyright (C) 1985-1986, 1992-1996, 1998, 2000-2017 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This mode provides mail-sending facilities from within Emacs. It is
27 ;; documented in the Emacs user's manual.
29 ;;; Code:
30 (require 'mail-utils)
31 (require 'rfc2047)
32 (autoload 'message-make-date "message")
34 (defgroup sendmail nil
35 "Mail sending commands for Emacs."
36 :prefix "mail-"
37 :group 'mail)
39 (defcustom mail-setup-with-from t
40 "Non-nil means insert `From:' field when setting up the message."
41 :type 'boolean
42 :group 'sendmail
43 :version "22.1")
45 (defcustom sendmail-program
46 (or (executable-find "sendmail")
47 (cond
48 ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
49 ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
50 ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
51 (t "sendmail")))
52 "Program used to send messages."
53 :version "24.1" ; add executable-find, remove fakemail
54 :group 'mail
55 :type 'file)
57 ;;;###autoload
58 (defcustom mail-from-style 'default
59 "Specifies how \"From:\" fields look.
61 If nil, they contain just the return address like:
62 king@grassland.com
63 If `parens', they look like:
64 king@grassland.com (Elvis Parsley)
65 If `angles', they look like:
66 Elvis Parsley <king@grassland.com>
68 Otherwise, most addresses look like `angles', but they look like
69 `parens' if `angles' would need quoting and `parens' would not."
70 ;; The value `system-default' is now deprecated.
71 :type '(choice (const :tag "simple" nil)
72 (const parens)
73 (const angles)
74 (const default))
75 :version "20.3"
76 :group 'sendmail)
78 ;;;###autoload
79 (defcustom mail-specify-envelope-from nil
80 "If non-nil, specify the envelope-from address when sending mail.
81 The value used to specify it is whatever is found in
82 the variable `mail-envelope-from', with `user-mail-address' as fallback.
84 On most systems, specifying the envelope-from address is a
85 privileged operation. This variable affects sendmail and
86 smtpmail -- if you use feedmail to send mail, see instead the
87 variable `feedmail-deduce-envelope-from'."
88 :version "21.1"
89 :type 'boolean
90 :group 'sendmail)
92 (defcustom mail-envelope-from nil
93 "If non-nil, designate the envelope-from address when sending mail.
94 This only has an effect if `mail-specify-envelope-from' is non-nil.
95 The value should be either a string, or the symbol `header' (in
96 which case the contents of the \"From\" header of the message
97 being sent is used), or nil (in which case the value of
98 `user-mail-address' is used)."
99 :version "21.1"
100 :type '(choice (string :tag "From-name")
101 (const :tag "Use From: header from message" header)
102 (const :tag "Use `user-mail-address'" nil))
103 :group 'sendmail)
105 ;;;###autoload
106 (defcustom mail-self-blind nil
107 "Non-nil means insert BCC to self in messages to be sent.
108 This is done when the message is initialized,
109 so you can remove or alter the BCC field to override the default."
110 :type 'boolean
111 :group 'sendmail)
113 ;;;###autoload
114 (defcustom mail-interactive t
115 ;; We used to use a default of nil rather than t, but nowadays it is very
116 ;; common for sendmail to be misconfigured, so one cannot rely on the
117 ;; bounce message to be delivered anywhere, least of all to the
118 ;; user's mailbox.
119 "Non-nil means when sending a message wait for and display errors.
120 Otherwise, let mailer send back a message to report errors."
121 :type 'boolean
122 :version "23.1" ; changed from nil to t
123 :group 'sendmail)
125 (defcustom mail-yank-ignored-headers
126 (concat "^"
127 (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
128 "received" "message-id" "summary-line" "to" "subject"
129 "in-reply-to" "return-path" "mail-reply-to"
130 ;; Should really be rmail-attribute-header and
131 ;; rmail-keyword-header, but this file does not
132 ;; require rmail (at run time).
133 "x-rmail-attributes" "x-rmail-keywords"
134 "mail-followup-to") "\\(?:")
135 ":")
136 "Delete these headers from old message when it's inserted in a reply."
137 :type 'regexp
138 :group 'sendmail
139 :version "23.1")
141 ;; Useful to set in site-init.el
142 ;;;###autoload
143 (defcustom send-mail-function
144 ;; Assume smtpmail is the preferred choice if it's already configured.
145 (if (and (boundp 'smtpmail-smtp-server)
146 smtpmail-smtp-server)
147 'smtpmail-send-it 'sendmail-query-once)
148 "Function to call to send the current buffer as mail.
149 The headers should be delimited by a line which is
150 not a valid RFC822 header or continuation line,
151 that matches the variable `mail-header-separator'.
152 This is used by the default mail-sending commands. See also
153 `message-send-mail-function' for use with the Message package."
154 :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
155 (function-item sendmail-query-once :tag "Query the user")
156 (function-item smtpmail-send-it :tag "Use SMTPmail package")
157 (function-item feedmail-send-it :tag "Use Feedmail package")
158 (function-item mailclient-send-it :tag "Use Mailclient package")
159 function)
160 :version "24.1"
161 :group 'sendmail)
163 ;;;###autoload
164 (defcustom mail-header-separator (purecopy "--text follows this line--")
165 "Line used to separate headers from text in messages being composed."
166 :type 'string
167 :group 'sendmail)
169 ;; Set up mail-header-separator for use as a category text property.
170 (put 'mail-header-separator 'rear-nonsticky '(category))
171 ;; This was a nice idea, for preventing accidental modification of
172 ;; the separator. But I found it also prevented or obstructed
173 ;; certain deliberate operations, such as copying the separator line
174 ;; up to the top to send myself a copy of an already sent outgoing message
175 ;; and other things. So I turned it off. --rms.
176 ;;(put 'mail-header-separator 'read-only t)
178 ;;;###autoload
179 (defcustom mail-archive-file-name nil
180 "Name of file to write all outgoing messages in, or nil for none.
181 This is normally an mbox file, but for backwards compatibility may also
182 be a Babyl file."
183 :type '(choice file (const nil))
184 :group 'sendmail)
186 ;;;###autoload
187 (defcustom mail-default-reply-to nil
188 "Address to insert as default Reply-to field of outgoing messages.
189 If nil, it will be initialized from the REPLYTO environment variable
190 when you first send mail."
191 :type '(choice (const nil) string)
192 :group 'sendmail)
194 (defcustom mail-alias-file nil
195 "If non-nil, the name of a file to use instead of the sendmail default.
196 This file defines aliases to be expanded by the mailer; this is a different
197 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
198 This variable has no effect unless your system uses sendmail as its mailer.
199 The default file is defined in sendmail's configuration file, e.g.
200 `/etc/aliases'."
201 :type '(choice (const :tag "Sendmail default" nil) file)
202 :group 'sendmail)
204 ;;;###autoload
205 (defcustom mail-personal-alias-file (purecopy "~/.mailrc")
206 "If non-nil, the name of the user's personal mail alias file.
207 This file typically should be in same format as the `.mailrc' file used by
208 the `Mail' or `mailx' program.
209 This file need not actually exist."
210 :type '(choice (const nil) file)
211 :group 'sendmail)
213 ;;;###autoload
214 (defcustom mail-setup-hook nil
215 "Normal hook, run each time a new outgoing message is initialized."
216 :type 'hook
217 :options '(fortune-to-signature spook mail-abbrevs-setup)
218 :group 'sendmail)
220 ;;;###autoload
221 (defvar mail-aliases t
222 "Alist of mail address aliases,
223 or t meaning should be initialized from your mail aliases file.
224 \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
225 can specify a different file name.)
226 The alias definitions in the file have this form:
227 alias ALIAS MEANING")
229 (defvar mail-alias-modtime nil
230 "The modification time of your mail alias file when it was last examined.")
232 ;;;###autoload
233 (defcustom mail-yank-prefix "> "
234 "Prefix insert on lines of yanked message being replied to.
235 If this is nil, use indentation, as specified by `mail-indentation-spaces'."
236 :type '(choice (const nil) string)
237 :group 'sendmail)
239 ;;;###autoload
240 (defcustom mail-indentation-spaces 3
241 "Number of spaces to insert at the beginning of each cited line.
242 Used by `mail-yank-original' via `mail-indent-citation'."
243 :type 'integer
244 :group 'sendmail)
246 ;;;###autoload
247 (defcustom mail-citation-hook nil
248 "Hook for modifying a citation just inserted in the mail buffer.
249 Each hook function can find the citation between (point) and (mark t),
250 and should leave point and mark around the citation text as modified.
251 The hook functions can find the header of the cited message
252 in the variable `mail-citation-header', whether or not this is included
253 in the cited portion of the message.
255 If this hook is entirely empty (nil), a default action is taken
256 instead of no action."
257 :type 'hook
258 :group 'sendmail)
260 (defvar mail-citation-header nil
261 "While running `mail-citation-hook', this variable holds the message header.
262 This enables the hook functions to see the whole message header
263 regardless of what part of it (if any) is included in the cited text.")
265 ;;;###autoload
266 (defcustom mail-citation-prefix-regexp
267 (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
268 "Regular expression to match a citation prefix plus whitespace.
269 It should match whatever sort of citation prefixes you want to handle,
270 with whitespace before and after; it should also match just whitespace.
271 The default value matches citations like `foo-bar>' plus whitespace."
272 :type 'regexp
273 :group 'sendmail
274 :version "24.1")
276 (defvar mail-abbrevs-loaded nil)
277 (defvar mail-mode-map
278 (let ((map (make-sparse-keymap)))
279 (define-key map "\M-\t" 'completion-at-point)
280 (define-key map "\C-c?" 'describe-mode)
281 (define-key map "\C-c\C-f\C-t" 'mail-to)
282 (define-key map "\C-c\C-f\C-b" 'mail-bcc)
283 (define-key map "\C-c\C-f\C-f" 'mail-fcc)
284 (define-key map "\C-c\C-f\C-c" 'mail-cc)
285 (define-key map "\C-c\C-f\C-s" 'mail-subject)
286 (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
287 (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to) ; author
288 (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
289 (define-key map "\C-c\C-t" 'mail-text)
290 (define-key map "\C-c\C-y" 'mail-yank-original)
291 (define-key map "\C-c\C-r" 'mail-yank-region)
292 (define-key map [remap split-line] 'mail-split-line)
293 (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
294 (define-key map "\C-c\C-w" 'mail-signature)
295 (define-key map "\C-c\C-c" 'mail-send-and-exit)
296 (define-key map "\C-c\C-s" 'mail-send)
297 (define-key map "\C-c\C-i" 'mail-insert-file)
298 ;; FIXME add this? "b" = bury buffer. It's in the menu-bar.
299 ;;; (define-key map "\C-c\C-b" 'mail-dont-send)
301 (define-key map [menu-bar mail]
302 (cons "Mail" (make-sparse-keymap "Mail")))
304 (define-key map [menu-bar mail attachment]
305 '("Attach File" . mail-add-attachment))
307 (define-key map [menu-bar mail fill]
308 '("Fill Citation" . mail-fill-yanked-message))
310 (define-key map [menu-bar mail yank]
311 '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
313 (define-key map [menu-bar mail signature]
314 '("Insert Signature" . mail-signature))
316 (define-key map [menu-bar mail mail-sep]
317 '("--"))
319 (define-key map [menu-bar mail cancel]
320 '("Cancel" . mail-dont-send))
322 (define-key map [menu-bar mail send-stay]
323 '("Send, Keep Editing" . mail-send))
325 (define-key map [menu-bar mail send]
326 '("Send Message" . mail-send-and-exit))
328 (define-key map [menu-bar headers]
329 (cons "Headers" (make-sparse-keymap "Move to Header")))
331 (define-key map [menu-bar headers text]
332 '("Text" . mail-text))
334 (define-key map [menu-bar headers expand-aliases]
335 '("Expand Aliases" . expand-mail-aliases))
337 (define-key map [menu-bar headers mail-reply-to]
338 '("Mail-Reply-To" . mail-mail-reply-to))
340 (define-key map [menu-bar headers mail-followup-to]
341 '("Mail-Followup-To" . mail-mail-followup-to))
343 (define-key map [menu-bar headers reply-to]
344 '("Reply-To" . mail-reply-to))
346 (define-key map [menu-bar headers bcc]
347 '("Bcc" . mail-bcc))
349 (define-key map [menu-bar headers fcc]
350 '("Fcc" . mail-fcc))
352 (define-key map [menu-bar headers cc]
353 '("Cc" . mail-cc))
355 (define-key map [menu-bar headers subject]
356 '("Subject" . mail-subject))
358 (define-key map [menu-bar headers to]
359 '("To" . mail-to))
361 map))
363 (autoload 'build-mail-aliases "mailalias"
364 "Read mail aliases from personal aliases file and set `mail-aliases'.
365 By default, this is the file specified by `mail-personal-alias-file'." t)
367 ;;;###autoload
368 (defcustom mail-signature t
369 "Text inserted at end of mail buffer when a message is initialized.
370 If t, it means to insert the contents of the file `mail-signature-file'.
371 If a string, that string is inserted.
372 (To make a proper signature, the string should begin with \\n\\n-- \\n,
373 which is the standard way to delimit a signature in a message.)
374 Otherwise, it should be an expression; it is evaluated
375 and should insert whatever you want to insert."
376 :type '(choice (const :tag "None" nil)
377 (const :tag "Use `.signature' file" t)
378 (string :tag "String to insert")
379 (sexp :tag "Expression to evaluate"))
380 :group 'sendmail)
381 (put 'mail-signature 'risky-local-variable t)
383 ;;;###autoload
384 (defcustom mail-signature-file (purecopy "~/.signature")
385 "File containing the text inserted at end of mail buffer."
386 :type 'file
387 :group 'sendmail)
389 ;;;###autoload
390 (defcustom mail-default-directory (purecopy "~/")
391 "Value of `default-directory' for Mail mode buffers.
392 This directory is used for auto-save files of Mail mode buffers.
394 Note that Message mode does not use this variable; it auto-saves
395 in `message-auto-save-directory'."
396 :type '(directory :tag "Directory")
397 :group 'sendmail
398 :version "22.1")
400 (defvar mail-reply-action nil)
401 (defvar mail-send-actions nil
402 "A list of actions to be performed upon successful sending of a message.")
403 (defvar mail-return-action nil)
405 ;;;###autoload
406 (defcustom mail-default-headers nil
407 "A string containing header lines, to be inserted in outgoing messages.
408 It can contain newlines, and should end in one. It is inserted
409 before you edit the message, so you can edit or delete the lines."
410 :type '(choice (const nil) string)
411 :group 'sendmail)
413 (defcustom mail-bury-selects-summary t
414 "If non-nil, try to show Rmail summary buffer after returning from mail.
415 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
416 the Rmail summary buffer before returning, if it exists and this variable
417 is non-nil."
418 :type 'boolean
419 :group 'sendmail)
421 (defcustom mail-send-nonascii 'mime
422 "Specify whether to allow sending non-ASCII characters in mail.
423 If t, that means do allow it. nil means don't allow it.
424 `query' means ask the user each time.
425 `mime' means add an appropriate MIME header if none already present.
426 The default is `mime'.
427 Including non-ASCII characters in a mail message can be problematical
428 for the recipient, who may not know how to decode them properly."
429 :type '(choice (const t) (const nil) (const query) (const mime))
430 :group 'sendmail)
432 (defcustom mail-use-dsn nil
433 "Ask MTA for notification of failed, delayed or successful delivery.
434 Note that only some MTAs (currently only recent versions of Sendmail)
435 support Delivery Status Notification."
436 :group 'sendmail
437 :type '(repeat (radio (const :tag "Failure" failure)
438 (const :tag "Delay" delay)
439 (const :tag "Success" success)))
440 :version "22.1")
442 ;; Note: could use /usr/ucb/mail instead of sendmail;
443 ;; options -t, and -v if not interactive.
444 (defvar mail-mailer-swallows-blank-line nil
445 "Set this non-nil if the system's mailer runs the header and body together.
446 The actual value should be an expression to evaluate that returns
447 non-nil if the problem will actually occur.
448 \(As far as we know, this is not an issue on any system still supported
449 by Emacs.)")
451 (put 'mail-mailer-swallows-blank-line 'risky-local-variable t) ; gets evalled
452 (make-obsolete-variable 'mail-mailer-swallows-blank-line
453 "no need to set this on any modern system."
454 "24.1" 'set)
456 (defvar mail-mode-syntax-table
457 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
458 (let ((st (make-syntax-table)))
459 ;; FIXME this is probably very obsolete now ("percent hack").
460 ;; sending.texi used to say:
461 ;; Mail mode defines the character `%' as a word separator; this
462 ;; is helpful for using the word commands to edit mail addresses.
463 (modify-syntax-entry ?% ". " st)
465 "Syntax table used while in `mail-mode'.")
467 (defvar mail-font-lock-keywords
468 (eval-when-compile
469 (let* ((cite-chars "[>|}]")
470 (cite-prefix "[:alpha:]")
471 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
472 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
473 '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
474 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
475 (1 font-lock-comment-face)
476 ;; (2 font-lock-type-face nil t)
478 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
479 '(eval .
480 (let ((separator (if (zerop (length mail-header-separator))
481 " \\`\\' "
482 (regexp-quote mail-header-separator))))
483 (cons (concat "^" separator "$") 'font-lock-warning-face)))
484 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
485 `(,cite-chars
486 (,(concat "\\=[ \t]*"
487 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
488 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
489 "\\(.*\\)")
490 (beginning-of-line) (end-of-line)
491 (1 font-lock-comment-delimiter-face nil t)
492 (5 font-lock-comment-face nil t)))
493 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
494 . font-lock-string-face))))
495 "Additional expressions to highlight in Mail mode.")
498 ;;;###autoload
499 (defun sendmail-query-once ()
500 "Query for `send-mail-function' and send mail with it.
501 This also saves the value of `send-mail-function' via Customize."
502 ;; If send-mail-function is already setup, we're incorrectly called
503 ;; a second time, probably because someone's using an old value
504 ;; of send-mail-function.
505 (when (eq send-mail-function 'sendmail-query-once)
506 (sendmail-query-user-about-smtp))
507 (funcall send-mail-function))
509 (defun sendmail-query-user-about-smtp ()
510 (let* ((options `(("mail client" . mailclient-send-it)
511 ,@(when (and sendmail-program
512 (executable-find sendmail-program))
513 '(("transport" . sendmail-send-it)))
514 ("smtp" . smtpmail-send-it)))
515 (choice
516 ;; Query the user.
517 (with-temp-buffer
518 (rename-buffer "*Emacs Mail Setup Help*" t)
519 (insert (substitute-command-keys "\
520 Emacs is about to send an email message, but it has not been
521 configured for sending email. To tell Emacs how to send email:
523 - Type `")
524 (propertize "mail client" 'face 'bold)
525 (substitute-command-keys "\
526 ' to start your default email client and
527 pass it the message text.\n\n"))
528 (and sendmail-program
529 (executable-find sendmail-program)
530 (insert (substitute-command-keys "\
531 - Type `")
532 (propertize "transport" 'face 'bold)
533 (substitute-command-keys "\
534 ' to invoke the system's mail transport agent
535 (the `")
536 sendmail-program
537 (substitute-command-keys "' program).\n\n")))
538 (insert (substitute-command-keys "\
539 - Type `")
540 (propertize "smtp" 'face 'bold)
541 (substitute-command-keys "' to send mail directly to an \"outgoing mail\" server.
542 (Emacs may prompt you for SMTP settings).
544 Emacs will record your selection and will use it thereafter.
545 To change it later, customize the option `send-mail-function'.\n"))
546 (goto-char (point-min))
547 (display-buffer (current-buffer))
548 (let ((completion-ignore-case t))
549 (completing-read
550 (format "Send mail via (default %s): " (caar options))
551 options nil 'require-match nil nil (car options))))))
552 (customize-save-variable 'send-mail-function
553 (cdr (assoc-string choice options t)))))
555 (defun sendmail-sync-aliases ()
556 (when mail-personal-alias-file
557 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
558 (or (equal mail-alias-modtime modtime)
559 (setq mail-alias-modtime modtime
560 mail-aliases t)))))
563 ;;;###autoload
564 (define-mail-user-agent 'sendmail-user-agent
565 'sendmail-user-agent-compose
566 'mail-send-and-exit)
568 ;;;###autoload
569 (defun sendmail-user-agent-compose (&optional to subject other-headers
570 continue switch-function yank-action
571 send-actions return-action
572 &rest ignored)
573 (if switch-function
574 (funcall switch-function "*mail*"))
575 (let ((cc (cdr (assoc-string "cc" other-headers t)))
576 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
577 (body (cdr (assoc-string "body" other-headers t))))
578 (or (mail continue to subject in-reply-to cc yank-action
579 send-actions return-action)
580 continue
581 (error "Message aborted"))
582 (save-excursion
583 (rfc822-goto-eoh)
584 (while other-headers
585 (unless (member-ignore-case (car (car other-headers))
586 '("in-reply-to" "cc" "body"))
587 (insert (car (car other-headers)) ": "
588 (cdr (car other-headers))
589 (if use-hard-newlines hard-newline "\n")))
590 (setq other-headers (cdr other-headers)))
591 (when body
592 (forward-line 1)
593 (insert body))
594 t)))
596 (defun mail-setup (to subject in-reply-to cc replybuffer
597 actions return-action)
598 (or mail-default-reply-to
599 (setq mail-default-reply-to (getenv "REPLYTO")))
600 (sendmail-sync-aliases)
601 (when (eq mail-aliases t)
602 (setq mail-aliases nil)
603 (and mail-personal-alias-file
604 (file-exists-p mail-personal-alias-file)
605 (build-mail-aliases)))
606 ;; Don't leave this around from a previous message.
607 (kill-local-variable 'buffer-file-coding-system)
608 ;; This doesn't work for enable-multibyte-characters.
609 ;; (kill-local-variable 'enable-multibyte-characters)
610 (set-buffer-multibyte (default-value 'enable-multibyte-characters))
611 (if current-input-method
612 (deactivate-input-method))
614 ;; Local variables for Mail mode.
615 (setq mail-send-actions actions)
616 (setq mail-reply-action replybuffer)
617 (setq mail-return-action return-action)
619 (goto-char (point-min))
620 (if mail-setup-with-from
621 (mail-insert-from-field))
622 (insert "To: ")
623 (save-excursion
624 (if to
625 ;; Here removed code to extract names from within <...>
626 ;; on the assumption that mail-strip-quoted-names
627 ;; has been called and has done so.
628 (let ((fill-prefix "\t")
629 (address-start (point)))
630 (insert to "\n")
631 (fill-region-as-paragraph address-start (point-max))
632 (goto-char (point-max))
633 (unless (bolp)
634 (newline)))
635 (newline))
636 (if cc
637 (let ((fill-prefix "\t")
638 (address-start (progn (insert "CC: ") (point))))
639 (insert cc "\n")
640 (fill-region-as-paragraph address-start (point-max))
641 (goto-char (point-max))
642 (unless (bolp)
643 (newline))))
644 (if in-reply-to
645 (let ((fill-prefix "\t")
646 (fill-column 78)
647 (address-start (point)))
648 (insert "In-reply-to: " in-reply-to "\n")
649 (fill-region-as-paragraph address-start (point-max))
650 (goto-char (point-max))
651 (unless (bolp)
652 (newline))))
653 (insert "Subject: " (or subject "") "\n")
654 (if mail-default-headers
655 (insert mail-default-headers))
656 (if mail-default-reply-to
657 (insert "Reply-to: " mail-default-reply-to "\n"))
658 (if mail-self-blind
659 (insert "BCC: " user-mail-address "\n"))
660 (if mail-archive-file-name
661 (insert "FCC: " mail-archive-file-name "\n"))
662 (put-text-property (point)
663 (progn
664 (insert mail-header-separator "\n")
665 (1- (point)))
666 'category 'mail-header-separator)
667 ;; Insert the signature. But remember the beginning of the message.
668 (if to (setq to (point)))
669 (if mail-signature (mail-signature t))
670 (goto-char (point-max))
671 (or (bolp) (newline)))
672 (if to (goto-char to))
673 (or to subject in-reply-to
674 (set-buffer-modified-p nil))
675 (run-hooks 'mail-setup-hook))
677 (defcustom mail-mode-hook nil
678 "Hook run by Mail mode.
679 When composing a mail, this runs immediately after creating, or
680 switching to, the `*mail*' buffer. See also `mail-setup-hook'."
681 :group 'sendmail
682 :type 'hook
683 :options '(footnote-mode))
685 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
686 (defvar mail-encode-mml)
687 ;;;###autoload
688 (define-derived-mode mail-mode text-mode "Mail"
689 "Major mode for editing mail to be sent.
690 Like Text Mode but with these additional commands:
692 \\[mail-send] mail-send (send the message)
693 \\[mail-send-and-exit] mail-send-and-exit (send the message and exit)
695 Here are commands that move to a header field (and create it if there isn't):
696 \\[mail-to] move to To: \\[mail-subject] move to Subj:
697 \\[mail-bcc] move to BCC: \\[mail-cc] move to CC:
698 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
699 \\[mail-mail-reply-to] move to Mail-Reply-To:
700 \\[mail-mail-followup-to] move to Mail-Followup-To:
701 \\[mail-text] move to message text.
702 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
703 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
704 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
705 \\[mail-insert-file] insert a text file into the message.
706 \\[mail-add-attachment] attach to the message a file as binary attachment.
707 Turning on Mail mode runs the normal hooks `text-mode-hook' and
708 `mail-mode-hook' (in that order)."
709 (make-local-variable 'mail-reply-action)
710 (make-local-variable 'mail-send-actions)
711 (make-local-variable 'mail-return-action)
712 (make-local-variable 'mail-encode-mml)
713 (setq mail-encode-mml nil)
714 (setq buffer-offer-save t)
715 (make-local-variable 'font-lock-defaults)
716 (setq font-lock-defaults '(mail-font-lock-keywords t t))
717 (make-local-variable 'paragraph-separate)
718 (make-local-variable 'normal-auto-fill-function)
719 (setq normal-auto-fill-function 'mail-mode-auto-fill)
720 (make-local-variable 'fill-paragraph-function)
721 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
722 ;; Allow using comment commands to add/remove quoting (this only does
723 ;; anything if mail-yank-prefix is set to a non-nil value).
724 (set (make-local-variable 'comment-start) mail-yank-prefix)
725 (if mail-yank-prefix
726 (set (make-local-variable 'comment-start-skip)
727 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
728 (make-local-variable 'adaptive-fill-regexp)
729 ;; Also update the paragraph-separate entry if you change this.
730 (setq adaptive-fill-regexp
731 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
732 adaptive-fill-regexp))
733 (make-local-variable 'adaptive-fill-first-line-regexp)
734 (setq adaptive-fill-first-line-regexp
735 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
736 adaptive-fill-first-line-regexp))
737 (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
738 nil 'local)
739 ;; `-- ' precedes the signature. `-----' appears at the start of the
740 ;; lines that delimit forwarded messages.
741 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
742 ;; are also sometimes used and should be separators.
743 (setq paragraph-separate
744 (concat (regexp-quote mail-header-separator)
745 ;; This is based on adaptive-fill-regexp (presumably
746 ;; the idea is to allow navigation etc of cited paragraphs).
747 "$\\|\t*[-–!|#%;>*·•‣⁃◦ ]+$"
748 "\\|[ \t]*[-[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
749 "--\\( \\|-+\\)$\\|"
750 page-delimiter)))
753 (defun mail-header-end ()
754 "Return the buffer location of the end of headers, as a number."
755 (save-restriction
756 (widen)
757 (save-excursion
758 (rfc822-goto-eoh)
759 (point))))
761 (defun mail-text-start ()
762 "Return the buffer location of the start of text, as a number."
763 (save-restriction
764 (widen)
765 (save-excursion
766 (rfc822-goto-eoh)
767 (forward-line 1)
768 (point))))
770 (defun mail-sendmail-delimit-header ()
771 "Set up whatever header delimiter convention sendmail will use.
772 Concretely: replace the first blank line in the header with the separator."
773 (rfc822-goto-eoh)
774 (insert mail-header-separator)
775 (point))
777 (defun mail-sendmail-undelimit-header ()
778 "Remove header separator to put the message in correct form for sendmail.
779 Leave point at the start of the delimiter line."
780 (rfc822-goto-eoh)
781 (delete-region (point) (progn (end-of-line) (point))))
783 (defun mail-mode-auto-fill ()
784 "Carry out Auto Fill for Mail mode.
785 If within the headers, this makes the new lines into continuation lines."
786 (if (< (point) (mail-header-end))
787 (let ((old-line-start (line-beginning-position)))
788 (if (do-auto-fill)
789 (save-excursion
790 (beginning-of-line)
791 (while (not (eq (point) old-line-start))
792 ;; Use insert-before-markers in case we're inserting
793 ;; before the saved value of point (which is common).
794 (insert-before-markers " ")
795 (forward-line -1))
796 t)))
797 (do-auto-fill)))
799 (defun mail-mode-fill-paragraph (arg)
800 ;; Do something special only if within the headers.
801 (if (< (point) (mail-header-end))
802 (let (beg end fieldname)
803 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
804 (setq beg (point)))
805 (setq fieldname
806 (downcase (buffer-substring beg (1- (match-end 0))))))
807 (forward-line 1)
808 ;; Find continuation lines and get rid of their continuation markers.
809 (while (looking-at "[ \t]")
810 (delete-horizontal-space)
811 (forward-line 1))
812 (setq end (point-marker))
813 (goto-char beg)
814 ;; If this field contains addresses,
815 ;; make sure we can fill after each address.
816 (if (member fieldname
817 '("to" "cc" "bcc" "from" "reply-to"
818 "mail-reply-to" "mail-followup-to"
819 "resent-to" "resent-cc" "resent-bcc"
820 "resent-from" "resent-reply-to"))
821 (while (search-forward "," end t)
822 (or (looking-at "[ \t]")
823 (insert " "))))
824 (fill-region-as-paragraph beg end arg)
825 ;; Mark all lines except the first as continuations.
826 (goto-char beg)
827 (forward-line 1)
828 (while (< (point) end)
829 (insert " ")
830 (forward-line 1))
831 (move-marker end nil)
832 t)))
834 ;; User-level commands for sending.
836 (defun mail-send-and-exit (&optional arg)
837 "Send message like `mail-send', then, if no errors, exit from mail buffer.
838 Prefix arg means don't delete this window."
839 (interactive "P")
840 (mail-send)
841 (mail-bury arg))
843 (defun mail-dont-send (&optional arg)
844 "Don't send the message you have been editing.
845 Prefix arg means don't delete this window."
846 (interactive "P")
847 (mail-bury arg))
849 (defun mail-bury (&optional arg)
850 "Bury this mail buffer."
851 (let ((newbuf (other-buffer (current-buffer)))
852 (return-action mail-return-action))
853 (bury-buffer (current-buffer))
854 ;; If there is an Rmail buffer, return to it nicely
855 ;; even if this message was not started by an Rmail command.
856 (unless return-action
857 (dolist (buffer (buffer-list))
858 (if (and (eq (buffer-local-value 'major-mode buffer) 'rmail-mode)
859 (null return-action)
860 ;; Don't match message-viewer buffer.
861 (not (string-match "\\` " (buffer-name buffer))))
862 (setq return-action `(rmail-mail-return ,buffer)))))
863 (if (and (null arg) return-action)
864 (apply (car return-action) (cdr return-action))
865 (switch-to-buffer newbuf))))
867 (defcustom mail-send-hook nil
868 "Hook run just before sending a message."
869 :type 'hook
870 :options '(flyspell-mode-off)
871 :group 'sendmail)
873 ;;;###autoload
874 (defcustom mail-mailing-lists nil
875 "List of mailing list addresses the user is subscribed to.
876 The variable is used to trigger insertion of the \"Mail-Followup-To\"
877 header when sending a message to a mailing list."
878 :type '(repeat string)
879 :group 'sendmail)
881 (declare-function mml-to-mime "mml" ())
883 (defun mail-send ()
884 "Send the message in the current buffer.
885 If `mail-interactive' is non-nil, wait for success indication
886 or error messages, and inform user.
887 Otherwise any failure is reported in a message back to
888 the user from the mailer."
889 (interactive)
890 (if (if buffer-file-name
891 (y-or-n-p "Send buffer contents as mail message? ")
892 (or (buffer-modified-p)
893 (y-or-n-p "Message already sent; resend? ")))
894 (let ((inhibit-read-only t)
895 (opoint (point))
896 (ml (when mail-mailing-lists
897 ;; The surrounding regexp assumes the use of
898 ;; `mail-strip-quoted-names' on addresses before matching
899 ;; Cannot deal with full RFC 822 freedom, but that is
900 ;; unlikely to be problematic.
901 (concat "\\(?:[[:space:];,]\\|\\`\\)"
902 (regexp-opt mail-mailing-lists t)
903 "\\(?:[[:space:];,]\\|\\'\\)"))))
904 (mail-combine-fields "To")
905 (mail-combine-fields "CC")
906 ;; If there are mailing lists defined
907 (when ml
908 (save-excursion
909 (let* ((to (mail-fetch-field "to" nil t))
910 (cc (mail-fetch-field "cc" nil t))
911 (new-header-values ; To: and Cc:
912 (mail-strip-quoted-names
913 (concat to (when cc (concat ", " cc))))))
914 ;; If message goes to known mailing list ...
915 (when (string-match ml new-header-values)
916 ;; Add Mail-Followup-To if none yet
917 (unless (mail-fetch-field "mail-followup-to")
918 (goto-char (mail-header-end))
919 (insert "Mail-Followup-To: "
920 (let ((l))
921 (mapc
922 ;; remove duplicates
923 (lambda (e)
924 (unless (member e l)
925 (push e l)))
926 (split-string new-header-values
927 ",[[:space:]]+" t))
928 (mapconcat 'identity l ", "))
929 "\n"))
930 ;; Add Mail-Reply-To if none yet
931 (unless (mail-fetch-field "mail-reply-to")
932 (goto-char (mail-header-end))
933 (insert "Mail-Reply-To: "
934 (or (mail-fetch-field "reply-to")
935 user-mail-address)
936 "\n"))))))
937 (unless (memq mail-send-nonascii '(t mime))
938 (goto-char (point-min))
939 (skip-chars-forward "\0-\177")
940 (or (= (point) (point-max))
941 (if (eq mail-send-nonascii 'query)
942 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
943 (error "Aborted"))
944 (error "Message contains non-ASCII characters"))))
945 ;; Complain about any invalid line.
946 (goto-char (point-min))
947 (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
948 (let ((header-end (or (match-beginning 0) (point-max))))
949 (goto-char (point-min))
950 (while (< (point) header-end)
951 (unless (looking-at "[ \t]\\|.*:\\|$")
952 (push-mark opoint)
953 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
954 (forward-line 1)))
955 (goto-char opoint)
956 (when mail-encode-mml
957 (mml-to-mime)
958 (setq mail-encode-mml nil))
959 (run-hooks 'mail-send-hook)
960 (message "Sending...")
961 (funcall send-mail-function)
962 ;; Now perform actions on successful sending.
963 (while mail-send-actions
964 (condition-case nil
965 (apply (car (car mail-send-actions))
966 (cdr (car mail-send-actions)))
967 (error))
968 (setq mail-send-actions (cdr mail-send-actions)))
969 (message "Sending...done")
970 ;; If buffer has no file, mark it as unmodified and delete auto-save.
971 (if (not buffer-file-name)
972 (progn
973 (set-buffer-modified-p nil)
974 (delete-auto-save-file-if-necessary t))))))
976 (defun mail-envelope-from ()
977 "Return the envelope mail address to use when sending mail.
978 This function uses `mail-envelope-from'."
979 (if (eq mail-envelope-from 'header)
980 (nth 1 (mail-extract-address-components
981 (mail-fetch-field "From")))
982 mail-envelope-from))
984 ;; This does the real work of sending a message via sendmail.
985 ;; It is called via the variable send-mail-function.
987 ;;;###autoload
988 (defvar sendmail-coding-system nil
989 "Coding system for encoding the outgoing mail.
990 This has higher priority than the default `buffer-file-coding-system'
991 and `default-sendmail-coding-system',
992 but lower priority than the local value of `buffer-file-coding-system'.
993 See also the function `select-message-coding-system'.")
995 ;;;###autoload
996 (defvar default-sendmail-coding-system 'iso-latin-1
997 "Default coding system for encoding the outgoing mail.
998 This variable is used only when `sendmail-coding-system' is nil.
1000 This variable is set/changed by the command `set-language-environment'.
1001 User should not set this variable manually,
1002 instead use `sendmail-coding-system' to get a constant encoding
1003 of outgoing mails regardless of the current language environment.
1004 See also the function `select-message-coding-system'.")
1006 (defun mail-insert-from-field ()
1007 "Insert the \"From:\" field of a mail header.
1008 The style of the field is determined by the variable `mail-from-style'.
1009 This function does not perform RFC2047 encoding."
1010 (let* ((login user-mail-address)
1011 (fullname (user-full-name))
1012 (quote-fullname nil))
1013 (if (string-match "[^\0-\177]" fullname)
1014 (setq quote-fullname t))
1015 (cond ((null mail-from-style)
1016 (insert "From: " login "\n"))
1017 ;; This is deprecated.
1018 ((eq mail-from-style 'system-default)
1019 nil)
1020 ((or (eq mail-from-style 'angles)
1021 (and (not (eq mail-from-style 'parens))
1022 ;; Use angles if no quoting is needed, or if
1023 ;; parens would need quoting too.
1024 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1025 (let ((tmp (concat fullname nil)))
1026 (while (string-match "([^()]*)" tmp)
1027 (aset tmp (match-beginning 0) ?-)
1028 (aset tmp (1- (match-end 0)) ?-))
1029 (string-match "[\\()]" tmp)))))
1030 (insert "From: " fullname)
1031 (let ((fullname-start (+ (point-min) 6))
1032 (fullname-end (point-marker)))
1033 (goto-char fullname-start)
1034 ;; Look for a character that cannot appear unquoted
1035 ;; according to RFC 822.
1036 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
1037 fullname-end 1)
1038 quote-fullname)
1039 (progn
1040 ;; Quote fullname, escaping specials.
1041 (goto-char fullname-start)
1042 (insert "\"")
1043 (while (re-search-forward "[\"\\]"
1044 fullname-end 1)
1045 (replace-match "\\\\\\&" t))
1046 (insert "\""))))
1047 (insert " <" login ">\n"))
1048 ;; 'parens or default
1050 (insert "From: " login " (")
1051 (let ((fullname-start (point)))
1052 (if quote-fullname
1053 (insert "\""))
1054 (insert fullname)
1055 (if quote-fullname
1056 (insert "\""))
1057 (let ((fullname-end (point-marker)))
1058 (goto-char fullname-start)
1059 ;; RFC 822 says \ and nonmatching parentheses
1060 ;; must be escaped in comments.
1061 ;; Escape every instance of ()\ ...
1062 (while (re-search-forward "[()\\]" fullname-end 1)
1063 (replace-match "\\\\\\&" t))
1064 ;; ... then undo escaping of matching parentheses,
1065 ;; including matching nested parentheses.
1066 (goto-char fullname-start)
1067 (while (re-search-forward
1068 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1069 fullname-end 1)
1070 (replace-match "\\1(\\3)" t)
1071 (goto-char fullname-start))))
1072 (insert ")\n")))))
1074 (defun mail-combine-fields (field)
1075 "Offer to combine all FIELD fields in buffer into one FIELD field.
1076 If this finds multiple FIELD fields, it asks the user whether
1077 to combine them into one, and does so if the user says y."
1078 (let ((search-pattern (format "^%s[ \t]*:" field))
1079 first-to-end
1080 query-asked
1081 query-answer
1082 (old-point (point))
1083 (old-max (point-max)))
1084 (save-excursion
1085 (save-restriction
1086 (goto-char (point-min))
1087 (narrow-to-region (point-min) (mail-header-end))
1088 ;; Find the first FIELD field and record where it ends.
1089 (when (re-search-forward search-pattern nil t)
1090 (forward-line 1)
1091 (re-search-forward "^[^ \t]" nil t)
1092 (beginning-of-line)
1093 (setq first-to-end (point-marker))
1094 (set-marker-insertion-type first-to-end t)
1095 ;; Find each following FIELD field
1096 ;; and combine it with the first FIELD field.
1097 (while (re-search-forward search-pattern nil t)
1098 ;; For the second FIELD field, ask user to
1099 ;; approve combining them.
1100 ;; But if the user refuse to combine them, signal error.
1101 (unless query-asked
1102 (save-restriction
1103 ;; This is just so the screen doesn't change.
1104 (narrow-to-region (point-min) old-max)
1105 (save-excursion
1106 (goto-char old-point)
1107 (setq query-asked t)
1108 (if (y-or-n-p (format "Message contains multiple %s fields. Combine? " field))
1109 (setq query-answer t)))))
1110 (when query-answer
1111 (let ((this-to-start (line-beginning-position))
1112 this-to-end
1113 this-to)
1114 (forward-line 1)
1115 (re-search-forward "^[^ \t]" nil t)
1116 (beginning-of-line)
1117 (setq this-to-end (point))
1118 ;; Get the text of this FIELD field.
1119 (setq this-to (buffer-substring this-to-start this-to-end))
1120 ;; Delete it.
1121 (delete-region this-to-start this-to-end)
1122 (save-excursion
1123 ;; Put a comma after the first FIELD field.
1124 (goto-char first-to-end)
1125 (forward-char -1)
1126 (insert ",")
1127 ;; Copy this one after it.
1128 (goto-char first-to-end)
1129 (save-excursion
1130 (insert this-to))
1131 ;; Replace the FIELD: with spaces.
1132 (looking-at search-pattern)
1133 ;; Try to preserve alignment of contents of the field
1134 (let ((prefix-length (length (match-string 0))))
1135 (replace-match " ")
1136 (dotimes (i (1- prefix-length))
1137 (insert " ")))))))
1138 (set-marker first-to-end nil))))))
1140 (defun mail-encode-header (beg end)
1141 "Encode the mail header between BEG and END according to RFC2047.
1142 Return non-nil if and only if some part of the header is encoded."
1143 (save-restriction
1144 (narrow-to-region beg end)
1145 (let* ((selected (select-message-coding-system))
1146 (mm-coding-system-priorities
1147 (if (and selected (coding-system-get selected :mime-charset))
1148 (cons selected mm-coding-system-priorities)
1149 mm-coding-system-priorities))
1150 (tick (buffer-chars-modified-tick))
1151 ;; Many mailers, including Gnus, passes a message of which
1152 ;; the header is already encoded, so this is necessary to
1153 ;; prevent it from being encoded again.
1154 (rfc2047-encode-encoded-words nil))
1155 (rfc2047-encode-message-header)
1156 (= tick (buffer-chars-modified-tick)))))
1158 ;; Normally you will not need to modify these options unless you are
1159 ;; using some non-genuine substitute for sendmail which does not
1160 ;; implement each and every option that the original supports.
1161 ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1162 ;; you will need to modify `sendmail-error-reporting-non-interactive'
1163 ;; in your site-init.el.
1164 (defvar sendmail-error-reporting-interactive
1165 ;; These mean "report errors to terminal" and "deliver interactively"
1166 '("-oep" "-odi"))
1167 (defvar sendmail-error-reporting-non-interactive
1168 ;; These mean "report errors by mail" and "deliver in background".
1169 '("-oem" "-odb"))
1171 (defun sendmail-send-it ()
1172 "Send the current mail buffer using the Sendmail package.
1173 This is a suitable value for `send-mail-function'. It sends using the
1174 external program defined by `sendmail-program'."
1175 (require 'mail-utils)
1176 (let ((errbuf (if mail-interactive
1177 (generate-new-buffer " sendmail errors")
1179 (error nil)
1180 (tembuf (generate-new-buffer " sendmail temp"))
1181 (multibyte enable-multibyte-characters)
1182 (case-fold-search nil)
1183 (selected-coding (select-message-coding-system))
1184 resend-to-addresses
1185 delimline
1186 fcc-was-found
1187 (mailbuf (current-buffer))
1188 ;; Examine these variables now, so that
1189 ;; local binding in the mail buffer will take effect.
1190 (envelope-from
1191 (and mail-specify-envelope-from
1192 (or (mail-envelope-from) user-mail-address))))
1193 (unwind-protect
1194 (with-current-buffer tembuf
1195 (erase-buffer)
1196 (unless multibyte
1197 (set-buffer-multibyte nil))
1198 (insert-buffer-substring mailbuf)
1199 (set-buffer-file-coding-system selected-coding)
1200 (goto-char (point-max))
1201 ;; require one newline at the end.
1202 (or (= (preceding-char) ?\n)
1203 (insert ?\n))
1204 ;; Change header-delimiter to be what sendmail expects.
1205 (goto-char (mail-header-end))
1206 (delete-region (point) (progn (end-of-line) (point)))
1207 (setq delimline (point-marker))
1208 (sendmail-sync-aliases)
1209 (if mail-aliases
1210 (expand-mail-aliases (point-min) delimline))
1211 (goto-char (point-min))
1212 ;; Ignore any blank lines in the header
1213 (while (and (re-search-forward "\n\n\n*" delimline t)
1214 (< (point) delimline))
1215 (replace-match "\n"))
1216 (goto-char (point-min))
1217 ;; Look for Resent- headers. They require sending
1218 ;; the message specially.
1219 (let ((case-fold-search t))
1220 (goto-char (point-min))
1221 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1222 ;; Put a list of such addresses in resend-to-addresses.
1223 (setq resend-to-addresses
1224 (save-restriction
1225 (narrow-to-region (point)
1226 (save-excursion
1227 (forward-line 1)
1228 (while (looking-at "^[ \t]")
1229 (forward-line 1))
1230 (point)))
1231 (append (mail-parse-comma-list)
1232 resend-to-addresses)))
1233 ;; Delete Resent-BCC ourselves
1234 (if (save-excursion (beginning-of-line)
1235 (looking-at "resent-bcc"))
1236 (delete-region (line-beginning-position)
1237 (line-beginning-position 2))))
1238 ;; Apparently this causes a duplicate Sender.
1239 ;; ;; If the From is different than current user, insert Sender.
1240 ;; (goto-char (point-min))
1241 ;; (and (re-search-forward "^From:" delimline t)
1242 ;; (progn
1243 ;; (require 'mail-utils)
1244 ;; (not (string-equal
1245 ;; (mail-strip-quoted-names
1246 ;; (save-restriction
1247 ;; (narrow-to-region (point-min) delimline)
1248 ;; (mail-fetch-field "From")))
1249 ;; (user-login-name))))
1250 ;; (progn
1251 ;; (forward-line 1)
1252 ;; (insert "Sender: " (user-login-name) "\n")))
1253 ;; Don't send out a blank subject line
1254 (goto-char (point-min))
1255 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1256 (replace-match "")
1257 ;; This one matches a Subject just before the header delimiter.
1258 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1259 (= (match-end 0) delimline))
1260 (replace-match "")))
1261 ;; Put the "From:" field in unless for some odd reason
1262 ;; they put one in themselves.
1263 (goto-char (point-min))
1264 (if (not (re-search-forward "^From:" delimline t))
1265 (mail-insert-from-field))
1266 ;; Possibly add a MIME header for the current coding system
1267 (let (charset where-content-type)
1268 (goto-char (point-min))
1269 (setq where-content-type
1270 (re-search-forward "^Content-type:" delimline t))
1271 (goto-char (point-min))
1272 (and (eq mail-send-nonascii 'mime)
1273 (not (re-search-forward "^MIME-version:" delimline t))
1274 (progn (skip-chars-forward "\0-\177")
1275 (/= (point) (point-max)))
1276 selected-coding
1277 (setq charset
1278 (coding-system-get selected-coding :mime-charset))
1279 (progn
1280 (goto-char delimline)
1281 (insert "MIME-version: 1.0\n"
1282 "Content-type: text/plain; charset="
1283 (symbol-name charset)
1284 "\nContent-Transfer-Encoding: 8bit\n")
1285 ;; The character set we will actually use
1286 ;; should override any specified in the message itself.
1287 (when where-content-type
1288 (goto-char where-content-type)
1289 (delete-region (point-at-bol)
1290 (progn (forward-line 1) (point)))))))
1291 ;; Insert an extra newline if we need it to work around
1292 ;; Sun's bug that swallows newlines.
1293 (goto-char (1+ delimline))
1294 (if (eval mail-mailer-swallows-blank-line)
1295 (newline))
1296 ;; Find and handle any FCC fields.
1297 (goto-char (point-min))
1298 (if (re-search-forward "^FCC:" delimline t)
1299 (progn
1300 (setq fcc-was-found t)
1301 (mail-do-fcc delimline)))
1302 (if mail-interactive
1303 (with-current-buffer errbuf
1304 (erase-buffer))))
1305 ;; Encode the header according to RFC2047.
1306 (mail-encode-header (point-min) delimline)
1307 (goto-char (point-min))
1308 (if (let ((case-fold-search t))
1309 (or resend-to-addresses
1310 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1311 delimline t)))
1312 (let* ((default-directory "/")
1313 (coding-system-for-write selected-coding)
1314 (args
1315 (append (list (point-min) (point-max)
1316 sendmail-program
1317 nil errbuf nil "-oi")
1318 (and envelope-from
1319 (list "-f" envelope-from))
1320 ;; ;; Don't say "from root" if running under su.
1321 ;; (and (equal (user-real-login-name) "root")
1322 ;; (list "-f" (user-login-name)))
1323 (and mail-alias-file
1324 (list (concat "-oA" mail-alias-file)))
1325 (if mail-interactive
1326 sendmail-error-reporting-interactive
1327 sendmail-error-reporting-non-interactive)
1328 ;; Get the addresses from the message
1329 ;; unless this is a resend.
1330 ;; We must not do that for a resend
1331 ;; because we would find the original addresses.
1332 ;; For a resend, include the specific addresses.
1333 (or resend-to-addresses
1334 '("-t")
1336 (if mail-use-dsn
1337 (list "-N" (mapconcat 'symbol-name
1338 mail-use-dsn ",")))
1341 (exit-value (apply 'call-process-region args)))
1342 (cond ((or (null exit-value) (eq 0 exit-value)))
1343 ((numberp exit-value)
1344 (setq error t)
1345 (error "Sending...failed with exit value %d" exit-value))
1346 ((stringp exit-value)
1347 (setq error t)
1348 (error "Sending...terminated by signal: %s" exit-value))
1350 (setq error t)
1351 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
1352 (or fcc-was-found
1353 (error "No recipients")))
1354 (if mail-interactive
1355 (with-current-buffer errbuf
1356 (goto-char (point-min))
1357 (while (re-search-forward "\n\n* *" nil t)
1358 (replace-match "; "))
1359 (unless (zerop (buffer-size))
1360 (setq error t)
1361 (error "Sending...failed to %s"
1362 (buffer-substring (point-min) (point-max)))))))
1363 (kill-buffer tembuf)
1364 (when (buffer-live-p errbuf)
1365 (if error
1366 (switch-to-buffer-other-window errbuf)
1367 (kill-buffer errbuf))))))
1369 (autoload 'rmail-output-to-rmail-buffer "rmailout")
1371 (defun mail-do-fcc (header-end)
1372 "Find and act on any FCC: headers in the current message before HEADER-END.
1373 If a buffer is visiting the FCC file, append to it before
1374 offering to save it, if it was modified initially. If this is an
1375 Rmail buffer, update Rmail as needed. If there is no buffer,
1376 just append to the file, in Babyl format if necessary."
1377 (unless (markerp header-end)
1378 (error "Value of `header-end' must be a marker"))
1379 (let (fcc-list
1380 (mailbuf (current-buffer))
1381 (time (current-time)))
1382 (save-excursion
1383 (goto-char (point-min))
1384 (let ((case-fold-search t))
1385 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1386 (push (buffer-substring (point)
1387 (progn
1388 (end-of-line)
1389 (skip-chars-backward " \t")
1390 (point)))
1391 fcc-list)
1392 (delete-region (match-beginning 0)
1393 (progn (forward-line 1) (point)))))
1394 (with-temp-buffer
1395 ;; This initial newline is not written out if we create a new
1396 ;; file (see below).
1397 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1398 ;; Insert the time zone before the year.
1399 (forward-char -1)
1400 (forward-word-strictly -1)
1401 (require 'mail-utils)
1402 (insert (mail-rfc822-time-zone time) " ")
1403 (goto-char (point-max))
1404 (insert "Date: " (message-make-date) "\n")
1405 (insert-buffer-substring mailbuf)
1406 ;; Make sure messages are separated.
1407 (goto-char (point-max))
1408 (insert ?\n)
1409 (goto-char 2)
1410 ;; ``Quote'' "^From " as ">From "
1411 ;; (note that this isn't really quoting, as there is no requirement
1412 ;; that "^[>]+From " be quoted in the same transparent way.)
1413 (let ((case-fold-search nil))
1414 (while (search-forward "\nFrom " nil t)
1415 (forward-char -5)
1416 (insert ?>)))
1417 (dolist (fcc fcc-list)
1418 (let* ((buffer (find-buffer-visiting fcc))
1419 (curbuf (current-buffer))
1420 dont-write-the-file
1421 buffer-matches-file
1422 (beg (point-min)) ; the initial blank line
1423 (end (point-max))
1424 ;; After the ^From line.
1425 (beg2 (save-excursion (goto-char (point-min))
1426 (forward-line 2) (point))))
1427 (if buffer
1428 ;; File is present in a buffer => append to that buffer.
1429 (with-current-buffer buffer
1430 (setq buffer-matches-file
1431 (and (not (buffer-modified-p))
1432 (verify-visited-file-modtime buffer)))
1433 (let ((msg (bound-and-true-p rmail-current-message))
1434 (buffer-read-only nil))
1435 ;; If MSG is non-nil, buffer is in Rmail mode.
1436 (if msg
1437 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1438 (unwind-protect
1439 (progn
1440 (with-current-buffer buff
1441 (insert-buffer-substring curbuf (1+ beg) end))
1442 (rmail-output-to-rmail-buffer buff msg))
1443 (kill-buffer buff)))
1444 ;; Output file not in Rmail mode => just insert
1445 ;; at the end.
1446 (save-restriction
1447 (widen)
1448 (goto-char (point-max))
1449 (insert-buffer-substring curbuf beg end)))
1450 ;; Offer to save the buffer if it was modified
1451 ;; before we started.
1452 (unless buffer-matches-file
1453 (if (y-or-n-p (format "Save file %s? " fcc))
1454 (save-buffer))
1455 (setq dont-write-the-file t)))))
1456 ;; Append to the file directly, unless we've already taken
1457 ;; care of it.
1458 (unless dont-write-the-file
1459 (if (and (file-exists-p fcc)
1460 (mail-file-babyl-p fcc))
1461 ;; If the file is a Babyl file, convert the message to
1462 ;; Babyl format. Even though Rmail no longer uses
1463 ;; Babyl, this code can remain for the time being, on
1464 ;; the off-chance one FCCs to a Babyl file that has
1465 ;; not yet been converted to mbox.
1466 (let ((coding-system-for-write
1467 (or rmail-file-coding-system 'emacs-mule)))
1468 (with-temp-buffer
1469 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1470 (mail-rfc822-date) "\n")
1471 (insert-buffer-substring curbuf beg2 end)
1472 (insert "\n\C-_")
1473 (write-region (point-min) (point-max) fcc t)))
1474 ;; Ensure there is a blank line between messages, but
1475 ;; not at the very start of the file.
1476 (write-region (if (file-exists-p fcc)
1477 (point-min)
1478 (1+ (point-min)))
1479 (point-max) fcc t)))
1480 (and buffer (not dont-write-the-file)
1481 (with-current-buffer buffer
1482 (set-visited-file-modtime)))))))))
1484 (defun mail-sent-via ()
1485 "Make a Sent-via header line from each To or CC header line."
1486 (declare (obsolete "nobody can remember what it is for." "24.1"))
1487 (interactive)
1488 (save-excursion
1489 ;; put a marker at the end of the header
1490 (let ((end (copy-marker (mail-header-end)))
1491 (case-fold-search t))
1492 (goto-char (point-min))
1493 ;; search for the To: lines and make Sent-via: lines from them
1494 ;; search for the next To: line
1495 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1496 ;; Grab this line plus all its continuations, sans the `to:'.
1497 (let ((to-line
1498 (buffer-substring (point)
1499 (progn
1500 (if (re-search-forward "^[^ \t\n]" end t)
1501 (backward-char 1)
1502 (goto-char end))
1503 (point)))))
1504 ;; Insert a copy, with altered header field name.
1505 (insert-before-markers "Sent-via:" to-line))))))
1507 (defun mail-to ()
1508 "Move point to end of To field, creating it if necessary."
1509 (interactive)
1510 (expand-abbrev)
1511 (mail-position-on-field "To"))
1513 (defun mail-subject ()
1514 "Move point to end of Subject field, creating it if necessary."
1515 (interactive)
1516 (expand-abbrev)
1517 (mail-position-on-field "Subject"))
1519 (defun mail-cc ()
1520 "Move point to end of CC field, creating it if necessary."
1521 (interactive)
1522 (expand-abbrev)
1523 (or (mail-position-on-field "cc" t)
1524 (progn (mail-position-on-field "to")
1525 (insert "\nCC: "))))
1527 (defun mail-bcc ()
1528 "Move point to end of BCC field, creating it if necessary."
1529 (interactive)
1530 (expand-abbrev)
1531 (or (mail-position-on-field "bcc" t)
1532 (progn (mail-position-on-field "to")
1533 (insert "\nBCC: "))))
1535 (defun mail-fcc (folder)
1536 "Add a new FCC field, with file name completion."
1537 (interactive "FFolder carbon copy: ")
1538 (expand-abbrev)
1539 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1540 (mail-position-on-field "to"))
1541 (insert "\nFCC: " folder))
1543 (defun mail-reply-to ()
1544 "Move point to end of Reply-To field, creating it if necessary."
1545 (interactive)
1546 (expand-abbrev)
1547 (mail-position-on-field "Reply-To"))
1549 (defun mail-mail-reply-to ()
1550 "Move point to end of Mail-Reply-To field, creating it if necessary."
1551 (interactive)
1552 (expand-abbrev)
1553 (or (mail-position-on-field "mail-reply-to" t)
1554 (progn (mail-position-on-field "to")
1555 (insert "\nMail-Reply-To: "))))
1557 (defun mail-mail-followup-to ()
1558 "Move point to end of Mail-Followup-To field, creating it if necessary."
1559 (interactive)
1560 (expand-abbrev)
1561 (or (mail-position-on-field "mail-followup-to" t)
1562 (progn (mail-position-on-field "to")
1563 (insert "\nMail-Followup-To: "))))
1565 (defun mail-position-on-field (field &optional soft)
1566 "Move to the end of the contents of header field FIELD.
1567 If there is no such header, insert one, unless SOFT is non-nil.
1568 If there are multiple FIELD fields, this goes to the first.
1569 Returns non-nil if FIELD was originally present."
1570 (let (end
1571 (case-fold-search t))
1572 (setq end (mail-header-end))
1573 (goto-char (point-min))
1574 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1575 (progn
1576 (re-search-forward "^[^ \t]" nil 'move)
1577 (beginning-of-line)
1578 (skip-chars-backward "\n")
1580 (or soft
1581 (progn (goto-char end)
1582 (insert field ": \n")
1583 (skip-chars-backward "\n")))
1584 nil)))
1586 (defun mail-text ()
1587 "Move point to beginning of text field."
1588 (interactive)
1589 (expand-abbrev)
1590 (goto-char (mail-text-start)))
1592 (defun mail-signature (&optional atpoint)
1593 "Sign letter with signature.
1594 If the variable `mail-signature' is a string, inserts it.
1595 If it is t or nil, inserts the contents of the file `mail-signature-file'.
1596 Otherwise, evals `mail-signature'.
1597 Prefix argument ATPOINT means insert at point rather than the end."
1598 (interactive "*P")
1599 ;; Test for an unreadable file here, before we delete trailing
1600 ;; whitespace, so that we don't modify the buffer needlessly.
1601 (if (and (memq mail-signature '(t nil))
1602 (not (file-readable-p mail-signature-file)))
1603 (if (called-interactively-p 'interactive)
1604 (message "The signature file `%s' could not be read"
1605 mail-signature-file))
1606 (save-excursion
1607 (unless atpoint
1608 (goto-char (point-max))
1609 ;; Delete trailing whitespace and blank lines.
1610 (skip-chars-backward " \t\n")
1611 (end-of-line)
1612 (delete-region (point) (point-max)))
1613 (cond ((stringp mail-signature)
1614 (insert mail-signature))
1615 ((memq mail-signature '(t nil))
1616 (insert "\n\n-- \n")
1617 (insert-file-contents (expand-file-name mail-signature-file)))
1619 ;; FIXME add condition-case error handling?
1620 (eval mail-signature))))))
1622 (defun mail-fill-yanked-message (&optional justifyp)
1623 "Fill the paragraphs of a message yanked into this one.
1624 Numeric argument means justify as well."
1625 (interactive "P")
1626 (save-excursion
1627 (goto-char (mail-text-start))
1628 (fill-individual-paragraphs (point)
1629 (point-max)
1630 justifyp
1631 mail-citation-prefix-regexp)))
1633 (defun mail-indent-citation ()
1634 "Modify text just inserted from a message to be cited.
1635 The inserted text should be the region.
1636 When this function returns, the region is again around the modified text.
1638 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1639 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1640 (mail-yank-clear-headers (region-beginning) (region-end))
1641 (if (null mail-yank-prefix)
1642 (indent-rigidly (region-beginning) (region-end)
1643 mail-indentation-spaces)
1644 (save-excursion
1645 (let ((end (set-marker (make-marker) (region-end))))
1646 (goto-char (region-beginning))
1647 (while (< (point) end)
1648 (insert mail-yank-prefix)
1649 (forward-line 1))))))
1651 (defun mail-yank-original (arg)
1652 "Insert the message being replied to, if any (in Rmail).
1653 Puts point after the text and mark before.
1654 Normally, indents each nonblank line ARG spaces (default 3).
1655 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1657 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1658 and don't delete any header fields."
1659 (interactive "P")
1660 (if mail-reply-action
1661 (let ((start (point))
1662 (original mail-reply-action)
1663 (omark (mark t)))
1664 (and (consp original) (eq (car original) 'insert-buffer)
1665 (setq original (nth 1 original)))
1666 (if (consp original)
1667 (progn
1668 ;; Call yank function, and set the mark if it doesn't.
1669 (apply (car original) (cdr original))
1670 (if (eq omark (mark t))
1671 (push-mark)))
1672 ;; If the original message is in another window in the same
1673 ;; frame, delete that window to save space.
1674 (delete-windows-on original t)
1675 (with-no-warnings
1676 ;; We really want this to set mark.
1677 (insert-buffer original)
1678 ;; If they yank the original text, the encoding of the
1679 ;; original message is a better default than
1680 ;; the default buffer-file-coding-system.
1681 (and (coding-system-equal
1682 (default-value 'buffer-file-coding-system)
1683 buffer-file-coding-system)
1684 (setq buffer-file-coding-system
1685 (coding-system-change-text-conversion
1686 buffer-file-coding-system
1687 (coding-system-base
1688 (with-current-buffer original
1689 buffer-file-coding-system))))))
1690 (set-text-properties (point) (mark t) nil))
1691 (if (consp arg)
1693 (goto-char start)
1694 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1695 mail-indentation-spaces))
1696 ;; Avoid error in Transient Mark mode
1697 ;; on account of mark's being inactive.
1698 (mark-even-if-inactive t))
1699 (cond (mail-citation-hook
1700 ;; Bind mail-citation-header to the inserted
1701 ;; message's header.
1702 (let ((mail-citation-header
1703 (buffer-substring-no-properties
1704 start
1705 (save-excursion
1706 (save-restriction
1707 (narrow-to-region start (point-max))
1708 (goto-char start)
1709 (rfc822-goto-eoh)
1710 (point))))))
1711 (run-hooks 'mail-citation-hook)))
1713 (mail-indent-citation)))))
1714 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1715 ;; It is cleaner to avoid activation, even though the command
1716 ;; loop would deactivate the mark because we inserted text.
1717 (goto-char (prog1 (mark t)
1718 (set-marker (mark-marker) (point) (current-buffer))))
1719 (if (not (eolp)) (insert ?\n)))))
1721 (defun mail-yank-clear-headers (start end)
1722 (if (< end start)
1723 (let (temp)
1724 (setq temp start start end end temp)))
1725 (if mail-yank-ignored-headers
1726 (save-excursion
1727 (goto-char start)
1728 (if (search-forward "\n\n" end t)
1729 (save-restriction
1730 (narrow-to-region start (point))
1731 (goto-char start)
1732 (while (let ((case-fold-search t))
1733 (re-search-forward mail-yank-ignored-headers nil t))
1734 (beginning-of-line)
1735 (delete-region (point)
1736 (progn (re-search-forward "\n[^ \t]")
1737 (forward-char -1)
1738 (point)))))))))
1740 (defun mail-yank-region (arg)
1741 "Insert the selected region from the message being replied to.
1742 Puts point after the text and mark before.
1743 Normally, indents each nonblank line ARG spaces (default 3).
1744 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1746 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1747 and don't delete any header fields."
1748 (interactive "P")
1749 (and (consp mail-reply-action)
1750 (memq (car mail-reply-action)
1751 '(rmail-yank-current-message insert-buffer))
1752 (with-current-buffer (nth 1 mail-reply-action)
1753 (or (mark t)
1754 (error "No mark set: %S" (current-buffer))))
1755 (let ((buffer (nth 1 mail-reply-action))
1756 (start (point))
1757 ;; Avoid error in Transient Mark mode
1758 ;; on account of mark's being inactive.
1759 (mark-even-if-inactive t))
1760 ;; Insert the citation text.
1761 (insert (with-current-buffer buffer
1762 (buffer-substring-no-properties (point) (mark))))
1763 (push-mark start)
1764 ;; Indent or otherwise annotate the citation text.
1765 (if (consp arg)
1767 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1768 mail-indentation-spaces)))
1769 (if mail-citation-hook
1770 ;; Bind mail-citation-header to the original message's header.
1771 (let ((mail-citation-header
1772 (with-current-buffer buffer
1773 (buffer-substring-no-properties
1774 (point-min)
1775 (save-excursion
1776 (goto-char (point-min))
1777 (rfc822-goto-eoh)
1778 (point))))))
1779 (run-hooks 'mail-citation-hook))
1780 (mail-indent-citation)))))))
1782 (defun mail-split-line ()
1783 "Split current line, moving portion beyond point vertically down.
1784 If the current line has `mail-yank-prefix', insert it on the new line."
1785 (interactive "*")
1786 (split-line mail-yank-prefix))
1789 (defun mail-insert-file (&optional file)
1790 "Insert a file at the end of the buffer, with separator lines around it."
1791 (interactive "fAttach file: ")
1792 (save-excursion
1793 (goto-char (point-max))
1794 (or (bolp) (newline))
1795 (newline)
1796 (let ((start (point))
1797 middle)
1798 (insert (format "===File %s===" file))
1799 (insert-char ?= (max 0 (- 60 (current-column))))
1800 (newline)
1801 (setq middle (point))
1802 (insert "============================================================\n")
1803 (push-mark)
1804 (goto-char middle)
1805 (insert-file-contents file)
1806 (or (bolp) (newline))
1807 (goto-char start))))
1809 (define-obsolete-function-alias 'mail-attach-file 'mail-insert-file "24.1")
1811 (declare-function mml-attach-file "mml"
1812 (file &optional type description disposition))
1813 (declare-function mm-default-file-encoding "mm-encode" (file))
1815 (defun mail-add-attachment (file)
1816 "Add FILE as a MIME attachment to the end of the mail message being composed."
1817 (interactive "fAttach file: ")
1818 (mml-attach-file file
1819 (or (mm-default-file-encoding file)
1820 "application/octet-stream") nil)
1821 (setq mail-encode-mml t))
1824 ;; Put these commands last, to reduce chance of lossage from quitting
1825 ;; in middle of loading the file.
1827 ;;;###autoload
1828 (defun mail (&optional noerase to subject in-reply-to cc replybuffer
1829 actions return-action)
1830 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1831 When this function returns, the buffer `*mail*' is selected.
1832 The value is t if the message was newly initialized; otherwise, nil.
1834 Optionally, the signature file `mail-signature-file' can be inserted at the
1835 end; see the variable `mail-signature'.
1837 \\<mail-mode-map>
1838 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1840 Various special commands starting with C-c are available in sendmail mode
1841 to move to message header fields:
1842 \\{mail-mode-map}
1844 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1845 when the message is initialized.
1847 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1848 a Reply-to: field with that address is inserted.
1850 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1851 is inserted.
1853 The normal hook `mail-setup-hook' is run after the message is
1854 initialized. It can add more default fields to the message.
1856 The first argument, NOERASE, determines what to do when there is
1857 an existing modified `*mail*' buffer. If NOERASE is nil, the
1858 existing mail buffer is used, and the user is prompted whether to
1859 keep the old contents or to erase them. If NOERASE has the value
1860 `new', a new mail buffer will be created instead of using the old
1861 one. Any other non-nil value means to always select the old
1862 buffer without erasing the contents.
1864 The second through fifth arguments,
1865 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1866 the initial contents of those header fields.
1867 These arguments should not have final newlines.
1868 The sixth argument REPLYBUFFER is a buffer which contains an
1869 original message being replied to, or else an action
1870 of the form (FUNCTION . ARGS) which says how to insert the original.
1871 Or it can be nil, if not replying to anything.
1872 The seventh argument ACTIONS is a list of actions to take
1873 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1874 when the message is sent, we apply FUNCTION to ARGS.
1875 This is how Rmail arranges to mark messages `answered'."
1876 (interactive "P")
1877 (if (eq noerase 'new)
1878 (pop-to-buffer-same-window (generate-new-buffer "*mail*"))
1879 (and noerase
1880 (not (get-buffer "*mail*"))
1881 (setq noerase nil))
1882 (pop-to-buffer-same-window "*mail*"))
1884 ;; Avoid danger that the auto-save file can't be written.
1885 (let ((dir (expand-file-name
1886 (file-name-as-directory mail-default-directory))))
1887 (if (file-exists-p dir)
1888 (setq default-directory dir)))
1889 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1890 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1891 (and (not auto-save-default) buffer-auto-save-file-name))
1892 (auto-save-mode auto-save-default))
1893 (mail-mode)
1894 ;; Disconnect the buffer from its visited file
1895 ;; (in case the user has actually visited a file *mail*).
1896 ;; (set-visited-file-name nil)
1897 (let (initialized)
1898 (and (not (and noerase
1899 (not (eq noerase 'new))))
1900 (if buffer-file-name
1901 (if (buffer-modified-p)
1902 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1903 (if (y-or-n-p "Disconnect buffer from visited file? ")
1904 (set-visited-file-name nil))
1906 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1907 (set-visited-file-name nil)
1909 ;; A non-file-visiting buffer.
1910 (if (buffer-modified-p)
1911 (y-or-n-p "Unsent message being composed; erase it? ")
1913 (let ((inhibit-read-only t))
1914 (erase-buffer)
1915 (mail-setup to subject in-reply-to cc replybuffer actions
1916 return-action)
1917 (setq initialized t)))
1918 (if (and buffer-auto-save-file-name
1919 (file-exists-p buffer-auto-save-file-name))
1920 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1921 initialized))
1923 (declare-function dired-view-file "dired" ())
1924 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1926 (defun mail-recover-1 ()
1927 "Pop up a list of auto-saved draft messages so you can recover one of them."
1928 (interactive)
1929 (let ((file-name (make-auto-save-file-name))
1930 (ls-lisp-support-shell-wildcards t)
1931 non-random-len wildcard)
1932 ;; Remove the random part from the auto-save-file-name, and
1933 ;; create a wildcard which matches possible candidates.
1934 ;; Note: this knows that make-auto-save-file-name appends
1935 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1936 ;; is the result of (make-temp-name "").
1937 (setq non-random-len
1938 (- (length file-name) (length (make-temp-name "")) 1))
1939 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1940 (if (null (file-expand-wildcards wildcard))
1941 (message "There are no auto-saved drafts to recover")
1942 ;; Bind dired-trivial-filenames to t because all auto-save file
1943 ;; names are normally ``trivial'', so Dired will set point after
1944 ;; all the files, at buffer bottom. We want it on the first
1945 ;; file instead.
1946 ;; Require dired so that dired-trivial-filenames does not get
1947 ;; unbound on exit from the let.
1948 (require 'dired)
1949 (let ((dired-trivial-filenames t))
1950 (dired-other-window wildcard (concat dired-listing-switches " -t")))
1951 (rename-buffer "*Auto-saved Drafts*" t)
1952 (save-excursion
1953 (goto-char (point-min))
1954 (or (looking-at " Move to the draft file you want to recover,")
1955 (let ((inhibit-read-only t))
1956 ;; Each line starts with a space so that Font Lock mode
1957 ;; won't highlight the first character.
1958 (insert "\
1959 Move to the draft file you want to recover, then type C-c C-c
1960 to recover text of message whose composition was interrupted.
1961 To browse text of a draft, type v on the draft file's line.
1963 You can also delete some of these files;
1964 type d on a line to mark that file for deletion.
1966 List of possible auto-save files for recovery:
1968 "))))
1969 (use-local-map
1970 (let ((map (make-sparse-keymap)))
1971 (set-keymap-parent map (current-local-map))
1972 map))
1973 (define-key (current-local-map) "v"
1974 (lambda ()
1975 (interactive)
1976 (let ((coding-system-for-read 'utf-8-emacs-unix))
1977 (dired-view-file))))
1978 (define-key (current-local-map) "\C-c\C-c"
1979 (lambda ()
1980 (interactive)
1981 (let ((fname (dired-get-filename))
1982 ;; Auto-saved files are written in the internal
1983 ;; representation, so they should be read accordingly.
1984 (coding-system-for-read 'utf-8-emacs-unix))
1985 (switch-to-buffer-other-window "*mail*")
1986 (let ((buffer-read-only nil))
1987 (erase-buffer)
1988 (insert-file-contents fname nil)
1989 ;; insert-file-contents will set buffer-file-coding-system
1990 ;; to utf-8-emacs, which is probably not what they want to
1991 ;; use for sending the message. But we don't know what
1992 ;; was its value before the buffer was killed or Emacs
1993 ;; crashed. We therefore reset buffer-file-coding-system
1994 ;; to the default value, so that either the default does
1995 ;; TRT, or the user will get prompted for the right
1996 ;; encoding when they send the message.
1997 (setq buffer-file-coding-system
1998 (default-value 'buffer-file-coding-system)))))))))
2000 (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
2001 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
2002 (declare-function dired-view-file "dired" ())
2004 (defun mail-recover ()
2005 "Recover interrupted mail composition from auto-save files.
2007 If the mail buffer has a current valid auto-save file,
2008 the command recovers that file. Otherwise, it displays a
2009 buffer showing the existing auto-saved draft messages;
2010 you can move to one of them and type C-c C-c to recover that one."
2011 (interactive)
2012 ;; In case they invoke us from some random buffer...
2013 (switch-to-buffer "*mail*")
2014 ;; If *mail* didn't exist, set its directory, so that auto-saved
2015 ;; drafts will be found.
2016 (let ((dir (expand-file-name
2017 (file-name-as-directory mail-default-directory))))
2018 (if (file-exists-p dir)
2019 (setq default-directory dir)))
2020 (or (eq major-mode 'mail-mode)
2021 (mail-mode))
2022 (let ((file-name buffer-auto-save-file-name))
2023 (cond ((and file-name (file-exists-p file-name))
2024 (let ((dispbuf
2025 ;; This used to invoke `ls' via call-process, but
2026 ;; dired-noselect is more portable to systems where
2027 ;; `ls' is not a standard program (it will use
2028 ;; ls-lisp instead).
2029 (dired-noselect file-name
2030 (concat dired-listing-switches " -t"))))
2031 (save-selected-window
2032 (switch-to-buffer-other-window dispbuf)
2033 (goto-char (point-min))
2034 (forward-line 2)
2035 (dired-move-to-filename)
2036 (setq dispbuf (rename-buffer "*Directory*" t)))
2037 (if (not (yes-or-no-p
2038 (format "Recover mail draft from auto save file %s? "
2039 file-name)))
2040 (error "mail-recover canceled")
2041 (let ((buffer-read-only nil)
2042 (buffer-coding buffer-file-coding-system)
2043 ;; Auto-save files are written in internal
2044 ;; representation of non-ASCII characters.
2045 (coding-system-for-read 'utf-8-emacs-unix))
2046 (erase-buffer)
2047 (insert-file-contents file-name nil)
2048 (setq buffer-file-coding-system buffer-coding)))))
2049 (t (mail-recover-1)))))
2051 ;;;###autoload
2052 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
2053 "Like `mail' command, but display mail buffer in another window."
2054 (interactive "P")
2055 (switch-to-buffer-other-window "*mail*")
2056 (mail noerase to subject in-reply-to cc replybuffer sendactions))
2058 ;;;###autoload
2059 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
2060 "Like `mail' command, but display mail buffer in another frame."
2061 (interactive "P")
2062 (switch-to-buffer-other-frame "*mail*")
2063 (mail noerase to subject in-reply-to cc replybuffer sendactions))
2065 ;; Do not add anything but external entries on this page.
2067 (provide 'sendmail)
2069 ;; Local Variables:
2070 ;; byte-compile-dynamic: t
2071 ;; End:
2073 ;;; sendmail.el ends here