Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / mail / sendmail.el
blob6fc91a3acd90864f78d7f3b98986fc555bb7df37
1 ;;; sendmail.el --- mail sending commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1992-1996, 1998, 2000-2018 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This mode provides mail-sending facilities from within Emacs. It is
27 ;; documented in the Emacs user's manual.
29 ;;; Code:
30 (require 'mail-utils)
31 (require 'rfc2047)
32 (autoload 'message-make-date "message")
34 (defgroup sendmail nil
35 "Mail sending commands for Emacs."
36 :prefix "mail-"
37 :group 'mail)
39 (defcustom mail-setup-with-from t
40 "Non-nil means insert `From:' field when setting up the message."
41 :type 'boolean
42 :group 'sendmail
43 :version "22.1")
45 (defcustom sendmail-program
46 (or (executable-find "sendmail")
47 (cond
48 ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
49 ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
50 ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
51 (t "sendmail")))
52 "Program used to send messages."
53 :version "24.1" ; add executable-find, remove fakemail
54 :group 'mail
55 :type 'file)
57 ;;;###autoload
58 (defcustom mail-from-style 'angles
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 "27.1"
76 :group 'sendmail)
77 (make-obsolete-variable
78 'mail-from-style
79 "only the `angles' value is valid according to RFC2822." "27.1" 'set)
81 ;;;###autoload
82 (defcustom mail-specify-envelope-from nil
83 "If non-nil, specify the envelope-from address when sending mail.
84 The value used to specify it is whatever is found in
85 the variable `mail-envelope-from', with `user-mail-address' as fallback.
87 On most systems, specifying the envelope-from address is a
88 privileged operation. This variable affects sendmail and
89 smtpmail -- if you use feedmail to send mail, see instead the
90 variable `feedmail-deduce-envelope-from'."
91 :version "21.1"
92 :type 'boolean
93 :group 'sendmail)
95 (defcustom mail-envelope-from nil
96 "If non-nil, designate the envelope-from address when sending mail.
97 This only has an effect if `mail-specify-envelope-from' is non-nil.
98 The value should be either a string, or the symbol `header' (in
99 which case the contents of the \"From\" header of the message
100 being sent is used), or nil (in which case the value of
101 `user-mail-address' is used)."
102 :version "21.1"
103 :type '(choice (string :tag "From-name")
104 (const :tag "Use From: header from message" header)
105 (const :tag "Use `user-mail-address'" nil))
106 :group 'sendmail)
108 ;;;###autoload
109 (defcustom mail-self-blind nil
110 "Non-nil means insert Bcc to self in messages to be sent.
111 This is done when the message is initialized,
112 so you can remove or alter the Bcc field to override the default."
113 :type 'boolean
114 :group 'sendmail)
116 ;;;###autoload
117 (defcustom mail-interactive t
118 ;; We used to use a default of nil rather than t, but nowadays it is very
119 ;; common for sendmail to be misconfigured, so one cannot rely on the
120 ;; bounce message to be delivered anywhere, least of all to the
121 ;; user's mailbox.
122 "Non-nil means when sending a message wait for and display errors.
123 Otherwise, let mailer send back a message to report errors."
124 :type 'boolean
125 :version "23.1" ; changed from nil to t
126 :group 'sendmail)
128 (defcustom mail-yank-ignored-headers
129 (concat "^"
130 (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
131 "received" "message-id" "summary-line" "to" "subject"
132 "in-reply-to" "return-path" "mail-reply-to"
133 ;; Should really be rmail-attribute-header and
134 ;; rmail-keyword-header, but this file does not
135 ;; require rmail (at run time).
136 "x-rmail-attributes" "x-rmail-keywords"
137 "mail-followup-to") "\\(?:")
138 ":")
139 "Delete these headers from old message when it's inserted in a reply."
140 :type 'regexp
141 :group 'sendmail
142 :version "23.1")
144 ;; Useful to set in site-init.el
145 ;;;###autoload
146 (defcustom send-mail-function
147 ;; Assume smtpmail is the preferred choice if it's already configured.
148 (if (and (boundp 'smtpmail-smtp-server)
149 smtpmail-smtp-server)
150 'smtpmail-send-it 'sendmail-query-once)
151 "Function to call to send the current buffer as mail.
152 The headers should be delimited by a line which is
153 not a valid RFC822 header or continuation line,
154 that matches the variable `mail-header-separator'.
155 This is used by the default mail-sending commands. See also
156 `message-send-mail-function' for use with the Message package."
157 :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
158 (function-item sendmail-query-once :tag "Query the user")
159 (function-item smtpmail-send-it :tag "Use SMTPmail package")
160 (function-item feedmail-send-it :tag "Use Feedmail package")
161 (function-item mailclient-send-it :tag "Use Mailclient package")
162 function)
163 :version "24.1"
164 :group 'sendmail)
166 ;;;###autoload
167 (defcustom mail-header-separator (purecopy "--text follows this line--")
168 "Line used to separate headers from text in messages being composed."
169 :type 'string
170 :group 'sendmail)
172 ;; Set up mail-header-separator for use as a category text property.
173 (put 'mail-header-separator 'rear-nonsticky '(category))
174 ;; This was a nice idea, for preventing accidental modification of
175 ;; the separator. But I found it also prevented or obstructed
176 ;; certain deliberate operations, such as copying the separator line
177 ;; up to the top to send myself a copy of an already sent outgoing message
178 ;; and other things. So I turned it off. --rms.
179 ;;(put 'mail-header-separator 'read-only t)
181 ;;;###autoload
182 (defcustom mail-archive-file-name nil
183 "Name of file to write all outgoing messages in, or nil for none.
184 This is normally an mbox file, but for backwards compatibility may also
185 be a Babyl file."
186 :type '(choice file (const nil))
187 :group 'sendmail)
189 ;;;###autoload
190 (defcustom mail-default-reply-to nil
191 "Address to insert as default Reply-To field of outgoing messages.
192 If nil, it will be initialized from the REPLYTO environment variable
193 when you first send mail."
194 :type '(choice (const nil) string)
195 :group 'sendmail)
197 (defcustom mail-alias-file nil
198 "If non-nil, the name of a file to use instead of the sendmail default.
199 This file defines aliases to be expanded by the mailer; this is a different
200 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
201 This variable has no effect unless your system uses sendmail as its mailer.
202 The default file is defined in sendmail's configuration file, e.g.
203 `/etc/aliases'."
204 :type '(choice (const :tag "Sendmail default" nil) file)
205 :group 'sendmail)
207 ;;;###autoload
208 (defcustom mail-personal-alias-file (purecopy "~/.mailrc")
209 "If non-nil, the name of the user's personal mail alias file.
210 This file typically should be in same format as the `.mailrc' file used by
211 the `Mail' or `mailx' program.
212 This file need not actually exist."
213 :type '(choice (const nil) file)
214 :group 'sendmail)
216 ;;;###autoload
217 (defcustom mail-setup-hook nil
218 "Normal hook, run each time a new outgoing message is initialized."
219 :type 'hook
220 :options '(fortune-to-signature spook mail-abbrevs-setup)
221 :group 'sendmail)
223 ;;;###autoload
224 (defvar mail-aliases t
225 "Alist of mail address aliases,
226 or t meaning should be initialized from your mail aliases file.
227 \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
228 can specify a different file name.)
229 The alias definitions in the file have this form:
230 alias ALIAS MEANING")
232 (defvar mail-alias-modtime nil
233 "The modification time of your mail alias file when it was last examined.")
235 ;;;###autoload
236 (defcustom mail-yank-prefix "> "
237 "Prefix insert on lines of yanked message being replied to.
238 If this is nil, use indentation, as specified by `mail-indentation-spaces'."
239 :type '(choice (const nil) string)
240 :group 'sendmail)
242 ;;;###autoload
243 (defcustom mail-indentation-spaces 3
244 "Number of spaces to insert at the beginning of each cited line.
245 Used by `mail-yank-original' via `mail-indent-citation'."
246 :type 'integer
247 :group 'sendmail)
249 ;;;###autoload
250 (defcustom mail-citation-hook nil
251 "Hook for modifying a citation just inserted in the mail buffer.
252 Each hook function can find the citation between (point) and (mark t),
253 and should leave point and mark around the citation text as modified.
254 The hook functions can find the header of the cited message
255 in the variable `mail-citation-header', whether or not this is included
256 in the cited portion of the message.
258 If this hook is entirely empty (nil), a default action is taken
259 instead of no action."
260 :type 'hook
261 :group 'sendmail)
263 (defvar mail-citation-header nil
264 "While running `mail-citation-hook', this variable holds the message header.
265 This enables the hook functions to see the whole message header
266 regardless of what part of it (if any) is included in the cited text.")
268 ;;;###autoload
269 (defcustom mail-citation-prefix-regexp
270 (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
271 "Regular expression to match a citation prefix plus whitespace.
272 It should match whatever sort of citation prefixes you want to handle,
273 with whitespace before and after; it should also match just whitespace.
274 The default value matches citations like `foo-bar>' plus whitespace."
275 :type 'regexp
276 :group 'sendmail
277 :version "24.1")
279 (defvar mail-abbrevs-loaded nil)
280 (defvar mail-mode-map
281 (let ((map (make-sparse-keymap)))
282 (define-key map "\M-\t" 'completion-at-point)
283 (define-key map "\C-c?" 'describe-mode)
284 (define-key map "\C-c\C-f\C-t" 'mail-to)
285 (define-key map "\C-c\C-f\C-b" 'mail-bcc)
286 (define-key map "\C-c\C-f\C-f" 'mail-fcc)
287 (define-key map "\C-c\C-f\C-c" 'mail-cc)
288 (define-key map "\C-c\C-f\C-s" 'mail-subject)
289 (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
290 (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to) ; author
291 (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
292 (define-key map "\C-c\C-t" 'mail-text)
293 (define-key map "\C-c\C-y" 'mail-yank-original)
294 (define-key map "\C-c\C-r" 'mail-yank-region)
295 (define-key map [remap split-line] 'mail-split-line)
296 (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
297 (define-key map "\C-c\C-w" 'mail-signature)
298 (define-key map "\C-c\C-c" 'mail-send-and-exit)
299 (define-key map "\C-c\C-s" 'mail-send)
300 (define-key map "\C-c\C-i" 'mail-insert-file)
301 ;; FIXME add this? "b" = bury buffer. It's in the menu-bar.
302 ;;; (define-key map "\C-c\C-b" 'mail-dont-send)
304 (define-key map [menu-bar mail]
305 (cons "Mail" (make-sparse-keymap "Mail")))
307 (define-key map [menu-bar mail attachment]
308 '("Attach File" . mail-add-attachment))
310 (define-key map [menu-bar mail fill]
311 '("Fill Citation" . mail-fill-yanked-message))
313 (define-key map [menu-bar mail yank]
314 '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
316 (define-key map [menu-bar mail signature]
317 '("Insert Signature" . mail-signature))
319 (define-key map [menu-bar mail mail-sep]
320 '("--"))
322 (define-key map [menu-bar mail cancel]
323 '("Cancel" . mail-dont-send))
325 (define-key map [menu-bar mail send-stay]
326 '("Send, Keep Editing" . mail-send))
328 (define-key map [menu-bar mail send]
329 '("Send Message" . mail-send-and-exit))
331 (define-key map [menu-bar headers]
332 (cons "Headers" (make-sparse-keymap "Move to Header")))
334 (define-key map [menu-bar headers text]
335 '("Text" . mail-text))
337 (define-key map [menu-bar headers expand-aliases]
338 '("Expand Aliases" . expand-mail-aliases))
340 (define-key map [menu-bar headers mail-reply-to]
341 '("Mail-Reply-To" . mail-mail-reply-to))
343 (define-key map [menu-bar headers mail-followup-to]
344 '("Mail-Followup-To" . mail-mail-followup-to))
346 (define-key map [menu-bar headers reply-to]
347 '("Reply-To" . mail-reply-to))
349 (define-key map [menu-bar headers bcc]
350 '("Bcc" . mail-bcc))
352 (define-key map [menu-bar headers fcc]
353 '("Fcc" . mail-fcc))
355 (define-key map [menu-bar headers cc]
356 '("Cc" . mail-cc))
358 (define-key map [menu-bar headers subject]
359 '("Subject" . mail-subject))
361 (define-key map [menu-bar headers to]
362 '("To" . mail-to))
364 map))
366 (autoload 'build-mail-aliases "mailalias"
367 "Read mail aliases from personal aliases file and set `mail-aliases'.
368 By default, this is the file specified by `mail-personal-alias-file'." t)
370 ;;;###autoload
371 (defcustom mail-signature t
372 "Text inserted at end of mail buffer when a message is initialized.
373 If t, it means to insert the contents of the file `mail-signature-file'.
374 If a string, that string is inserted.
375 (To make a proper signature, the string should begin with \\n\\n-- \\n,
376 which is the standard way to delimit a signature in a message.)
377 Otherwise, it should be an expression; it is evaluated
378 and should insert whatever you want to insert."
379 :type '(choice (const :tag "None" nil)
380 (const :tag "Use `.signature' file" t)
381 (string :tag "String to insert")
382 (sexp :tag "Expression to evaluate"))
383 :group 'sendmail)
384 (put 'mail-signature 'risky-local-variable t)
386 ;;;###autoload
387 (defcustom mail-signature-file (purecopy "~/.signature")
388 "File containing the text inserted at end of mail buffer."
389 :type 'file
390 :group 'sendmail)
392 ;;;###autoload
393 (defcustom mail-default-directory (purecopy "~/")
394 "Value of `default-directory' for Mail mode buffers.
395 This directory is used for auto-save files of Mail mode buffers.
397 Note that Message mode does not use this variable; it auto-saves
398 in `message-auto-save-directory'."
399 :type '(directory :tag "Directory")
400 :group 'sendmail
401 :version "22.1")
403 (defvar mail-reply-action nil)
404 (defvar mail-send-actions nil
405 "A list of actions to be performed upon successful sending of a message.")
406 (defvar mail-return-action nil)
408 ;;;###autoload
409 (defcustom mail-default-headers nil
410 "A string containing header lines, to be inserted in outgoing messages.
411 It can contain newlines, and should end in one. It is inserted
412 before you edit the message, so you can edit or delete the lines."
413 :type '(choice (const nil) string)
414 :group 'sendmail)
416 (defcustom mail-bury-selects-summary t
417 "If non-nil, try to show Rmail summary buffer after returning from mail.
418 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
419 the Rmail summary buffer before returning, if it exists and this variable
420 is non-nil."
421 :type 'boolean
422 :group 'sendmail)
424 (defcustom mail-send-nonascii 'mime
425 "Specify whether to allow sending non-ASCII characters in mail.
426 If t, that means do allow it. nil means don't allow it.
427 `query' means ask the user each time.
428 `mime' means add an appropriate MIME header if none already present.
429 The default is `mime'.
430 Including non-ASCII characters in a mail message can be problematical
431 for the recipient, who may not know how to decode them properly."
432 :type '(choice (const t) (const nil) (const query) (const mime))
433 :group 'sendmail)
435 (defcustom mail-use-dsn nil
436 "Ask MTA for notification of failed, delayed or successful delivery.
437 Note that only some MTAs (currently only recent versions of Sendmail)
438 support Delivery Status Notification."
439 :group 'sendmail
440 :type '(repeat (radio (const :tag "Failure" failure)
441 (const :tag "Delay" delay)
442 (const :tag "Success" success)))
443 :version "22.1")
445 ;; Note: could use /usr/ucb/mail instead of sendmail;
446 ;; options -t, and -v if not interactive.
447 (defvar mail-mailer-swallows-blank-line nil
448 "Set this non-nil if the system's mailer runs the header and body together.
449 The actual value should be an expression to evaluate that returns
450 non-nil if the problem will actually occur.
451 \(As far as we know, this is not an issue on any system still supported
452 by Emacs.)")
454 (put 'mail-mailer-swallows-blank-line 'risky-local-variable t) ; gets evalled
455 (make-obsolete-variable 'mail-mailer-swallows-blank-line
456 "no need to set this on any modern system."
457 "24.1" 'set)
459 (defvar mail-mode-syntax-table
460 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
461 (let ((st (make-syntax-table)))
462 ;; FIXME this is probably very obsolete now ("percent hack").
463 ;; sending.texi used to say:
464 ;; Mail mode defines the character `%' as a word separator; this
465 ;; is helpful for using the word commands to edit mail addresses.
466 (modify-syntax-entry ?% ". " st)
468 "Syntax table used while in `mail-mode'.")
470 (defvar mail-font-lock-keywords
471 (eval-when-compile
472 (let* ((cite-chars "[>|}]")
473 (cite-prefix "[:alpha:]")
474 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
475 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
476 '("^\\(B?Cc\\|Reply-To\\|Mail-\\(Reply\\|Followup\\)-To\\):" . font-lock-keyword-face)
477 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
478 (1 font-lock-comment-face)
479 ;; (2 font-lock-type-face nil t)
481 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
482 '(eval .
483 (let ((separator (if (zerop (length mail-header-separator))
484 " \\`\\' "
485 (regexp-quote mail-header-separator))))
486 (cons (concat "^" separator "$") 'font-lock-warning-face)))
487 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
488 `(,cite-chars
489 (,(concat "\\=[ \t]*"
490 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
491 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
492 "\\(.*\\)")
493 (beginning-of-line) (end-of-line)
494 (1 font-lock-comment-delimiter-face nil t)
495 (5 font-lock-comment-face nil t)))
496 '("^\\(X-[A-Za-z0-9-]+\\|In-Reply-To\\):.*\\(\n[ \t]+.*\\)*$"
497 . font-lock-string-face))))
498 "Additional expressions to highlight in Mail mode.")
501 ;;;###autoload
502 (defun sendmail-query-once ()
503 "Query for `send-mail-function' and send mail with it.
504 This also saves the value of `send-mail-function' via Customize."
505 ;; If send-mail-function is already setup, we're incorrectly called
506 ;; a second time, probably because someone's using an old value
507 ;; of send-mail-function.
508 (if (not (eq send-mail-function 'sendmail-query-once))
509 (funcall send-mail-function)
510 (let ((function (sendmail-query-user-about-smtp)))
511 (funcall function)
512 (when (y-or-n-p "Save this mail sending choice?")
513 (setq send-mail-function function)
514 (customize-save-variable 'send-mail-function function)))))
516 (defun sendmail-query-user-about-smtp ()
517 (let* ((options `(("mail client" . mailclient-send-it)
518 ,@(when (and sendmail-program
519 (executable-find sendmail-program))
520 '(("transport" . sendmail-send-it)))
521 ("smtp" . smtpmail-send-it)))
522 (choice
523 ;; Query the user.
524 (with-temp-buffer
525 (rename-buffer "*Emacs Mail Setup Help*" t)
526 (insert (substitute-command-keys "\
527 Emacs is about to send an email message, but it has not been
528 configured for sending email. To tell Emacs how to send email:
530 - Type `")
531 (propertize "mail client" 'face 'bold)
532 (substitute-command-keys "\
533 ' to start your default email client and
534 pass it the message text.\n\n"))
535 (and sendmail-program
536 (executable-find sendmail-program)
537 (insert (substitute-command-keys "\
538 - Type `")
539 (propertize "transport" 'face 'bold)
540 (substitute-command-keys "\
541 ' to invoke the system's mail transport agent
542 (the `")
543 sendmail-program
544 (substitute-command-keys "' program).\n\n")))
545 (insert (substitute-command-keys "\
546 - Type `")
547 (propertize "smtp" 'face 'bold)
548 (substitute-command-keys "' to send mail directly to an \"outgoing mail\" server.
549 (Emacs may prompt you for SMTP settings).
551 Emacs will record your selection and will use it thereafter.
552 To change it later, customize the option `send-mail-function'.\n"))
553 (goto-char (point-min))
554 (display-buffer (current-buffer))
555 (let ((completion-ignore-case t))
556 (completing-read
557 (format "Send mail via (default %s): " (caar options))
558 options nil 'require-match nil nil (car options))))))
559 ;; Return the choice.
560 (cdr (assoc-string choice options t))))
562 (defun sendmail-sync-aliases ()
563 (when mail-personal-alias-file
564 (let ((modtime (file-attribute-modification-time
565 (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 t)
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 (goto-char (point-min))
789 (when (re-search-forward
790 (concat "^" (regexp-quote mail-header-separator) "\n")
791 nil t)
792 (replace-match "\n"))
793 (rfc822-goto-eoh))
795 (defun mail-mode-auto-fill ()
796 "Carry out Auto Fill for Mail mode.
797 If within the headers, this makes the new lines into continuation lines."
798 (if (< (point) (mail-header-end))
799 (let ((old-line-start (line-beginning-position)))
800 (if (do-auto-fill)
801 (save-excursion
802 (beginning-of-line)
803 (while (not (eq (point) old-line-start))
804 ;; Use insert-before-markers in case we're inserting
805 ;; before the saved value of point (which is common).
806 (insert-before-markers " ")
807 (forward-line -1))
808 t)))
809 (do-auto-fill)))
811 (defun mail-mode-fill-paragraph (arg)
812 ;; Do something special only if within the headers.
813 (if (< (point) (mail-header-end))
814 (let (beg end fieldname)
815 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
816 (setq beg (point)))
817 (setq fieldname
818 (downcase (buffer-substring beg (1- (match-end 0))))))
819 (forward-line 1)
820 ;; Find continuation lines and get rid of their continuation markers.
821 (while (looking-at "[ \t]")
822 (delete-horizontal-space)
823 (forward-line 1))
824 (setq end (point-marker))
825 (goto-char beg)
826 ;; If this field contains addresses,
827 ;; make sure we can fill after each address.
828 (if (member fieldname
829 '("to" "cc" "bcc" "from" "reply-to"
830 "mail-reply-to" "mail-followup-to"
831 "resent-to" "resent-cc" "resent-bcc"
832 "resent-from" "resent-reply-to"))
833 (while (search-forward "," end t)
834 (or (looking-at "[ \t]")
835 (insert " "))))
836 (fill-region-as-paragraph beg end arg)
837 ;; Mark all lines except the first as continuations.
838 (goto-char beg)
839 (forward-line 1)
840 (while (< (point) end)
841 (insert " ")
842 (forward-line 1))
843 (move-marker end nil)
844 t)))
846 ;; User-level commands for sending.
848 (defun mail-send-and-exit (&optional arg)
849 "Send message like `mail-send', then, if no errors, exit from mail buffer.
850 Prefix arg means don't delete this window."
851 (interactive "P")
852 (mail-send)
853 (mail-bury arg))
855 (defun mail-dont-send (&optional arg)
856 "Don't send the message you have been editing.
857 Prefix arg means don't delete this window."
858 (interactive "P")
859 (mail-bury arg))
861 (defun mail-bury (&optional arg)
862 "Bury this mail buffer."
863 (let ((newbuf (other-buffer (current-buffer)))
864 (return-action mail-return-action))
865 (bury-buffer (current-buffer))
866 ;; If there is an Rmail buffer, return to it nicely
867 ;; even if this message was not started by an Rmail command.
868 (unless return-action
869 (dolist (buffer (buffer-list))
870 (if (and (eq (buffer-local-value 'major-mode buffer) 'rmail-mode)
871 (null return-action)
872 ;; Don't match message-viewer buffer.
873 (not (string-match "\\` " (buffer-name buffer))))
874 (setq return-action `(rmail-mail-return ,buffer)))))
875 (if (and (null arg) return-action)
876 (apply (car return-action) (cdr return-action))
877 (switch-to-buffer newbuf))))
879 (defcustom mail-send-hook nil
880 "Hook run just before sending a message."
881 :type 'hook
882 :options '(flyspell-mode-off)
883 :group 'sendmail)
885 ;;;###autoload
886 (defcustom mail-mailing-lists nil
887 "List of mailing list addresses the user is subscribed to.
888 The variable is used to trigger insertion of the \"Mail-Followup-To\"
889 header when sending a message to a mailing list."
890 :type '(repeat string)
891 :group 'sendmail)
893 (declare-function mml-to-mime "mml" ())
895 (defun mail-send ()
896 "Send the message in the current buffer.
897 If `mail-interactive' is non-nil, wait for success indication
898 or error messages, and inform user.
899 Otherwise any failure is reported in a message back to
900 the user from the mailer."
901 (interactive)
902 (if (if buffer-file-name
903 (y-or-n-p "Send buffer contents as mail message? ")
904 (or (buffer-modified-p)
905 (y-or-n-p "Message already sent; resend? ")))
906 (let ((inhibit-read-only t)
907 (opoint (point))
908 (ml (when mail-mailing-lists
909 ;; The surrounding regexp assumes the use of
910 ;; `mail-strip-quoted-names' on addresses before matching
911 ;; Cannot deal with full RFC 822 freedom, but that is
912 ;; unlikely to be problematic.
913 (concat "\\(?:[[:space:];,]\\|\\`\\)"
914 (regexp-opt mail-mailing-lists t)
915 "\\(?:[[:space:];,]\\|\\'\\)"))))
916 (mail-combine-fields "To")
917 (mail-combine-fields "Cc")
918 ;; If there are mailing lists defined
919 (when ml
920 (save-excursion
921 (let* ((to (mail-fetch-field "to" nil t))
922 (cc (mail-fetch-field "cc" nil t))
923 (new-header-values ; To: and Cc:
924 (mail-strip-quoted-names
925 (concat to (when cc (concat ", " cc))))))
926 ;; If message goes to known mailing list ...
927 (when (string-match ml new-header-values)
928 ;; Add Mail-Followup-To if none yet
929 (unless (mail-fetch-field "mail-followup-to")
930 (goto-char (mail-header-end))
931 (insert "Mail-Followup-To: "
932 (let ((l))
933 (mapc
934 ;; remove duplicates
935 (lambda (e)
936 (unless (member e l)
937 (push e l)))
938 (split-string new-header-values
939 ",[[:space:]]+" t))
940 (mapconcat 'identity l ", "))
941 "\n"))
942 ;; Add Mail-Reply-To if none yet
943 (unless (mail-fetch-field "mail-reply-to")
944 (goto-char (mail-header-end))
945 (insert "Mail-Reply-To: "
946 (or (mail-fetch-field "reply-to")
947 user-mail-address)
948 "\n"))))))
949 (unless (memq mail-send-nonascii '(t mime))
950 (goto-char (point-min))
951 (skip-chars-forward "\0-\177")
952 (or (= (point) (point-max))
953 (if (eq mail-send-nonascii 'query)
954 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
955 (error "Aborted"))
956 (error "Message contains non-ASCII characters"))))
957 ;; Complain about any invalid line.
958 (goto-char (point-min))
959 (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
960 (let ((header-end (or (match-beginning 0) (point-max))))
961 (goto-char (point-min))
962 (while (< (point) header-end)
963 (unless (looking-at "[ \t]\\|.*:\\|$")
964 (push-mark opoint)
965 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
966 (forward-line 1)))
967 (goto-char opoint)
968 (when mail-encode-mml
969 (mml-to-mime)
970 (setq mail-encode-mml nil))
971 (run-hooks 'mail-send-hook)
972 (message "Sending...")
973 (funcall send-mail-function)
974 ;; Now perform actions on successful sending.
975 (while mail-send-actions
976 (condition-case nil
977 (apply (car (car mail-send-actions))
978 (cdr (car mail-send-actions)))
979 (error))
980 (setq mail-send-actions (cdr mail-send-actions)))
981 (message "Sending...done")
982 ;; If buffer has no file, mark it as unmodified and delete auto-save.
983 (if (not buffer-file-name)
984 (progn
985 (set-buffer-modified-p nil)
986 (delete-auto-save-file-if-necessary t))))))
988 (defun mail-envelope-from ()
989 "Return the envelope mail address to use when sending mail.
990 This function uses `mail-envelope-from'."
991 (if (eq mail-envelope-from 'header)
992 (nth 1 (mail-extract-address-components
993 (mail-fetch-field "From")))
994 mail-envelope-from))
996 ;; This does the real work of sending a message via sendmail.
997 ;; It is called via the variable send-mail-function.
999 ;;;###autoload
1000 (defvar sendmail-coding-system nil
1001 "Coding system for encoding the outgoing mail.
1002 This has higher priority than the default `buffer-file-coding-system'
1003 and `default-sendmail-coding-system',
1004 but lower priority than the local value of `buffer-file-coding-system'.
1005 See also the function `select-message-coding-system'.")
1007 ;;;###autoload
1008 (defvar default-sendmail-coding-system 'iso-latin-1
1009 "Default coding system for encoding the outgoing mail.
1010 This variable is used only when `sendmail-coding-system' is nil.
1012 This variable is set/changed by the command `set-language-environment'.
1013 User should not set this variable manually,
1014 instead use `sendmail-coding-system' to get a constant encoding
1015 of outgoing mails regardless of the current language environment.
1016 See also the function `select-message-coding-system'.")
1018 (defun mail-insert-from-field ()
1019 "Insert the \"From:\" field of a mail header.
1020 The style of the field is determined by the variable `mail-from-style'.
1021 This function does not perform RFC2047 encoding."
1022 (let* ((login user-mail-address)
1023 (fullname (user-full-name))
1024 (quote-fullname nil))
1025 (if (string-match "[^\0-\177]" fullname)
1026 (setq quote-fullname t))
1027 (cond ((null mail-from-style)
1028 (insert "From: " login "\n"))
1029 ;; This is deprecated.
1030 ((eq mail-from-style 'system-default)
1031 nil)
1032 ((or (eq mail-from-style 'angles)
1033 (and (not (eq mail-from-style 'parens))
1034 ;; Use angles if no quoting is needed, or if
1035 ;; parens would need quoting too.
1036 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1037 (let ((tmp (concat fullname nil)))
1038 (while (string-match "([^()]*)" tmp)
1039 (aset tmp (match-beginning 0) ?-)
1040 (aset tmp (1- (match-end 0)) ?-))
1041 (string-match "[\\()]" tmp)))))
1042 (insert "From: " fullname)
1043 (let ((fullname-start (+ (point-min) 6))
1044 (fullname-end (point-marker)))
1045 (goto-char fullname-start)
1046 ;; Look for a character that cannot appear unquoted
1047 ;; according to RFC 822.
1048 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
1049 fullname-end 1)
1050 quote-fullname)
1051 (progn
1052 ;; Quote fullname, escaping specials.
1053 (goto-char fullname-start)
1054 (insert "\"")
1055 (while (re-search-forward "[\"\\]"
1056 fullname-end 1)
1057 (replace-match "\\\\\\&" t))
1058 (insert "\""))))
1059 (insert " <" login ">\n"))
1060 ;; 'parens or default
1062 (insert "From: " login " (")
1063 (let ((fullname-start (point)))
1064 (if quote-fullname
1065 (insert "\""))
1066 (insert fullname)
1067 (if quote-fullname
1068 (insert "\""))
1069 (let ((fullname-end (point-marker)))
1070 (goto-char fullname-start)
1071 ;; RFC 822 says \ and nonmatching parentheses
1072 ;; must be escaped in comments.
1073 ;; Escape every instance of ()\ ...
1074 (while (re-search-forward "[()\\]" fullname-end 1)
1075 (replace-match "\\\\\\&" t))
1076 ;; ... then undo escaping of matching parentheses,
1077 ;; including matching nested parentheses.
1078 (goto-char fullname-start)
1079 (while (re-search-forward
1080 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1081 fullname-end 1)
1082 (replace-match "\\1(\\3)" t)
1083 (goto-char fullname-start))))
1084 (insert ")\n")))))
1086 (defun mail-combine-fields (field)
1087 "Offer to combine all FIELD fields in buffer into one FIELD field.
1088 If this finds multiple FIELD fields, it asks the user whether
1089 to combine them into one, and does so if the user says y."
1090 (let ((search-pattern (format "^%s[ \t]*:" field))
1091 first-to-end
1092 query-asked
1093 query-answer
1094 (old-point (point))
1095 (old-max (point-max)))
1096 (save-excursion
1097 (save-restriction
1098 (goto-char (point-min))
1099 (narrow-to-region (point-min) (mail-header-end))
1100 ;; Find the first FIELD field and record where it ends.
1101 (when (re-search-forward search-pattern nil t)
1102 (forward-line 1)
1103 (re-search-forward "^[^ \t]" nil t)
1104 (beginning-of-line)
1105 (setq first-to-end (point-marker))
1106 (set-marker-insertion-type first-to-end t)
1107 ;; Find each following FIELD field
1108 ;; and combine it with the first FIELD field.
1109 (while (re-search-forward search-pattern nil t)
1110 ;; For the second FIELD field, ask user to
1111 ;; approve combining them.
1112 ;; But if the user refuse to combine them, signal error.
1113 (unless query-asked
1114 (save-restriction
1115 ;; This is just so the screen doesn't change.
1116 (narrow-to-region (point-min) old-max)
1117 (save-excursion
1118 (goto-char old-point)
1119 (setq query-asked t)
1120 (if (y-or-n-p (format "Message contains multiple %s fields. Combine? " field))
1121 (setq query-answer t)))))
1122 (when query-answer
1123 (let ((this-to-start (line-beginning-position))
1124 this-to-end
1125 this-to)
1126 (forward-line 1)
1127 (re-search-forward "^[^ \t]" nil t)
1128 (beginning-of-line)
1129 (setq this-to-end (point))
1130 ;; Get the text of this FIELD field.
1131 (setq this-to (buffer-substring this-to-start this-to-end))
1132 ;; Delete it.
1133 (delete-region this-to-start this-to-end)
1134 (save-excursion
1135 ;; Put a comma after the first FIELD field.
1136 (goto-char first-to-end)
1137 (forward-char -1)
1138 (insert ",")
1139 ;; Copy this one after it.
1140 (goto-char first-to-end)
1141 (save-excursion
1142 (insert this-to))
1143 ;; Replace the FIELD: with spaces.
1144 (looking-at search-pattern)
1145 ;; Try to preserve alignment of contents of the field
1146 (let ((prefix-length (length (match-string 0))))
1147 (replace-match " ")
1148 (dotimes (_ (1- prefix-length))
1149 (insert " ")))))))
1150 (set-marker first-to-end nil))))))
1152 (defun mail-encode-header (beg end)
1153 "Encode the mail header between BEG and END according to RFC2047.
1154 Return non-nil if and only if some part of the header is encoded."
1155 (save-restriction
1156 (narrow-to-region beg end)
1157 (let* ((selected (select-message-coding-system))
1158 (mm-coding-system-priorities
1159 (if (and selected (coding-system-get selected :mime-charset))
1160 (cons selected mm-coding-system-priorities)
1161 mm-coding-system-priorities))
1162 (tick (buffer-chars-modified-tick))
1163 ;; Many mailers, including Gnus, passes a message of which
1164 ;; the header is already encoded, so this is necessary to
1165 ;; prevent it from being encoded again.
1166 (rfc2047-encode-encoded-words nil))
1167 (rfc2047-encode-message-header)
1168 (= tick (buffer-chars-modified-tick)))))
1170 ;; Normally you will not need to modify these options unless you are
1171 ;; using some non-genuine substitute for sendmail which does not
1172 ;; implement each and every option that the original supports.
1173 ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1174 ;; you will need to modify `sendmail-error-reporting-non-interactive'
1175 ;; in your site-init.el.
1176 (defvar sendmail-error-reporting-interactive
1177 ;; These mean "report errors to terminal" and "deliver interactively"
1178 '("-oep" "-odi"))
1179 (defvar sendmail-error-reporting-non-interactive
1180 ;; These mean "report errors by mail" and "deliver in background".
1181 '("-oem" "-odb"))
1183 (defun sendmail-send-it ()
1184 "Send the current mail buffer using the Sendmail package.
1185 This is a suitable value for `send-mail-function'. It sends using the
1186 external program defined by `sendmail-program'."
1187 (require 'mail-utils)
1188 (let ((errbuf (if mail-interactive
1189 (generate-new-buffer " sendmail errors")
1191 (error nil)
1192 (tembuf (generate-new-buffer " sendmail temp"))
1193 (multibyte enable-multibyte-characters)
1194 (case-fold-search nil)
1195 (selected-coding (select-message-coding-system))
1196 resend-to-addresses
1197 delimline
1198 fcc-was-found
1199 (mailbuf (current-buffer))
1200 ;; Examine these variables now, so that
1201 ;; local binding in the mail buffer will take effect.
1202 (envelope-from
1203 (and mail-specify-envelope-from
1204 (or (mail-envelope-from) user-mail-address))))
1205 (unwind-protect
1206 (with-current-buffer tembuf
1207 (erase-buffer)
1208 (unless multibyte
1209 (set-buffer-multibyte nil))
1210 (insert-buffer-substring mailbuf)
1211 (set-buffer-file-coding-system selected-coding)
1212 (goto-char (point-max))
1213 ;; require one newline at the end.
1214 (or (= (preceding-char) ?\n)
1215 (insert ?\n))
1216 ;; Change header-delimiter to be what sendmail expects.
1217 (goto-char (mail-header-end))
1218 (delete-region (point) (progn (end-of-line) (point)))
1219 (setq delimline (point-marker))
1220 (sendmail-sync-aliases)
1221 (if mail-aliases
1222 (expand-mail-aliases (point-min) delimline))
1223 (goto-char (point-min))
1224 ;; Ignore any blank lines in the header
1225 (while (and (re-search-forward "\n\n\n*" delimline t)
1226 (< (point) delimline))
1227 (replace-match "\n"))
1228 (goto-char (point-min))
1229 ;; Look for Resent- headers. They require sending
1230 ;; the message specially.
1231 (let ((case-fold-search t))
1232 (goto-char (point-min))
1233 (while (re-search-forward "^Resent-\\(To\\|Cc\\|Bcc\\):" delimline t)
1234 ;; Put a list of such addresses in resend-to-addresses.
1235 (setq resend-to-addresses
1236 (save-restriction
1237 (narrow-to-region (point)
1238 (save-excursion
1239 (forward-line 1)
1240 (while (looking-at "^[ \t]")
1241 (forward-line 1))
1242 (point)))
1243 (append (mail-parse-comma-list)
1244 resend-to-addresses)))
1245 ;; Delete Resent-Bcc ourselves
1246 (if (save-excursion (beginning-of-line)
1247 (looking-at "resent-bcc"))
1248 (delete-region (line-beginning-position)
1249 (line-beginning-position 2))))
1250 ;; Apparently this causes a duplicate Sender.
1251 ;; ;; If the From is different than current user, insert Sender.
1252 ;; (goto-char (point-min))
1253 ;; (and (re-search-forward "^From:" delimline t)
1254 ;; (progn
1255 ;; (require 'mail-utils)
1256 ;; (not (string-equal
1257 ;; (mail-strip-quoted-names
1258 ;; (save-restriction
1259 ;; (narrow-to-region (point-min) delimline)
1260 ;; (mail-fetch-field "From")))
1261 ;; (user-login-name))))
1262 ;; (progn
1263 ;; (forward-line 1)
1264 ;; (insert "Sender: " (user-login-name) "\n")))
1265 ;; Don't send out a blank subject line
1266 (goto-char (point-min))
1267 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1268 (replace-match "")
1269 ;; This one matches a Subject just before the header delimiter.
1270 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1271 (= (match-end 0) delimline))
1272 (replace-match "")))
1273 ;; Put the "From:" field in unless for some odd reason
1274 ;; they put one in themselves.
1275 (goto-char (point-min))
1276 (if (not (re-search-forward "^From:" delimline t))
1277 (mail-insert-from-field))
1278 ;; Possibly add a MIME header for the current coding system
1279 (let (charset where-content-type)
1280 (goto-char (point-min))
1281 (setq where-content-type
1282 (re-search-forward "^Content-type:" delimline t))
1283 (goto-char (point-min))
1284 (and (eq mail-send-nonascii 'mime)
1285 (not (re-search-forward "^MIME-version:" delimline t))
1286 (progn (skip-chars-forward "\0-\177")
1287 (/= (point) (point-max)))
1288 selected-coding
1289 (setq charset
1290 (coding-system-get selected-coding :mime-charset))
1291 (progn
1292 (goto-char delimline)
1293 (insert "MIME-version: 1.0\n"
1294 "Content-type: text/plain; charset="
1295 (symbol-name charset)
1296 "\nContent-Transfer-Encoding: 8bit\n")
1297 ;; The character set we will actually use
1298 ;; should override any specified in the message itself.
1299 (when where-content-type
1300 (goto-char where-content-type)
1301 (delete-region (point-at-bol)
1302 (progn (forward-line 1) (point)))))))
1303 ;; Insert an extra newline if we need it to work around
1304 ;; Sun's bug that swallows newlines.
1305 (goto-char (1+ delimline))
1306 (if (eval mail-mailer-swallows-blank-line)
1307 (newline))
1308 ;; Find and handle any Fcc fields.
1309 (goto-char (point-min))
1310 (if (re-search-forward "^Fcc:" delimline t)
1311 (progn
1312 (setq fcc-was-found t)
1313 (mail-do-fcc delimline)))
1314 (if mail-interactive
1315 (with-current-buffer errbuf
1316 (erase-buffer))))
1317 ;; Encode the header according to RFC2047.
1318 (mail-encode-header (point-min) delimline)
1319 (goto-char (point-min))
1320 (if (let ((case-fold-search t))
1321 (or resend-to-addresses
1322 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1323 delimline t)))
1324 (let* ((default-directory "/")
1325 (coding-system-for-write selected-coding)
1326 (args
1327 (append (list (point-min) (point-max)
1328 sendmail-program
1329 nil errbuf nil "-oi")
1330 (and envelope-from
1331 (list "-f" envelope-from))
1332 ;; ;; Don't say "from root" if running under su.
1333 ;; (and (equal (user-real-login-name) "root")
1334 ;; (list "-f" (user-login-name)))
1335 (and mail-alias-file
1336 (list (concat "-oA" mail-alias-file)))
1337 (if mail-interactive
1338 sendmail-error-reporting-interactive
1339 sendmail-error-reporting-non-interactive)
1340 ;; Get the addresses from the message
1341 ;; unless this is a resend.
1342 ;; We must not do that for a resend
1343 ;; because we would find the original addresses.
1344 ;; For a resend, include the specific addresses.
1345 (or resend-to-addresses
1346 '("-t")
1348 (if mail-use-dsn
1349 (list "-N" (mapconcat 'symbol-name
1350 mail-use-dsn ",")))
1353 (exit-value (apply 'call-process-region args)))
1354 (cond ((or (null exit-value) (eq 0 exit-value)))
1355 ((numberp exit-value)
1356 (setq error t)
1357 (error "Sending...failed with exit value %d" exit-value))
1358 ((stringp exit-value)
1359 (setq error t)
1360 (error "Sending...terminated by signal: %s" exit-value))
1362 (setq error t)
1363 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
1364 (or fcc-was-found
1365 (error "No recipients")))
1366 (if mail-interactive
1367 (with-current-buffer errbuf
1368 (goto-char (point-min))
1369 (while (re-search-forward "\n\n* *" nil t)
1370 (replace-match "; "))
1371 (unless (zerop (buffer-size))
1372 (setq error t)
1373 (error "Sending...failed to %s"
1374 (buffer-substring (point-min) (point-max)))))))
1375 (kill-buffer tembuf)
1376 (when (buffer-live-p errbuf)
1377 (if error
1378 (switch-to-buffer-other-window errbuf)
1379 (kill-buffer errbuf))))))
1381 (autoload 'rmail-output-to-rmail-buffer "rmailout")
1383 (defun mail-do-fcc (header-end)
1384 "Find and act on any Fcc: headers in the current message before HEADER-END.
1385 If a buffer is visiting the Fcc file, append to it before
1386 offering to save it, if it was modified initially. If this is an
1387 Rmail buffer, update Rmail as needed. If there is no buffer,
1388 just append to the file, in Babyl format if necessary."
1389 (unless (markerp header-end)
1390 (error "Value of `header-end' must be a marker"))
1391 (let (fcc-list
1392 (mailbuf (current-buffer))
1393 (time (current-time)))
1394 (save-excursion
1395 (goto-char (point-min))
1396 (let ((case-fold-search t))
1397 (while (re-search-forward "^Fcc:[ \t]*" header-end t)
1398 (push (buffer-substring (point)
1399 (progn
1400 (end-of-line)
1401 (skip-chars-backward " \t")
1402 (point)))
1403 fcc-list)
1404 (delete-region (match-beginning 0)
1405 (progn (forward-line 1) (point)))))
1406 (with-temp-buffer
1407 ;; This initial newline is not written out if we create a new
1408 ;; file (see below).
1409 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1410 ;; Insert the time zone before the year.
1411 (forward-char -1)
1412 (forward-word-strictly -1)
1413 (require 'mail-utils)
1414 (insert (mail-rfc822-time-zone time) " ")
1415 (goto-char (point-max))
1416 (insert "Date: " (message-make-date) "\n")
1417 (insert-buffer-substring mailbuf)
1418 ;; Make sure messages are separated.
1419 (goto-char (point-max))
1420 (insert ?\n)
1421 (goto-char 2)
1422 ;; ``Quote'' "^From " as ">From "
1423 ;; (note that this isn't really quoting, as there is no requirement
1424 ;; that "^[>]+From " be quoted in the same transparent way.)
1425 (let ((case-fold-search nil))
1426 (while (search-forward "\nFrom " nil t)
1427 (forward-char -5)
1428 (insert ?>)))
1429 (dolist (fcc fcc-list)
1430 (let* ((buffer (find-buffer-visiting fcc))
1431 (curbuf (current-buffer))
1432 dont-write-the-file
1433 buffer-matches-file
1434 (beg (point-min)) ; the initial blank line
1435 (end (point-max))
1436 ;; After the ^From line.
1437 (beg2 (save-excursion (goto-char (point-min))
1438 (forward-line 2) (point))))
1439 (if buffer
1440 ;; File is present in a buffer => append to that buffer.
1441 (with-current-buffer buffer
1442 (setq buffer-matches-file
1443 (and (not (buffer-modified-p))
1444 (verify-visited-file-modtime buffer)))
1445 (let ((msg (bound-and-true-p rmail-current-message))
1446 (buffer-read-only nil))
1447 ;; If MSG is non-nil, buffer is in Rmail mode.
1448 (if msg
1449 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1450 (unwind-protect
1451 (progn
1452 (with-current-buffer buff
1453 (insert-buffer-substring curbuf (1+ beg) end))
1454 (rmail-output-to-rmail-buffer buff msg))
1455 (kill-buffer buff)))
1456 ;; Output file not in Rmail mode => just insert
1457 ;; at the end.
1458 (save-restriction
1459 (widen)
1460 (goto-char (point-max))
1461 (insert-buffer-substring curbuf beg end)))
1462 ;; Offer to save the buffer if it was modified
1463 ;; before we started.
1464 (unless buffer-matches-file
1465 (if (y-or-n-p (format "Save file %s? " fcc))
1466 (save-buffer))
1467 (setq dont-write-the-file t)))))
1468 ;; Append to the file directly, unless we've already taken
1469 ;; care of it.
1470 (unless dont-write-the-file
1471 (if (and (file-exists-p fcc)
1472 (mail-file-babyl-p fcc))
1473 ;; If the file is a Babyl file, convert the message to
1474 ;; Babyl format. Even though Rmail no longer uses
1475 ;; Babyl, this code can remain for the time being, on
1476 ;; the off-chance one Fccs to a Babyl file that has
1477 ;; not yet been converted to mbox.
1478 (let ((coding-system-for-write
1479 (or rmail-file-coding-system 'emacs-mule)))
1480 (with-temp-buffer
1481 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1482 (mail-rfc822-date) "\n")
1483 (insert-buffer-substring curbuf beg2 end)
1484 (insert "\n\C-_")
1485 (write-region (point-min) (point-max) fcc t)))
1486 ;; Ensure there is a blank line between messages, but
1487 ;; not at the very start of the file.
1488 (write-region (if (file-exists-p fcc)
1489 (point-min)
1490 (1+ (point-min)))
1491 (point-max) fcc t)))
1492 (and buffer (not dont-write-the-file)
1493 (with-current-buffer buffer
1494 (set-visited-file-modtime)))))))))
1496 (defun mail-sent-via ()
1497 "Make a Sent-via header line from each To or Cc header line."
1498 (declare (obsolete "nobody can remember what it is for." "24.1"))
1499 (interactive)
1500 (save-excursion
1501 ;; put a marker at the end of the header
1502 (let ((end (copy-marker (mail-header-end)))
1503 (case-fold-search t))
1504 (goto-char (point-min))
1505 ;; search for the To: lines and make Sent-via: lines from them
1506 ;; search for the next To: line
1507 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1508 ;; Grab this line plus all its continuations, sans the `to:'.
1509 (let ((to-line
1510 (buffer-substring (point)
1511 (progn
1512 (if (re-search-forward "^[^ \t\n]" end t)
1513 (backward-char 1)
1514 (goto-char end))
1515 (point)))))
1516 ;; Insert a copy, with altered header field name.
1517 (insert-before-markers "Sent-via:" to-line))))))
1519 (defun mail-to ()
1520 "Move point to end of To field, creating it if necessary."
1521 (interactive)
1522 (expand-abbrev)
1523 (mail-position-on-field "To"))
1525 (defun mail-subject ()
1526 "Move point to end of Subject field, creating it if necessary."
1527 (interactive)
1528 (expand-abbrev)
1529 (mail-position-on-field "Subject"))
1531 (defun mail-cc ()
1532 "Move point to end of Cc field, creating it if necessary."
1533 (interactive)
1534 (expand-abbrev)
1535 (or (mail-position-on-field "cc" t)
1536 (progn (mail-position-on-field "to")
1537 (insert "\nCC: "))))
1539 (defun mail-bcc ()
1540 "Move point to end of Bcc field, creating it if necessary."
1541 (interactive)
1542 (expand-abbrev)
1543 (or (mail-position-on-field "bcc" t)
1544 (progn (mail-position-on-field "to")
1545 (insert "\nBcc: "))))
1547 (defun mail-fcc (folder)
1548 "Add a new Fcc field, with file name completion."
1549 (interactive "FFolder carbon copy: ")
1550 (expand-abbrev)
1551 (or (mail-position-on-field "fcc" t) ;Put new field after exiting Fcc.
1552 (mail-position-on-field "to"))
1553 (insert "\nFcc: " folder))
1555 (defun mail-reply-to ()
1556 "Move point to end of Reply-To field, creating it if necessary."
1557 (interactive)
1558 (expand-abbrev)
1559 (mail-position-on-field "Reply-To"))
1561 (defun mail-mail-reply-to ()
1562 "Move point to end of Mail-Reply-To field, creating it if necessary."
1563 (interactive)
1564 (expand-abbrev)
1565 (or (mail-position-on-field "mail-reply-to" t)
1566 (progn (mail-position-on-field "to")
1567 (insert "\nMail-Reply-To: "))))
1569 (defun mail-mail-followup-to ()
1570 "Move point to end of Mail-Followup-To field, creating it if necessary."
1571 (interactive)
1572 (expand-abbrev)
1573 (or (mail-position-on-field "mail-followup-to" t)
1574 (progn (mail-position-on-field "to")
1575 (insert "\nMail-Followup-To: "))))
1577 (defun mail-position-on-field (field &optional soft)
1578 "Move to the end of the contents of header field FIELD.
1579 If there is no such header, insert one, unless SOFT is non-nil.
1580 If there are multiple FIELD fields, this goes to the first.
1581 Returns non-nil if FIELD was originally present."
1582 (let (end
1583 (case-fold-search t))
1584 (setq end (mail-header-end))
1585 (goto-char (point-min))
1586 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1587 (progn
1588 (re-search-forward "^[^ \t]" nil 'move)
1589 (beginning-of-line)
1590 (skip-chars-backward "\n")
1592 (or soft
1593 (progn (goto-char end)
1594 (insert field ": \n")
1595 (skip-chars-backward "\n")))
1596 nil)))
1598 (defun mail-text ()
1599 "Move point to beginning of text field."
1600 (interactive)
1601 (expand-abbrev)
1602 (goto-char (mail-text-start)))
1604 (defun mail-signature (&optional atpoint)
1605 "Sign letter with signature.
1606 If the variable `mail-signature' is a string, inserts it.
1607 If it is t or nil, inserts the contents of the file `mail-signature-file'.
1608 Otherwise, evals `mail-signature'.
1609 Prefix argument ATPOINT means insert at point rather than the end."
1610 (interactive "*P")
1611 ;; Test for an unreadable file here, before we delete trailing
1612 ;; whitespace, so that we don't modify the buffer needlessly.
1613 (if (and (memq mail-signature '(t nil))
1614 (not (file-readable-p mail-signature-file)))
1615 (if (called-interactively-p 'interactive)
1616 (message "The signature file `%s' could not be read"
1617 mail-signature-file))
1618 (save-excursion
1619 (unless atpoint
1620 (goto-char (point-max))
1621 ;; Delete trailing whitespace and blank lines.
1622 (skip-chars-backward " \t\n")
1623 (end-of-line)
1624 (delete-region (point) (point-max)))
1625 (cond ((stringp mail-signature)
1626 (insert mail-signature))
1627 ((memq mail-signature '(t nil))
1628 (insert "\n\n-- \n")
1629 (insert-file-contents (expand-file-name mail-signature-file)))
1631 ;; FIXME add condition-case error handling?
1632 (eval mail-signature))))))
1634 (defun mail-fill-yanked-message (&optional justifyp)
1635 "Fill the paragraphs of a message yanked into this one.
1636 Numeric argument means justify as well."
1637 (interactive "P")
1638 (save-excursion
1639 (goto-char (mail-text-start))
1640 (fill-individual-paragraphs (point)
1641 (point-max)
1642 justifyp
1643 mail-citation-prefix-regexp)))
1645 (defun mail-indent-citation ()
1646 "Modify text just inserted from a message to be cited.
1647 The inserted text should be the region.
1648 When this function returns, the region is again around the modified text.
1650 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1651 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1652 (mail-yank-clear-headers (region-beginning) (region-end))
1653 (if (null mail-yank-prefix)
1654 (indent-rigidly (region-beginning) (region-end)
1655 mail-indentation-spaces)
1656 (save-excursion
1657 (let ((end (set-marker (make-marker) (region-end))))
1658 (goto-char (region-beginning))
1659 (while (< (point) end)
1660 (insert mail-yank-prefix)
1661 (forward-line 1))))))
1663 (defun mail-yank-original (arg)
1664 "Insert the message being replied to, if any (in Rmail).
1665 Puts point after the text and mark before.
1666 Normally, indents each nonblank line ARG spaces (default 3).
1667 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1669 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1670 and don't delete any header fields."
1671 (interactive "P")
1672 (if mail-reply-action
1673 (let ((start (point))
1674 (original mail-reply-action)
1675 (omark (mark t)))
1676 (and (consp original) (eq (car original) 'insert-buffer)
1677 (setq original (nth 1 original)))
1678 (if (consp original)
1679 (progn
1680 ;; Call yank function, and set the mark if it doesn't.
1681 (apply (car original) (cdr original))
1682 (if (eq omark (mark t))
1683 (push-mark)))
1684 ;; If the original message is in another window in the same
1685 ;; frame, delete that window to save space.
1686 (delete-windows-on original t)
1687 (with-no-warnings
1688 ;; We really want this to set mark.
1689 (insert-buffer original)
1690 ;; If they yank the original text, the encoding of the
1691 ;; original message is a better default than
1692 ;; the default buffer-file-coding-system.
1693 (and (coding-system-equal
1694 (default-value 'buffer-file-coding-system)
1695 buffer-file-coding-system)
1696 (setq buffer-file-coding-system
1697 (coding-system-change-text-conversion
1698 buffer-file-coding-system
1699 (coding-system-base
1700 (with-current-buffer original
1701 buffer-file-coding-system))))))
1702 (set-text-properties (point) (mark t) nil))
1703 (if (consp arg)
1705 (goto-char start)
1706 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1707 mail-indentation-spaces))
1708 ;; Avoid error in Transient Mark mode
1709 ;; on account of mark's being inactive.
1710 (mark-even-if-inactive t))
1711 (cond (mail-citation-hook
1712 ;; Bind mail-citation-header to the inserted
1713 ;; message's header.
1714 (let ((mail-citation-header
1715 (buffer-substring-no-properties
1716 start
1717 (save-excursion
1718 (save-restriction
1719 (narrow-to-region start (point-max))
1720 (goto-char start)
1721 (rfc822-goto-eoh)
1722 (point))))))
1723 (run-hooks 'mail-citation-hook)))
1725 (mail-indent-citation)))))
1726 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1727 ;; It is cleaner to avoid activation, even though the command
1728 ;; loop would deactivate the mark because we inserted text.
1729 (goto-char (prog1 (mark t)
1730 (set-marker (mark-marker) (point) (current-buffer))))
1731 (if (not (eolp)) (insert ?\n)))))
1733 (defun mail-yank-clear-headers (start end)
1734 (if (< end start)
1735 (let (temp)
1736 (setq temp start start end end temp)))
1737 (if mail-yank-ignored-headers
1738 (save-excursion
1739 (goto-char start)
1740 (if (search-forward "\n\n" end t)
1741 (save-restriction
1742 (narrow-to-region start (point))
1743 (goto-char start)
1744 (while (let ((case-fold-search t))
1745 (re-search-forward mail-yank-ignored-headers nil t))
1746 (beginning-of-line)
1747 (delete-region (point)
1748 (progn (re-search-forward "\n[^ \t]")
1749 (forward-char -1)
1750 (point)))))))))
1752 (defun mail-yank-region (arg)
1753 "Insert the selected region from the message being replied to.
1754 Puts point after the text and mark before.
1755 Normally, indents each nonblank line ARG spaces (default 3).
1756 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1758 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1759 and don't delete any header fields."
1760 (interactive "P")
1761 (and (consp mail-reply-action)
1762 (memq (car mail-reply-action)
1763 '(rmail-yank-current-message insert-buffer))
1764 (with-current-buffer (nth 1 mail-reply-action)
1765 (or (mark t)
1766 (error "No mark set: %S" (current-buffer))))
1767 (let ((buffer (nth 1 mail-reply-action))
1768 (start (point))
1769 ;; Avoid error in Transient Mark mode
1770 ;; on account of mark's being inactive.
1771 (mark-even-if-inactive t))
1772 ;; Insert the citation text.
1773 (insert (with-current-buffer buffer
1774 (buffer-substring-no-properties (point) (mark))))
1775 (push-mark start)
1776 ;; Indent or otherwise annotate the citation text.
1777 (if (consp arg)
1779 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1780 mail-indentation-spaces)))
1781 (if mail-citation-hook
1782 ;; Bind mail-citation-header to the original message's header.
1783 (let ((mail-citation-header
1784 (with-current-buffer buffer
1785 (buffer-substring-no-properties
1786 (point-min)
1787 (save-excursion
1788 (goto-char (point-min))
1789 (rfc822-goto-eoh)
1790 (point))))))
1791 (run-hooks 'mail-citation-hook))
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 (defvar dired-trivial-filenames)
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