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