Fix bug in handling GnuPG's TRUST_MARGINAL status
[emacs.git] / lisp / mail / sendmail.el
blob12d69aa23c38b826ad6d535f1420836162b1cdfb
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
559 (format "Send mail via (default %s): " (caar options))
560 options nil 'require-match nil nil (car options))))))
561 (customize-save-variable 'send-mail-function
562 (cdr (assoc-string choice options t)))))
564 (defun sendmail-sync-aliases ()
565 (when mail-personal-alias-file
566 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
567 (or (equal mail-alias-modtime modtime)
568 (setq mail-alias-modtime modtime
569 mail-aliases t)))))
572 ;;;###autoload
573 (define-mail-user-agent 'sendmail-user-agent
574 'sendmail-user-agent-compose
575 'mail-send-and-exit)
577 ;;;###autoload
578 (defun sendmail-user-agent-compose (&optional to subject other-headers
579 continue switch-function yank-action
580 send-actions return-action
581 &rest ignored)
582 (if switch-function
583 (funcall switch-function "*mail*"))
584 (let ((cc (cdr (assoc-string "cc" other-headers t)))
585 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
586 (body (cdr (assoc-string "body" other-headers t))))
587 (or (mail continue to subject in-reply-to cc yank-action
588 send-actions return-action)
589 continue
590 (error "Message aborted"))
591 (save-excursion
592 (rfc822-goto-eoh)
593 (while other-headers
594 (unless (member-ignore-case (car (car other-headers))
595 '("in-reply-to" "cc" "body"))
596 (insert (car (car other-headers)) ": "
597 (cdr (car other-headers))
598 (if use-hard-newlines hard-newline "\n")))
599 (setq other-headers (cdr other-headers)))
600 (when body
601 (forward-line 1)
602 (insert body))
603 t)))
605 (defun mail-setup (to subject in-reply-to cc replybuffer
606 actions return-action)
607 (or mail-default-reply-to
608 (setq mail-default-reply-to (getenv "REPLYTO")))
609 (sendmail-sync-aliases)
610 (when (eq mail-aliases t)
611 (setq mail-aliases nil)
612 (and mail-personal-alias-file
613 (file-exists-p mail-personal-alias-file)
614 (build-mail-aliases)))
615 ;; Don't leave this around from a previous message.
616 (kill-local-variable 'buffer-file-coding-system)
617 ;; This doesn't work for enable-multibyte-characters.
618 ;; (kill-local-variable 'enable-multibyte-characters)
619 (set-buffer-multibyte (default-value 'enable-multibyte-characters))
620 (if current-input-method
621 (deactivate-input-method))
623 ;; Local variables for Mail mode.
624 (setq mail-send-actions actions)
625 (setq mail-reply-action replybuffer)
626 (setq mail-return-action return-action)
628 (goto-char (point-min))
629 (if mail-setup-with-from
630 (mail-insert-from-field))
631 (insert "To: ")
632 (save-excursion
633 (if to
634 ;; Here removed code to extract names from within <...>
635 ;; on the assumption that mail-strip-quoted-names
636 ;; has been called and has done so.
637 (let ((fill-prefix "\t")
638 (address-start (point)))
639 (insert to "\n")
640 (fill-region-as-paragraph address-start (point-max))
641 (goto-char (point-max))
642 (unless (bolp)
643 (newline)))
644 (newline))
645 (if cc
646 (let ((fill-prefix "\t")
647 (address-start (progn (insert "CC: ") (point))))
648 (insert cc "\n")
649 (fill-region-as-paragraph address-start (point-max))
650 (goto-char (point-max))
651 (unless (bolp)
652 (newline))))
653 (if in-reply-to
654 (let ((fill-prefix "\t")
655 (fill-column 78)
656 (address-start (point)))
657 (insert "In-reply-to: " in-reply-to "\n")
658 (fill-region-as-paragraph address-start (point-max))
659 (goto-char (point-max))
660 (unless (bolp)
661 (newline))))
662 (insert "Subject: " (or subject "") "\n")
663 (if mail-default-headers
664 (insert mail-default-headers))
665 (if mail-default-reply-to
666 (insert "Reply-to: " mail-default-reply-to "\n"))
667 (if mail-self-blind
668 (insert "BCC: " user-mail-address "\n"))
669 (if mail-archive-file-name
670 (insert "FCC: " mail-archive-file-name "\n"))
671 (put-text-property (point)
672 (progn
673 (insert mail-header-separator "\n")
674 (1- (point)))
675 'category 'mail-header-separator)
676 ;; Insert the signature. But remember the beginning of the message.
677 (if to (setq to (point)))
678 (if mail-signature (mail-signature t))
679 (goto-char (point-max))
680 (or (bolp) (newline)))
681 (if to (goto-char to))
682 (or to subject in-reply-to
683 (set-buffer-modified-p nil))
684 (run-hooks 'mail-setup-hook))
686 (defcustom mail-mode-hook nil
687 "Hook run by Mail mode.
688 When composing a mail, this runs immediately after creating, or
689 switching to, the `*mail*' buffer. See also `mail-setup-hook'."
690 :group 'sendmail
691 :type 'hook
692 :options '(footnote-mode))
694 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
695 (defvar mail-encode-mml)
696 ;;;###autoload
697 (define-derived-mode mail-mode text-mode "Mail"
698 "Major mode for editing mail to be sent.
699 Like Text Mode but with these additional commands:
701 \\[mail-send] mail-send (send the message)
702 \\[mail-send-and-exit] mail-send-and-exit (send the message and exit)
704 Here are commands that move to a header field (and create it if there isn't):
705 \\[mail-to] move to To: \\[mail-subject] move to Subj:
706 \\[mail-bcc] move to BCC: \\[mail-cc] move to CC:
707 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
708 \\[mail-mail-reply-to] move to Mail-Reply-To:
709 \\[mail-mail-followup-to] move to Mail-Followup-To:
710 \\[mail-text] move to message text.
711 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
712 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
713 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
714 \\[mail-insert-file] insert a text file into the message.
715 \\[mail-add-attachment] attach to the message a file as binary attachment.
716 Turning on Mail mode runs the normal hooks `text-mode-hook' and
717 `mail-mode-hook' (in that order)."
718 (make-local-variable 'mail-reply-action)
719 (make-local-variable 'mail-send-actions)
720 (make-local-variable 'mail-return-action)
721 (make-local-variable 'mail-encode-mml)
722 (setq mail-encode-mml nil)
723 (setq buffer-offer-save t)
724 (make-local-variable 'font-lock-defaults)
725 (setq font-lock-defaults '(mail-font-lock-keywords t t))
726 (make-local-variable 'paragraph-separate)
727 (make-local-variable 'normal-auto-fill-function)
728 (setq normal-auto-fill-function 'mail-mode-auto-fill)
729 (make-local-variable 'fill-paragraph-function)
730 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
731 ;; Allow using comment commands to add/remove quoting (this only does
732 ;; anything if mail-yank-prefix is set to a non-nil value).
733 (set (make-local-variable 'comment-start) mail-yank-prefix)
734 (if mail-yank-prefix
735 (set (make-local-variable 'comment-start-skip)
736 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
737 (make-local-variable 'adaptive-fill-regexp)
738 ;; Also update the paragraph-separate entry if you change this.
739 (setq adaptive-fill-regexp
740 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
741 adaptive-fill-regexp))
742 (make-local-variable 'adaptive-fill-first-line-regexp)
743 (setq adaptive-fill-first-line-regexp
744 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
745 adaptive-fill-first-line-regexp))
746 (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
747 nil 'local)
748 ;; `-- ' precedes the signature. `-----' appears at the start of the
749 ;; lines that delimit forwarded messages.
750 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
751 ;; are also sometimes used and should be separators.
752 (setq paragraph-separate
753 (concat (regexp-quote mail-header-separator)
754 ;; This is based on adaptive-fill-regexp (presumably
755 ;; the idea is to allow navigation etc of cited paragraphs).
756 "$\\|\t*[-–!|#%;>*·•‣⁃◦ ]+$"
757 "\\|[ \t]*[-[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
758 "--\\( \\|-+\\)$\\|"
759 page-delimiter)))
762 (defun mail-header-end ()
763 "Return the buffer location of the end of headers, as a number."
764 (save-restriction
765 (widen)
766 (save-excursion
767 (rfc822-goto-eoh)
768 (point))))
770 (defun mail-text-start ()
771 "Return the buffer location of the start of text, as a number."
772 (save-restriction
773 (widen)
774 (save-excursion
775 (rfc822-goto-eoh)
776 (forward-line 1)
777 (point))))
779 (defun mail-sendmail-delimit-header ()
780 "Set up whatever header delimiter convention sendmail will use.
781 Concretely: replace the first blank line in the header with the separator."
782 (rfc822-goto-eoh)
783 (insert mail-header-separator)
784 (point))
786 (defun mail-sendmail-undelimit-header ()
787 "Remove header separator to put the message in correct form for sendmail.
788 Leave point at the start of the delimiter line."
789 (rfc822-goto-eoh)
790 (delete-region (point) (progn (end-of-line) (point))))
792 (defun mail-mode-auto-fill ()
793 "Carry out Auto Fill for Mail mode.
794 If within the headers, this makes the new lines into continuation lines."
795 (if (< (point) (mail-header-end))
796 (let ((old-line-start (line-beginning-position)))
797 (if (do-auto-fill)
798 (save-excursion
799 (beginning-of-line)
800 (while (not (eq (point) old-line-start))
801 ;; Use insert-before-markers in case we're inserting
802 ;; before the saved value of point (which is common).
803 (insert-before-markers " ")
804 (forward-line -1))
805 t)))
806 (do-auto-fill)))
808 (defun mail-mode-fill-paragraph (arg)
809 ;; Do something special only if within the headers.
810 (if (< (point) (mail-header-end))
811 (let (beg end fieldname)
812 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
813 (setq beg (point)))
814 (setq fieldname
815 (downcase (buffer-substring beg (1- (match-end 0))))))
816 (forward-line 1)
817 ;; Find continuation lines and get rid of their continuation markers.
818 (while (looking-at "[ \t]")
819 (delete-horizontal-space)
820 (forward-line 1))
821 (setq end (point-marker))
822 (goto-char beg)
823 ;; If this field contains addresses,
824 ;; make sure we can fill after each address.
825 (if (member fieldname
826 '("to" "cc" "bcc" "from" "reply-to"
827 "mail-reply-to" "mail-followup-to"
828 "resent-to" "resent-cc" "resent-bcc"
829 "resent-from" "resent-reply-to"))
830 (while (search-forward "," end t)
831 (or (looking-at "[ \t]")
832 (insert " "))))
833 (fill-region-as-paragraph beg end arg)
834 ;; Mark all lines except the first as continuations.
835 (goto-char beg)
836 (forward-line 1)
837 (while (< (point) end)
838 (insert " ")
839 (forward-line 1))
840 (move-marker end nil)
841 t)))
843 ;; User-level commands for sending.
845 (defun mail-send-and-exit (&optional arg)
846 "Send message like `mail-send', then, if no errors, exit from mail buffer.
847 Prefix arg means don't delete this window."
848 (interactive "P")
849 (mail-send)
850 (mail-bury arg))
852 (defun mail-dont-send (&optional arg)
853 "Don't send the message you have been editing.
854 Prefix arg means don't delete this window."
855 (interactive "P")
856 (mail-bury arg))
858 (defun mail-bury (&optional arg)
859 "Bury this mail buffer."
860 (let ((newbuf (other-buffer (current-buffer)))
861 (return-action mail-return-action))
862 (bury-buffer (current-buffer))
863 ;; If there is an Rmail buffer, return to it nicely
864 ;; even if this message was not started by an Rmail command.
865 (unless return-action
866 (dolist (buffer (buffer-list))
867 (if (and (eq (buffer-local-value 'major-mode buffer) 'rmail-mode)
868 (null return-action)
869 ;; Don't match message-viewer buffer.
870 (not (string-match "\\` " (buffer-name buffer))))
871 (setq return-action `(rmail-mail-return ,buffer)))))
872 (if (and (null arg) return-action)
873 (apply (car return-action) (cdr return-action))
874 (switch-to-buffer newbuf))))
876 (defcustom mail-send-hook nil
877 "Hook run just before sending a message."
878 :type 'hook
879 :options '(flyspell-mode-off)
880 :group 'sendmail)
882 ;;;###autoload
883 (defcustom mail-mailing-lists nil
884 "List of mailing list addresses the user is subscribed to.
885 The variable is used to trigger insertion of the \"Mail-Followup-To\"
886 header when sending a message to a mailing list."
887 :type '(repeat string)
888 :group 'sendmail)
890 (declare-function mml-to-mime "mml" ())
892 (defun mail-send ()
893 "Send the message in the current buffer.
894 If `mail-interactive' is non-nil, wait for success indication
895 or error messages, and inform user.
896 Otherwise any failure is reported in a message back to
897 the user from the mailer."
898 (interactive)
899 (if (if buffer-file-name
900 (y-or-n-p "Send buffer contents as mail message? ")
901 (or (buffer-modified-p)
902 (y-or-n-p "Message already sent; resend? ")))
903 (let ((inhibit-read-only t)
904 (opoint (point))
905 (ml (when mail-mailing-lists
906 ;; The surrounding regexp assumes the use of
907 ;; `mail-strip-quoted-names' on addresses before matching
908 ;; Cannot deal with full RFC 822 freedom, but that is
909 ;; unlikely to be problematic.
910 (concat "\\(?:[[:space:];,]\\|\\`\\)"
911 (regexp-opt mail-mailing-lists t)
912 "\\(?:[[:space:];,]\\|\\'\\)"))))
913 (mail-combine-fields "To")
914 (mail-combine-fields "CC")
915 ;; If there are mailing lists defined
916 (when ml
917 (save-excursion
918 (let* ((to (mail-fetch-field "to" nil t))
919 (cc (mail-fetch-field "cc" nil t))
920 (new-header-values ; To: and Cc:
921 (mail-strip-quoted-names
922 (concat to (when cc (concat ", " cc))))))
923 ;; If message goes to known mailing list ...
924 (when (string-match ml new-header-values)
925 ;; Add Mail-Followup-To if none yet
926 (unless (mail-fetch-field "mail-followup-to")
927 (goto-char (mail-header-end))
928 (insert "Mail-Followup-To: "
929 (let ((l))
930 (mapc
931 ;; remove duplicates
932 (lambda (e)
933 (unless (member e l)
934 (push e l)))
935 (split-string new-header-values
936 ",[[:space:]]+" t))
937 (mapconcat 'identity l ", "))
938 "\n"))
939 ;; Add Mail-Reply-To if none yet
940 (unless (mail-fetch-field "mail-reply-to")
941 (goto-char (mail-header-end))
942 (insert "Mail-Reply-To: "
943 (or (mail-fetch-field "reply-to")
944 user-mail-address)
945 "\n"))))))
946 (unless (memq mail-send-nonascii '(t mime))
947 (goto-char (point-min))
948 (skip-chars-forward "\0-\177")
949 (or (= (point) (point-max))
950 (if (eq mail-send-nonascii 'query)
951 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
952 (error "Aborted"))
953 (error "Message contains non-ASCII characters"))))
954 ;; Complain about any invalid line.
955 (goto-char (point-min))
956 (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
957 (let ((header-end (or (match-beginning 0) (point-max))))
958 (goto-char (point-min))
959 (while (< (point) header-end)
960 (unless (looking-at "[ \t]\\|.*:\\|$")
961 (push-mark opoint)
962 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
963 (forward-line 1)))
964 (goto-char opoint)
965 (when mail-encode-mml
966 (mml-to-mime)
967 (setq mail-encode-mml nil))
968 (run-hooks 'mail-send-hook)
969 (message "Sending...")
970 (funcall send-mail-function)
971 ;; Now perform actions on successful sending.
972 (while mail-send-actions
973 (condition-case nil
974 (apply (car (car mail-send-actions))
975 (cdr (car mail-send-actions)))
976 (error))
977 (setq mail-send-actions (cdr mail-send-actions)))
978 (message "Sending...done")
979 ;; If buffer has no file, mark it as unmodified and delete auto-save.
980 (if (not buffer-file-name)
981 (progn
982 (set-buffer-modified-p nil)
983 (delete-auto-save-file-if-necessary t))))))
985 (defun mail-envelope-from ()
986 "Return the envelope mail address to use when sending mail.
987 This function uses `mail-envelope-from'."
988 (if (eq mail-envelope-from 'header)
989 (nth 1 (mail-extract-address-components
990 (mail-fetch-field "From")))
991 mail-envelope-from))
993 ;; This does the real work of sending a message via sendmail.
994 ;; It is called via the variable send-mail-function.
996 ;;;###autoload
997 (defvar sendmail-coding-system nil
998 "Coding system for encoding the outgoing mail.
999 This has higher priority than the default `buffer-file-coding-system'
1000 and `default-sendmail-coding-system',
1001 but lower priority than the local value of `buffer-file-coding-system'.
1002 See also the function `select-message-coding-system'.")
1004 ;;;###autoload
1005 (defvar default-sendmail-coding-system 'iso-latin-1
1006 "Default coding system for encoding the outgoing mail.
1007 This variable is used only when `sendmail-coding-system' is nil.
1009 This variable is set/changed by the command `set-language-environment'.
1010 User should not set this variable manually,
1011 instead use `sendmail-coding-system' to get a constant encoding
1012 of outgoing mails regardless of the current language environment.
1013 See also the function `select-message-coding-system'.")
1015 (defun mail-insert-from-field ()
1016 "Insert the \"From:\" field of a mail header.
1017 The style of the field is determined by the variable `mail-from-style'.
1018 This function does not perform RFC2047 encoding."
1019 (let* ((login user-mail-address)
1020 (fullname (user-full-name))
1021 (quote-fullname nil))
1022 (if (string-match "[^\0-\177]" fullname)
1023 (setq quote-fullname t))
1024 (cond ((null mail-from-style)
1025 (insert "From: " login "\n"))
1026 ;; This is deprecated.
1027 ((eq mail-from-style 'system-default)
1028 nil)
1029 ((or (eq mail-from-style 'angles)
1030 (and (not (eq mail-from-style 'parens))
1031 ;; Use angles if no quoting is needed, or if
1032 ;; parens would need quoting too.
1033 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1034 (let ((tmp (concat fullname nil)))
1035 (while (string-match "([^()]*)" tmp)
1036 (aset tmp (match-beginning 0) ?-)
1037 (aset tmp (1- (match-end 0)) ?-))
1038 (string-match "[\\()]" tmp)))))
1039 (insert "From: " fullname)
1040 (let ((fullname-start (+ (point-min) 6))
1041 (fullname-end (point-marker)))
1042 (goto-char fullname-start)
1043 ;; Look for a character that cannot appear unquoted
1044 ;; according to RFC 822.
1045 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
1046 fullname-end 1)
1047 quote-fullname)
1048 (progn
1049 ;; Quote fullname, escaping specials.
1050 (goto-char fullname-start)
1051 (insert "\"")
1052 (while (re-search-forward "[\"\\]"
1053 fullname-end 1)
1054 (replace-match "\\\\\\&" t))
1055 (insert "\""))))
1056 (insert " <" login ">\n"))
1057 ;; 'parens or default
1059 (insert "From: " login " (")
1060 (let ((fullname-start (point)))
1061 (if quote-fullname
1062 (insert "\""))
1063 (insert fullname)
1064 (if quote-fullname
1065 (insert "\""))
1066 (let ((fullname-end (point-marker)))
1067 (goto-char fullname-start)
1068 ;; RFC 822 says \ and nonmatching parentheses
1069 ;; must be escaped in comments.
1070 ;; Escape every instance of ()\ ...
1071 (while (re-search-forward "[()\\]" fullname-end 1)
1072 (replace-match "\\\\\\&" t))
1073 ;; ... then undo escaping of matching parentheses,
1074 ;; including matching nested parentheses.
1075 (goto-char fullname-start)
1076 (while (re-search-forward
1077 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1078 fullname-end 1)
1079 (replace-match "\\1(\\3)" t)
1080 (goto-char fullname-start))))
1081 (insert ")\n")))))
1083 (defun mail-combine-fields (field)
1084 "Offer to combine all FIELD fields in buffer into one FIELD field.
1085 If this finds multiple FIELD fields, it asks the user whether
1086 to combine them into one, and does so if the user says y."
1087 (let ((search-pattern (format "^%s[ \t]*:" field))
1088 first-to-end
1089 query-asked
1090 query-answer
1091 (old-point (point))
1092 (old-max (point-max)))
1093 (save-excursion
1094 (save-restriction
1095 (goto-char (point-min))
1096 (narrow-to-region (point-min) (mail-header-end))
1097 ;; Find the first FIELD field and record where it ends.
1098 (when (re-search-forward search-pattern nil t)
1099 (forward-line 1)
1100 (re-search-forward "^[^ \t]" nil t)
1101 (beginning-of-line)
1102 (setq first-to-end (point-marker))
1103 (set-marker-insertion-type first-to-end t)
1104 ;; Find each following FIELD field
1105 ;; and combine it with the first FIELD field.
1106 (while (re-search-forward search-pattern nil t)
1107 ;; For the second FIELD field, ask user to
1108 ;; approve combining them.
1109 ;; But if the user refuse to combine them, signal error.
1110 (unless query-asked
1111 (save-restriction
1112 ;; This is just so the screen doesn't change.
1113 (narrow-to-region (point-min) old-max)
1114 (save-excursion
1115 (goto-char old-point)
1116 (setq query-asked t)
1117 (if (y-or-n-p (format "Message contains multiple %s fields. Combine? " field))
1118 (setq query-answer t)))))
1119 (when query-answer
1120 (let ((this-to-start (line-beginning-position))
1121 this-to-end
1122 this-to)
1123 (forward-line 1)
1124 (re-search-forward "^[^ \t]" nil t)
1125 (beginning-of-line)
1126 (setq this-to-end (point))
1127 ;; Get the text of this FIELD field.
1128 (setq this-to (buffer-substring this-to-start this-to-end))
1129 ;; Delete it.
1130 (delete-region this-to-start this-to-end)
1131 (save-excursion
1132 ;; Put a comma after the first FIELD field.
1133 (goto-char first-to-end)
1134 (forward-char -1)
1135 (insert ",")
1136 ;; Copy this one after it.
1137 (goto-char first-to-end)
1138 (save-excursion
1139 (insert this-to))
1140 ;; Replace the FIELD: with spaces.
1141 (looking-at search-pattern)
1142 ;; Try to preserve alignment of contents of the field
1143 (let ((prefix-length (length (match-string 0))))
1144 (replace-match " ")
1145 (dotimes (i (1- prefix-length))
1146 (insert " ")))))))
1147 (set-marker first-to-end nil))))))
1149 (defun mail-encode-header (beg end)
1150 "Encode the mail header between BEG and END according to RFC2047.
1151 Return non-nil if and only if some part of the header is encoded."
1152 (save-restriction
1153 (narrow-to-region beg end)
1154 (let* ((selected (select-message-coding-system))
1155 (mm-coding-system-priorities
1156 (if (and selected (coding-system-get selected :mime-charset))
1157 (cons selected mm-coding-system-priorities)
1158 mm-coding-system-priorities))
1159 (tick (buffer-chars-modified-tick))
1160 ;; Many mailers, including Gnus, passes a message of which
1161 ;; the header is already encoded, so this is necessary to
1162 ;; prevent it from being encoded again.
1163 (rfc2047-encode-encoded-words nil))
1164 (rfc2047-encode-message-header)
1165 (= tick (buffer-chars-modified-tick)))))
1167 ;; Normally you will not need to modify these options unless you are
1168 ;; using some non-genuine substitute for sendmail which does not
1169 ;; implement each and every option that the original supports.
1170 ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1171 ;; you will need to modify `sendmail-error-reporting-non-interactive'
1172 ;; in your site-init.el.
1173 (defvar sendmail-error-reporting-interactive
1174 ;; These mean "report errors to terminal" and "deliver interactively"
1175 '("-oep" "-odi"))
1176 (defvar sendmail-error-reporting-non-interactive
1177 ;; These mean "report errors by mail" and "deliver in background".
1178 '("-oem" "-odb"))
1180 (defun sendmail-send-it ()
1181 "Send the current mail buffer using the Sendmail package.
1182 This is a suitable value for `send-mail-function'. It sends using the
1183 external program defined by `sendmail-program'."
1184 (require 'mail-utils)
1185 (let ((errbuf (if mail-interactive
1186 (generate-new-buffer " sendmail errors")
1188 (error nil)
1189 (tembuf (generate-new-buffer " sendmail temp"))
1190 (multibyte enable-multibyte-characters)
1191 (case-fold-search nil)
1192 (selected-coding (select-message-coding-system))
1193 resend-to-addresses
1194 delimline
1195 fcc-was-found
1196 (mailbuf (current-buffer))
1197 ;; Examine these variables now, so that
1198 ;; local binding in the mail buffer will take effect.
1199 (envelope-from
1200 (and mail-specify-envelope-from
1201 (or (mail-envelope-from) user-mail-address))))
1202 (unwind-protect
1203 (with-current-buffer tembuf
1204 (erase-buffer)
1205 (unless multibyte
1206 (set-buffer-multibyte nil))
1207 (insert-buffer-substring mailbuf)
1208 (set-buffer-file-coding-system selected-coding)
1209 (goto-char (point-max))
1210 ;; require one newline at the end.
1211 (or (= (preceding-char) ?\n)
1212 (insert ?\n))
1213 ;; Change header-delimiter to be what sendmail expects.
1214 (goto-char (mail-header-end))
1215 (delete-region (point) (progn (end-of-line) (point)))
1216 (setq delimline (point-marker))
1217 (sendmail-sync-aliases)
1218 (if mail-aliases
1219 (expand-mail-aliases (point-min) delimline))
1220 (goto-char (point-min))
1221 ;; Ignore any blank lines in the header
1222 (while (and (re-search-forward "\n\n\n*" delimline t)
1223 (< (point) delimline))
1224 (replace-match "\n"))
1225 (goto-char (point-min))
1226 ;; Look for Resent- headers. They require sending
1227 ;; the message specially.
1228 (let ((case-fold-search t))
1229 (goto-char (point-min))
1230 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1231 ;; Put a list of such addresses in resend-to-addresses.
1232 (setq resend-to-addresses
1233 (save-restriction
1234 (narrow-to-region (point)
1235 (save-excursion
1236 (forward-line 1)
1237 (while (looking-at "^[ \t]")
1238 (forward-line 1))
1239 (point)))
1240 (append (mail-parse-comma-list)
1241 resend-to-addresses)))
1242 ;; Delete Resent-BCC ourselves
1243 (if (save-excursion (beginning-of-line)
1244 (looking-at "resent-bcc"))
1245 (delete-region (line-beginning-position)
1246 (line-beginning-position 2))))
1247 ;; Apparently this causes a duplicate Sender.
1248 ;; ;; If the From is different than current user, insert Sender.
1249 ;; (goto-char (point-min))
1250 ;; (and (re-search-forward "^From:" delimline t)
1251 ;; (progn
1252 ;; (require 'mail-utils)
1253 ;; (not (string-equal
1254 ;; (mail-strip-quoted-names
1255 ;; (save-restriction
1256 ;; (narrow-to-region (point-min) delimline)
1257 ;; (mail-fetch-field "From")))
1258 ;; (user-login-name))))
1259 ;; (progn
1260 ;; (forward-line 1)
1261 ;; (insert "Sender: " (user-login-name) "\n")))
1262 ;; Don't send out a blank subject line
1263 (goto-char (point-min))
1264 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1265 (replace-match "")
1266 ;; This one matches a Subject just before the header delimiter.
1267 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1268 (= (match-end 0) delimline))
1269 (replace-match "")))
1270 ;; Put the "From:" field in unless for some odd reason
1271 ;; they put one in themselves.
1272 (goto-char (point-min))
1273 (if (not (re-search-forward "^From:" delimline t))
1274 (mail-insert-from-field))
1275 ;; Possibly add a MIME header for the current coding system
1276 (let (charset where-content-type)
1277 (goto-char (point-min))
1278 (setq where-content-type
1279 (re-search-forward "^Content-type:" delimline t))
1280 (goto-char (point-min))
1281 (and (eq mail-send-nonascii 'mime)
1282 (not (re-search-forward "^MIME-version:" delimline t))
1283 (progn (skip-chars-forward "\0-\177")
1284 (/= (point) (point-max)))
1285 selected-coding
1286 (setq charset
1287 (coding-system-get selected-coding :mime-charset))
1288 (progn
1289 (goto-char delimline)
1290 (insert "MIME-version: 1.0\n"
1291 "Content-type: text/plain; charset="
1292 (symbol-name charset)
1293 "\nContent-Transfer-Encoding: 8bit\n")
1294 ;; The character set we will actually use
1295 ;; should override any specified in the message itself.
1296 (when where-content-type
1297 (goto-char where-content-type)
1298 (delete-region (point-at-bol)
1299 (progn (forward-line 1) (point)))))))
1300 ;; Insert an extra newline if we need it to work around
1301 ;; Sun's bug that swallows newlines.
1302 (goto-char (1+ delimline))
1303 (if (eval mail-mailer-swallows-blank-line)
1304 (newline))
1305 ;; Find and handle any FCC fields.
1306 (goto-char (point-min))
1307 (if (re-search-forward "^FCC:" delimline t)
1308 (progn
1309 (setq fcc-was-found t)
1310 (mail-do-fcc delimline)))
1311 (if mail-interactive
1312 (with-current-buffer errbuf
1313 (erase-buffer))))
1314 ;; Encode the header according to RFC2047.
1315 (mail-encode-header (point-min) delimline)
1316 (goto-char (point-min))
1317 (if (let ((case-fold-search t))
1318 (or resend-to-addresses
1319 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1320 delimline t)))
1321 (let* ((default-directory "/")
1322 (coding-system-for-write selected-coding)
1323 (args
1324 (append (list (point-min) (point-max)
1325 sendmail-program
1326 nil errbuf nil "-oi")
1327 (and envelope-from
1328 (list "-f" envelope-from))
1329 ;; ;; Don't say "from root" if running under su.
1330 ;; (and (equal (user-real-login-name) "root")
1331 ;; (list "-f" (user-login-name)))
1332 (and mail-alias-file
1333 (list (concat "-oA" mail-alias-file)))
1334 (if mail-interactive
1335 sendmail-error-reporting-interactive
1336 sendmail-error-reporting-non-interactive)
1337 ;; Get the addresses from the message
1338 ;; unless this is a resend.
1339 ;; We must not do that for a resend
1340 ;; because we would find the original addresses.
1341 ;; For a resend, include the specific addresses.
1342 (or resend-to-addresses
1343 '("-t")
1345 (if mail-use-dsn
1346 (list "-N" (mapconcat 'symbol-name
1347 mail-use-dsn ",")))
1350 (exit-value (apply 'call-process-region args)))
1351 (cond ((or (null exit-value) (eq 0 exit-value)))
1352 ((numberp exit-value)
1353 (setq error t)
1354 (error "Sending...failed with exit value %d" exit-value))
1355 ((stringp exit-value)
1356 (setq error t)
1357 (error "Sending...terminated by signal: %s" exit-value))
1359 (setq error t)
1360 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
1361 (or fcc-was-found
1362 (error "No recipients")))
1363 (if mail-interactive
1364 (with-current-buffer errbuf
1365 (goto-char (point-min))
1366 (while (re-search-forward "\n\n* *" nil t)
1367 (replace-match "; "))
1368 (unless (zerop (buffer-size))
1369 (setq error t)
1370 (error "Sending...failed to %s"
1371 (buffer-substring (point-min) (point-max)))))))
1372 (kill-buffer tembuf)
1373 (when (buffer-live-p errbuf)
1374 (if error
1375 (switch-to-buffer-other-window errbuf)
1376 (kill-buffer errbuf))))))
1378 (autoload 'rmail-output-to-rmail-buffer "rmailout")
1380 (defun mail-do-fcc (header-end)
1381 "Find and act on any FCC: headers in the current message before HEADER-END.
1382 If a buffer is visiting the FCC file, append to it before
1383 offering to save it, if it was modified initially. If this is an
1384 Rmail buffer, update Rmail as needed. If there is no buffer,
1385 just append to the file, in Babyl format if necessary."
1386 (unless (markerp header-end)
1387 (error "Value of `header-end' must be a marker"))
1388 (let (fcc-list
1389 (mailbuf (current-buffer))
1390 (time (current-time)))
1391 (save-excursion
1392 (goto-char (point-min))
1393 (let ((case-fold-search t))
1394 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1395 (push (buffer-substring (point)
1396 (progn
1397 (end-of-line)
1398 (skip-chars-backward " \t")
1399 (point)))
1400 fcc-list)
1401 (delete-region (match-beginning 0)
1402 (progn (forward-line 1) (point)))))
1403 (with-temp-buffer
1404 ;; This initial newline is not written out if we create a new
1405 ;; file (see below).
1406 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1407 ;; Insert the time zone before the year.
1408 (forward-char -1)
1409 (forward-word-strictly -1)
1410 (require 'mail-utils)
1411 (insert (mail-rfc822-time-zone time) " ")
1412 (goto-char (point-max))
1413 (insert "Date: " (message-make-date) "\n")
1414 (insert-buffer-substring mailbuf)
1415 ;; Make sure messages are separated.
1416 (goto-char (point-max))
1417 (insert ?\n)
1418 (goto-char 2)
1419 ;; ``Quote'' "^From " as ">From "
1420 ;; (note that this isn't really quoting, as there is no requirement
1421 ;; that "^[>]+From " be quoted in the same transparent way.)
1422 (let ((case-fold-search nil))
1423 (while (search-forward "\nFrom " nil t)
1424 (forward-char -5)
1425 (insert ?>)))
1426 (dolist (fcc fcc-list)
1427 (let* ((buffer (find-buffer-visiting fcc))
1428 (curbuf (current-buffer))
1429 dont-write-the-file
1430 buffer-matches-file
1431 (beg (point-min)) ; the initial blank line
1432 (end (point-max))
1433 ;; After the ^From line.
1434 (beg2 (save-excursion (goto-char (point-min))
1435 (forward-line 2) (point))))
1436 (if buffer
1437 ;; File is present in a buffer => append to that buffer.
1438 (with-current-buffer buffer
1439 (setq buffer-matches-file
1440 (and (not (buffer-modified-p))
1441 (verify-visited-file-modtime buffer)))
1442 (let ((msg (bound-and-true-p rmail-current-message))
1443 (buffer-read-only nil))
1444 ;; If MSG is non-nil, buffer is in Rmail mode.
1445 (if msg
1446 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1447 (unwind-protect
1448 (progn
1449 (with-current-buffer buff
1450 (insert-buffer-substring curbuf (1+ beg) end))
1451 (rmail-output-to-rmail-buffer buff msg))
1452 (kill-buffer buff)))
1453 ;; Output file not in Rmail mode => just insert
1454 ;; at the end.
1455 (save-restriction
1456 (widen)
1457 (goto-char (point-max))
1458 (insert-buffer-substring curbuf beg end)))
1459 ;; Offer to save the buffer if it was modified
1460 ;; before we started.
1461 (unless buffer-matches-file
1462 (if (y-or-n-p (format "Save file %s? " fcc))
1463 (save-buffer))
1464 (setq dont-write-the-file t)))))
1465 ;; Append to the file directly, unless we've already taken
1466 ;; care of it.
1467 (unless dont-write-the-file
1468 (if (and (file-exists-p fcc)
1469 (mail-file-babyl-p fcc))
1470 ;; If the file is a Babyl file, convert the message to
1471 ;; Babyl format. Even though Rmail no longer uses
1472 ;; Babyl, this code can remain for the time being, on
1473 ;; the off-chance one FCCs to a Babyl file that has
1474 ;; not yet been converted to mbox.
1475 (let ((coding-system-for-write
1476 (or rmail-file-coding-system 'emacs-mule)))
1477 (with-temp-buffer
1478 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1479 (mail-rfc822-date) "\n")
1480 (insert-buffer-substring curbuf beg2 end)
1481 (insert "\n\C-_")
1482 (write-region (point-min) (point-max) fcc t)))
1483 ;; Ensure there is a blank line between messages, but
1484 ;; not at the very start of the file.
1485 (write-region (if (file-exists-p fcc)
1486 (point-min)
1487 (1+ (point-min)))
1488 (point-max) fcc t)))
1489 (and buffer (not dont-write-the-file)
1490 (with-current-buffer buffer
1491 (set-visited-file-modtime)))))))))
1493 (defun mail-sent-via ()
1494 "Make a Sent-via header line from each To or CC header line."
1495 (declare (obsolete "nobody can remember what it is for." "24.1"))
1496 (interactive)
1497 (save-excursion
1498 ;; put a marker at the end of the header
1499 (let ((end (copy-marker (mail-header-end)))
1500 (case-fold-search t))
1501 (goto-char (point-min))
1502 ;; search for the To: lines and make Sent-via: lines from them
1503 ;; search for the next To: line
1504 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1505 ;; Grab this line plus all its continuations, sans the `to:'.
1506 (let ((to-line
1507 (buffer-substring (point)
1508 (progn
1509 (if (re-search-forward "^[^ \t\n]" end t)
1510 (backward-char 1)
1511 (goto-char end))
1512 (point)))))
1513 ;; Insert a copy, with altered header field name.
1514 (insert-before-markers "Sent-via:" to-line))))))
1516 (defun mail-to ()
1517 "Move point to end of To field, creating it if necessary."
1518 (interactive)
1519 (expand-abbrev)
1520 (mail-position-on-field "To"))
1522 (defun mail-subject ()
1523 "Move point to end of Subject field, creating it if necessary."
1524 (interactive)
1525 (expand-abbrev)
1526 (mail-position-on-field "Subject"))
1528 (defun mail-cc ()
1529 "Move point to end of CC field, creating it if necessary."
1530 (interactive)
1531 (expand-abbrev)
1532 (or (mail-position-on-field "cc" t)
1533 (progn (mail-position-on-field "to")
1534 (insert "\nCC: "))))
1536 (defun mail-bcc ()
1537 "Move point to end of BCC field, creating it if necessary."
1538 (interactive)
1539 (expand-abbrev)
1540 (or (mail-position-on-field "bcc" t)
1541 (progn (mail-position-on-field "to")
1542 (insert "\nBCC: "))))
1544 (defun mail-fcc (folder)
1545 "Add a new FCC field, with file name completion."
1546 (interactive "FFolder carbon copy: ")
1547 (expand-abbrev)
1548 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1549 (mail-position-on-field "to"))
1550 (insert "\nFCC: " folder))
1552 (defun mail-reply-to ()
1553 "Move point to end of Reply-To field, creating it if necessary."
1554 (interactive)
1555 (expand-abbrev)
1556 (mail-position-on-field "Reply-To"))
1558 (defun mail-mail-reply-to ()
1559 "Move point to end of Mail-Reply-To field, creating it if necessary."
1560 (interactive)
1561 (expand-abbrev)
1562 (or (mail-position-on-field "mail-reply-to" t)
1563 (progn (mail-position-on-field "to")
1564 (insert "\nMail-Reply-To: "))))
1566 (defun mail-mail-followup-to ()
1567 "Move point to end of Mail-Followup-To field, creating it if necessary."
1568 (interactive)
1569 (expand-abbrev)
1570 (or (mail-position-on-field "mail-followup-to" t)
1571 (progn (mail-position-on-field "to")
1572 (insert "\nMail-Followup-To: "))))
1574 (defun mail-position-on-field (field &optional soft)
1575 "Move to the end of the contents of header field FIELD.
1576 If there is no such header, insert one, unless SOFT is non-nil.
1577 If there are multiple FIELD fields, this goes to the first.
1578 Returns non-nil if FIELD was originally present."
1579 (let (end
1580 (case-fold-search t))
1581 (setq end (mail-header-end))
1582 (goto-char (point-min))
1583 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1584 (progn
1585 (re-search-forward "^[^ \t]" nil 'move)
1586 (beginning-of-line)
1587 (skip-chars-backward "\n")
1589 (or soft
1590 (progn (goto-char end)
1591 (insert field ": \n")
1592 (skip-chars-backward "\n")))
1593 nil)))
1595 (defun mail-text ()
1596 "Move point to beginning of text field."
1597 (interactive)
1598 (expand-abbrev)
1599 (goto-char (mail-text-start)))
1601 (defun mail-signature (&optional atpoint)
1602 "Sign letter with signature.
1603 If the variable `mail-signature' is a string, inserts it.
1604 If it is t or nil, inserts the contents of the file `mail-signature-file'.
1605 Otherwise, evals `mail-signature'.
1606 Prefix argument ATPOINT means insert at point rather than the end."
1607 (interactive "*P")
1608 ;; Test for an unreadable file here, before we delete trailing
1609 ;; whitespace, so that we don't modify the buffer needlessly.
1610 (if (and (memq mail-signature '(t nil))
1611 (not (file-readable-p mail-signature-file)))
1612 (if (called-interactively-p 'interactive)
1613 (message "The signature file `%s' could not be read"
1614 mail-signature-file))
1615 (save-excursion
1616 (unless atpoint
1617 (goto-char (point-max))
1618 ;; Delete trailing whitespace and blank lines.
1619 (skip-chars-backward " \t\n")
1620 (end-of-line)
1621 (delete-region (point) (point-max)))
1622 (cond ((stringp mail-signature)
1623 (insert mail-signature))
1624 ((memq mail-signature '(t nil))
1625 (insert "\n\n-- \n")
1626 (insert-file-contents (expand-file-name mail-signature-file)))
1628 ;; FIXME add condition-case error handling?
1629 (eval mail-signature))))))
1631 (defun mail-fill-yanked-message (&optional justifyp)
1632 "Fill the paragraphs of a message yanked into this one.
1633 Numeric argument means justify as well."
1634 (interactive "P")
1635 (save-excursion
1636 (goto-char (mail-text-start))
1637 (fill-individual-paragraphs (point)
1638 (point-max)
1639 justifyp
1640 mail-citation-prefix-regexp)))
1642 (defun mail-indent-citation ()
1643 "Modify text just inserted from a message to be cited.
1644 The inserted text should be the region.
1645 When this function returns, the region is again around the modified text.
1647 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1648 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1649 (mail-yank-clear-headers (region-beginning) (region-end))
1650 (if (null mail-yank-prefix)
1651 (indent-rigidly (region-beginning) (region-end)
1652 mail-indentation-spaces)
1653 (save-excursion
1654 (let ((end (set-marker (make-marker) (region-end))))
1655 (goto-char (region-beginning))
1656 (while (< (point) end)
1657 (insert mail-yank-prefix)
1658 (forward-line 1))))))
1660 (defun mail-yank-original (arg)
1661 "Insert the message being replied to, if any (in Rmail).
1662 Puts point after the text and mark before.
1663 Normally, indents each nonblank line ARG spaces (default 3).
1664 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1666 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1667 and don't delete any header fields."
1668 (interactive "P")
1669 (if mail-reply-action
1670 (let ((start (point))
1671 (original mail-reply-action)
1672 (omark (mark t)))
1673 (and (consp original) (eq (car original) 'insert-buffer)
1674 (setq original (nth 1 original)))
1675 (if (consp original)
1676 (progn
1677 ;; Call yank function, and set the mark if it doesn't.
1678 (apply (car original) (cdr original))
1679 (if (eq omark (mark t))
1680 (push-mark)))
1681 ;; If the original message is in another window in the same
1682 ;; frame, delete that window to save space.
1683 (delete-windows-on original t)
1684 (with-no-warnings
1685 ;; We really want this to set mark.
1686 (insert-buffer original)
1687 ;; If they yank the original text, the encoding of the
1688 ;; original message is a better default than
1689 ;; the default buffer-file-coding-system.
1690 (and (coding-system-equal
1691 (default-value 'buffer-file-coding-system)
1692 buffer-file-coding-system)
1693 (setq buffer-file-coding-system
1694 (coding-system-change-text-conversion
1695 buffer-file-coding-system
1696 (coding-system-base
1697 (with-current-buffer original
1698 buffer-file-coding-system))))))
1699 (set-text-properties (point) (mark t) nil))
1700 (if (consp arg)
1702 (goto-char start)
1703 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1704 mail-indentation-spaces))
1705 ;; Avoid error in Transient Mark mode
1706 ;; on account of mark's being inactive.
1707 (mark-even-if-inactive t))
1708 (cond (mail-citation-hook
1709 ;; Bind mail-citation-header to the inserted
1710 ;; message's header.
1711 (let ((mail-citation-header
1712 (buffer-substring-no-properties
1713 start
1714 (save-excursion
1715 (save-restriction
1716 (narrow-to-region start (point-max))
1717 (goto-char start)
1718 (rfc822-goto-eoh)
1719 (point))))))
1720 (run-hooks 'mail-citation-hook)))
1721 (mail-yank-hooks
1722 (run-hooks 'mail-yank-hooks))
1724 (mail-indent-citation)))))
1725 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1726 ;; It is cleaner to avoid activation, even though the command
1727 ;; loop would deactivate the mark because we inserted text.
1728 (goto-char (prog1 (mark t)
1729 (set-marker (mark-marker) (point) (current-buffer))))
1730 (if (not (eolp)) (insert ?\n)))))
1732 (defun mail-yank-clear-headers (start end)
1733 (if (< end start)
1734 (let (temp)
1735 (setq temp start start end end temp)))
1736 (if mail-yank-ignored-headers
1737 (save-excursion
1738 (goto-char start)
1739 (if (search-forward "\n\n" end t)
1740 (save-restriction
1741 (narrow-to-region start (point))
1742 (goto-char start)
1743 (while (let ((case-fold-search t))
1744 (re-search-forward mail-yank-ignored-headers nil t))
1745 (beginning-of-line)
1746 (delete-region (point)
1747 (progn (re-search-forward "\n[^ \t]")
1748 (forward-char -1)
1749 (point)))))))))
1751 (defun mail-yank-region (arg)
1752 "Insert the selected region from the message being replied to.
1753 Puts point after the text and mark before.
1754 Normally, indents each nonblank line ARG spaces (default 3).
1755 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1757 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1758 and don't delete any header fields."
1759 (interactive "P")
1760 (and (consp mail-reply-action)
1761 (memq (car mail-reply-action)
1762 '(rmail-yank-current-message insert-buffer))
1763 (with-current-buffer (nth 1 mail-reply-action)
1764 (or (mark t)
1765 (error "No mark set: %S" (current-buffer))))
1766 (let ((buffer (nth 1 mail-reply-action))
1767 (start (point))
1768 ;; Avoid error in Transient Mark mode
1769 ;; on account of mark's being inactive.
1770 (mark-even-if-inactive t))
1771 ;; Insert the citation text.
1772 (insert (with-current-buffer buffer
1773 (buffer-substring-no-properties (point) (mark))))
1774 (push-mark start)
1775 ;; Indent or otherwise annotate the citation text.
1776 (if (consp arg)
1778 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1779 mail-indentation-spaces)))
1780 (if mail-citation-hook
1781 ;; Bind mail-citation-header to the original message's header.
1782 (let ((mail-citation-header
1783 (with-current-buffer buffer
1784 (buffer-substring-no-properties
1785 (point-min)
1786 (save-excursion
1787 (goto-char (point-min))
1788 (rfc822-goto-eoh)
1789 (point))))))
1790 (run-hooks 'mail-citation-hook))
1791 (if mail-yank-hooks
1792 (run-hooks 'mail-yank-hooks)
1793 (mail-indent-citation))))))))
1795 (defun mail-split-line ()
1796 "Split current line, moving portion beyond point vertically down.
1797 If the current line has `mail-yank-prefix', insert it on the new line."
1798 (interactive "*")
1799 (split-line mail-yank-prefix))
1802 (defun mail-insert-file (&optional file)
1803 "Insert a file at the end of the buffer, with separator lines around it."
1804 (interactive "fAttach file: ")
1805 (save-excursion
1806 (goto-char (point-max))
1807 (or (bolp) (newline))
1808 (newline)
1809 (let ((start (point))
1810 middle)
1811 (insert (format "===File %s===" file))
1812 (insert-char ?= (max 0 (- 60 (current-column))))
1813 (newline)
1814 (setq middle (point))
1815 (insert "============================================================\n")
1816 (push-mark)
1817 (goto-char middle)
1818 (insert-file-contents file)
1819 (or (bolp) (newline))
1820 (goto-char start))))
1822 (define-obsolete-function-alias 'mail-attach-file 'mail-insert-file "24.1")
1824 (declare-function mml-attach-file "mml"
1825 (file &optional type description disposition))
1826 (declare-function mm-default-file-encoding "mm-encode" (file))
1828 (defun mail-add-attachment (file)
1829 "Add FILE as a MIME attachment to the end of the mail message being composed."
1830 (interactive "fAttach file: ")
1831 (mml-attach-file file
1832 (or (mm-default-file-encoding file)
1833 "application/octet-stream") nil)
1834 (setq mail-encode-mml t))
1837 ;; Put these commands last, to reduce chance of lossage from quitting
1838 ;; in middle of loading the file.
1840 ;;;###autoload
1841 (defun mail (&optional noerase to subject in-reply-to cc replybuffer
1842 actions return-action)
1843 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1844 When this function returns, the buffer `*mail*' is selected.
1845 The value is t if the message was newly initialized; otherwise, nil.
1847 Optionally, the signature file `mail-signature-file' can be inserted at the
1848 end; see the variable `mail-signature'.
1850 \\<mail-mode-map>
1851 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1853 Various special commands starting with C-c are available in sendmail mode
1854 to move to message header fields:
1855 \\{mail-mode-map}
1857 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1858 when the message is initialized.
1860 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1861 a Reply-to: field with that address is inserted.
1863 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1864 is inserted.
1866 The normal hook `mail-setup-hook' is run after the message is
1867 initialized. It can add more default fields to the message.
1869 The first argument, NOERASE, determines what to do when there is
1870 an existing modified `*mail*' buffer. If NOERASE is nil, the
1871 existing mail buffer is used, and the user is prompted whether to
1872 keep the old contents or to erase them. If NOERASE has the value
1873 `new', a new mail buffer will be created instead of using the old
1874 one. Any other non-nil value means to always select the old
1875 buffer without erasing the contents.
1877 The second through fifth arguments,
1878 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1879 the initial contents of those header fields.
1880 These arguments should not have final newlines.
1881 The sixth argument REPLYBUFFER is a buffer which contains an
1882 original message being replied to, or else an action
1883 of the form (FUNCTION . ARGS) which says how to insert the original.
1884 Or it can be nil, if not replying to anything.
1885 The seventh argument ACTIONS is a list of actions to take
1886 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1887 when the message is sent, we apply FUNCTION to ARGS.
1888 This is how Rmail arranges to mark messages `answered'."
1889 (interactive "P")
1890 (if (eq noerase 'new)
1891 (pop-to-buffer-same-window (generate-new-buffer "*mail*"))
1892 (and noerase
1893 (not (get-buffer "*mail*"))
1894 (setq noerase nil))
1895 (pop-to-buffer-same-window "*mail*"))
1897 ;; Avoid danger that the auto-save file can't be written.
1898 (let ((dir (expand-file-name
1899 (file-name-as-directory mail-default-directory))))
1900 (if (file-exists-p dir)
1901 (setq default-directory dir)))
1902 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1903 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1904 (and (not auto-save-default) buffer-auto-save-file-name))
1905 (auto-save-mode auto-save-default))
1906 (mail-mode)
1907 ;; Disconnect the buffer from its visited file
1908 ;; (in case the user has actually visited a file *mail*).
1909 ;; (set-visited-file-name nil)
1910 (let (initialized)
1911 (and (not (and noerase
1912 (not (eq noerase 'new))))
1913 (if buffer-file-name
1914 (if (buffer-modified-p)
1915 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1916 (if (y-or-n-p "Disconnect buffer from visited file? ")
1917 (set-visited-file-name nil))
1919 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1920 (set-visited-file-name nil)
1922 ;; A non-file-visiting buffer.
1923 (if (buffer-modified-p)
1924 (y-or-n-p "Unsent message being composed; erase it? ")
1926 (let ((inhibit-read-only t))
1927 (erase-buffer)
1928 (mail-setup to subject in-reply-to cc replybuffer actions
1929 return-action)
1930 (setq initialized t)))
1931 (if (and buffer-auto-save-file-name
1932 (file-exists-p buffer-auto-save-file-name))
1933 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1934 initialized))
1936 (declare-function dired-view-file "dired" ())
1937 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1939 (defun mail-recover-1 ()
1940 "Pop up a list of auto-saved draft messages so you can recover one of them."
1941 (interactive)
1942 (let ((file-name (make-auto-save-file-name))
1943 (ls-lisp-support-shell-wildcards t)
1944 non-random-len wildcard)
1945 ;; Remove the random part from the auto-save-file-name, and
1946 ;; create a wildcard which matches possible candidates.
1947 ;; Note: this knows that make-auto-save-file-name appends
1948 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1949 ;; is the result of (make-temp-name "").
1950 (setq non-random-len
1951 (- (length file-name) (length (make-temp-name "")) 1))
1952 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1953 (if (null (file-expand-wildcards wildcard))
1954 (message "There are no auto-saved drafts to recover")
1955 ;; Bind dired-trivial-filenames to t because all auto-save file
1956 ;; names are normally ``trivial'', so Dired will set point after
1957 ;; all the files, at buffer bottom. We want it on the first
1958 ;; file instead.
1959 ;; Require dired so that dired-trivial-filenames does not get
1960 ;; unbound on exit from the let.
1961 (require 'dired)
1962 (let ((dired-trivial-filenames t))
1963 (dired-other-window wildcard (concat dired-listing-switches " -t")))
1964 (rename-buffer "*Auto-saved Drafts*" t)
1965 (save-excursion
1966 (goto-char (point-min))
1967 (or (looking-at " Move to the draft file you want to recover,")
1968 (let ((inhibit-read-only t))
1969 ;; Each line starts with a space so that Font Lock mode
1970 ;; won't highlight the first character.
1971 (insert "\
1972 Move to the draft file you want to recover, then type C-c C-c
1973 to recover text of message whose composition was interrupted.
1974 To browse text of a draft, type v on the draft file's line.
1976 You can also delete some of these files;
1977 type d on a line to mark that file for deletion.
1979 List of possible auto-save files for recovery:
1981 "))))
1982 (use-local-map
1983 (let ((map (make-sparse-keymap)))
1984 (set-keymap-parent map (current-local-map))
1985 map))
1986 (define-key (current-local-map) "v"
1987 (lambda ()
1988 (interactive)
1989 (let ((coding-system-for-read 'utf-8-emacs-unix))
1990 (dired-view-file))))
1991 (define-key (current-local-map) "\C-c\C-c"
1992 (lambda ()
1993 (interactive)
1994 (let ((fname (dired-get-filename))
1995 ;; Auto-saved files are written in the internal
1996 ;; representation, so they should be read accordingly.
1997 (coding-system-for-read 'utf-8-emacs-unix))
1998 (switch-to-buffer-other-window "*mail*")
1999 (let ((buffer-read-only nil))
2000 (erase-buffer)
2001 (insert-file-contents fname nil)
2002 ;; insert-file-contents will set buffer-file-coding-system
2003 ;; to utf-8-emacs, which is probably not what they want to
2004 ;; use for sending the message. But we don't know what
2005 ;; was its value before the buffer was killed or Emacs
2006 ;; crashed. We therefore reset buffer-file-coding-system
2007 ;; to the default value, so that either the default does
2008 ;; TRT, or the user will get prompted for the right
2009 ;; encoding when they send the message.
2010 (setq buffer-file-coding-system
2011 (default-value 'buffer-file-coding-system)))))))))
2013 (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
2014 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
2015 (declare-function dired-view-file "dired" ())
2017 (defun mail-recover ()
2018 "Recover interrupted mail composition from auto-save files.
2020 If the mail buffer has a current valid auto-save file,
2021 the command recovers that file. Otherwise, it displays a
2022 buffer showing the existing auto-saved draft messages;
2023 you can move to one of them and type C-c C-c to recover that one."
2024 (interactive)
2025 ;; In case they invoke us from some random buffer...
2026 (switch-to-buffer "*mail*")
2027 ;; If *mail* didn't exist, set its directory, so that auto-saved
2028 ;; drafts will be found.
2029 (let ((dir (expand-file-name
2030 (file-name-as-directory mail-default-directory))))
2031 (if (file-exists-p dir)
2032 (setq default-directory dir)))
2033 (or (eq major-mode 'mail-mode)
2034 (mail-mode))
2035 (let ((file-name buffer-auto-save-file-name))
2036 (cond ((and file-name (file-exists-p file-name))
2037 (let ((dispbuf
2038 ;; This used to invoke `ls' via call-process, but
2039 ;; dired-noselect is more portable to systems where
2040 ;; `ls' is not a standard program (it will use
2041 ;; ls-lisp instead).
2042 (dired-noselect file-name
2043 (concat dired-listing-switches " -t"))))
2044 (save-selected-window
2045 (switch-to-buffer-other-window dispbuf)
2046 (goto-char (point-min))
2047 (forward-line 2)
2048 (dired-move-to-filename)
2049 (setq dispbuf (rename-buffer "*Directory*" t)))
2050 (if (not (yes-or-no-p
2051 (format "Recover mail draft from auto save file %s? "
2052 file-name)))
2053 (error "mail-recover canceled")
2054 (let ((buffer-read-only nil)
2055 (buffer-coding buffer-file-coding-system)
2056 ;; Auto-save files are written in internal
2057 ;; representation of non-ASCII characters.
2058 (coding-system-for-read 'utf-8-emacs-unix))
2059 (erase-buffer)
2060 (insert-file-contents file-name nil)
2061 (setq buffer-file-coding-system buffer-coding)))))
2062 (t (mail-recover-1)))))
2064 ;;;###autoload
2065 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
2066 "Like `mail' command, but display mail buffer in another window."
2067 (interactive "P")
2068 (switch-to-buffer-other-window "*mail*")
2069 (mail noerase to subject in-reply-to cc replybuffer sendactions))
2071 ;;;###autoload
2072 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
2073 "Like `mail' command, but display mail buffer in another frame."
2074 (interactive "P")
2075 (switch-to-buffer-other-frame "*mail*")
2076 (mail noerase to subject in-reply-to cc replybuffer sendactions))
2078 ;; Do not add anything but external entries on this page.
2080 (provide 'sendmail)
2082 ;; Local Variables:
2083 ;; byte-compile-dynamic: t
2084 ;; End:
2086 ;;; sendmail.el ends here