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