sendmail.el 71.7 KB
Newer Older
1
;;; sendmail.el --- mail sending commands for Emacs.  -*- byte-compile-dynamic: t -*-
Eric S. Raymond's avatar
Eric S. Raymond committed
2

3
;; Copyright (C) 1985-1986, 1992-1996, 1998, 2000-2011
4
;;   Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
7
;; Keywords: mail
Eric S. Raymond's avatar
Eric S. Raymond committed
8

Richard M. Stallman's avatar
Richard M. Stallman committed
9 10
;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
23

24 25 26 27 28
;;; Commentary:

;; This mode provides mail-sending facilities from within Emacs.  It is
;; documented in the Emacs user's manual.

Eric S. Raymond's avatar
Eric S. Raymond committed
29
;;; Code:
Glenn Morris's avatar
Glenn Morris committed
30
(require 'mail-utils)
31

32
(require 'rfc2047)
Dave Love's avatar
Dave Love committed
33

Stephen Eglen's avatar
Stephen Eglen committed
34 35 36 37
(defgroup sendmail nil
  "Mail sending commands for Emacs."
  :prefix "mail-"
  :group 'mail)
Richard M. Stallman's avatar
Richard M. Stallman committed
38

39 40
(defcustom mail-setup-with-from t
  "Non-nil means insert `From:' field when setting up the message."
41
  :type 'boolean
42 43 44
  :group 'sendmail
  :version "22.1")

45
(defcustom sendmail-program
Glenn Morris's avatar
Glenn Morris committed
46 47 48 49 50
  (or (executable-find "sendmail")
      (cond
       ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
       ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
       ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
Glenn Morris's avatar
Glenn Morris committed
51
       (t "sendmail")))
52
  "Program used to send messages."
Glenn Morris's avatar
Glenn Morris committed
53
  :version "24.1"		; add executable-find, remove fakemail
54 55 56
  :group 'mail
  :type 'file)

57
;;;###autoload
58
(defcustom mail-from-style 'default
59
  "Specifies how \"From:\" fields look.
60 61 62 63 64 65

If `nil', they contain just the return address like:
	king@grassland.com
If `parens', they look like:
	king@grassland.com (Elvis Parsley)
If `angles', they look like:
66
	Elvis Parsley <king@grassland.com>
67 68 69 70 71 72 73 74

Otherwise, most addresses look like `angles', but they look like
`parens' if `angles' would need quoting and `parens' would not."
  ;; The value `system-default' is now deprecated.
  :type '(choice (const :tag "simple" nil)
		 (const parens)
		 (const angles)
		 (const default))
75
  :version "20.3"
Stephen Eglen's avatar
Stephen Eglen committed
76
  :group 'sendmail)
77

78
;;;###autoload
79
(defcustom mail-specify-envelope-from nil
80
  "If non-nil, specify the envelope-from address when sending mail.
81
The value used to specify it is whatever is found in
Kenichi Handa's avatar
Kenichi Handa committed
82
the variable `mail-envelope-from', with `user-mail-address' as fallback.
83

84
On most systems, specifying the envelope-from address is a
Kenichi Handa's avatar
Kenichi Handa committed
85 86 87
privileged operation.  This variable affects sendmail and
smtpmail -- if you use feedmail to send mail, see instead the
variable `feedmail-deduce-envelope-from'."
88 89 90 91
  :version "21.1"
  :type 'boolean
  :group 'sendmail)

92
(defcustom mail-envelope-from nil
93
  "If non-nil, designate the envelope-from address when sending mail.
94 95 96 97 98
This only has an effect if `mail-specify-envelope-from' is non-nil.
The value should be either a string, or the symbol `header' (in
which case the contents of the \"From\" header of the message
being sent is used), or nil (in which case the value of
`user-mail-address' is used)."
99
  :version "21.1"
100
  :type '(choice (string :tag "From-name")
101
		 (const :tag "Use From: header from message" header)
102
		 (const :tag "Use `user-mail-address'" nil))
103 104
  :group 'sendmail)

Richard M. Stallman's avatar
Richard M. Stallman committed
105
;;;###autoload
106 107
(defcustom mail-self-blind nil
  "Non-nil means insert BCC to self in messages to be sent.
Richard M. Stallman's avatar
Richard M. Stallman committed
108
This is done when the message is initialized,
Stephen Eglen's avatar
Stephen Eglen committed
109 110 111
so you can remove or alter the BCC field to override the default."
  :type 'boolean
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
112 113

;;;###autoload
114 115 116 117 118
(defcustom mail-interactive t
  ;; We used to use a default of nil rather than t, but nowadays it is very
  ;; common for sendmail to be misconfigured, so one cannot rely on the
  ;; bounce message to be delivered anywhere, least of all to the
  ;; user's mailbox.
119
  "Non-nil means when sending a message wait for and display errors.
Glenn Morris's avatar
Glenn Morris committed
120
Otherwise, let mailer send back a message to report errors."
Stephen Eglen's avatar
Stephen Eglen committed
121
  :type 'boolean
122
  :version "23.1"			; changed from nil to t
Stephen Eglen's avatar
Stephen Eglen committed
123
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
124

125 126 127 128 129
(defcustom mail-yank-ignored-headers
  (concat "^"
          (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
                        "received" "message-id" "summary-line" "to" "subject"
                        "in-reply-to" "return-path" "mail-reply-to"
Glenn Morris's avatar
Glenn Morris committed
130 131 132 133
                        ;; Should really be rmail-attribute-header and
                        ;; rmail-keyword-header, but this file does not
                        ;; require rmail (at run time).
                        "x-rmail-attributes" "x-rmail-keywords"
134 135 136
                        "mail-followup-to") "\\(?:")
          ":")
  "Delete these headers from old message when it's inserted in a reply."
Stephen Eglen's avatar
Stephen Eglen committed
137
  :type 'regexp
Glenn Morris's avatar
Glenn Morris committed
138 139
  :group 'sendmail
  :version "23.1")
Richard M. Stallman's avatar
Richard M. Stallman committed
140 141 142

;; Useful to set in site-init.el
;;;###autoload
143
(defcustom send-mail-function 'sendmail-query-once
Dave Love's avatar
Dave Love committed
144
  "Function to call to send the current buffer as mail.
145
The headers should be delimited by a line which is
146 147
not a valid RFC822 header or continuation line,
that matches the variable `mail-header-separator'.
Dave Love's avatar
Dave Love committed
148 149
This is used by the default mail-sending commands.  See also
`message-send-mail-function' for use with the Message package."
Dave Love's avatar
Dave Love committed
150
  :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
151
		(function-item sendmail-query-once :tag "Query the user")
Dave Love's avatar
Dave Love committed
152 153
		(function-item smtpmail-send-it :tag "Use SMTPmail package")
		(function-item feedmail-send-it :tag "Use Feedmail package")
154
		(function-item mailclient-send-it :tag "Use Mailclient package")
Dave Love's avatar
Dave Love committed
155
		function)
156
  :version "24.1"
Dave Love's avatar
Dave Love committed
157
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
158 159

;;;###autoload
160
(defcustom mail-header-separator (purecopy "--text follows this line--")
161
  "Line used to separate headers from text in messages being composed."
Stephen Eglen's avatar
Stephen Eglen committed
162 163
  :type 'string
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
164

165 166
;; Set up mail-header-separator for use as a category text property.
(put 'mail-header-separator 'rear-nonsticky '(category))
167 168 169 170 171 172
;; This was a nice idea, for preventing accidental modification of
;; the separator.   But I found it also prevented or obstructed
;; certain deliberate operations, such as copying the separator line
;; up to the top to send myself a copy of an already sent outgoing message
;; and other things.  So I turned it off.  --rms.
;;(put 'mail-header-separator 'read-only t)
173

Richard M. Stallman's avatar
Richard M. Stallman committed
174
;;;###autoload
175 176
(defcustom mail-archive-file-name nil
  "Name of file to write all outgoing messages in, or nil for none.
177 178
This is normally an mbox file, but for backwards compatibility may also
be a Babyl file."
Stephen Eglen's avatar
Stephen Eglen committed
179 180
  :type '(choice file (const nil))
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
181

182
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
183
(defcustom mail-default-reply-to nil
184
  "Address to insert as default Reply-to field of outgoing messages.
185
If nil, it will be initialized from the REPLYTO environment variable
Stephen Eglen's avatar
Stephen Eglen committed
186 187 188
when you first send mail."
  :type '(choice (const nil) string)
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
189

Stephen Eglen's avatar
Stephen Eglen committed
190
(defcustom mail-alias-file nil
Glenn Morris's avatar
Glenn Morris committed
191
  "If non-nil, the name of a file to use instead of the sendmail default.
Richard M. Stallman's avatar
Richard M. Stallman committed
192 193
This file defines aliases to be expanded by the mailer; this is a different
feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
Glenn Morris's avatar
Glenn Morris committed
194 195 196 197
This variable has no effect unless your system uses sendmail as its mailer.
The default file is defined in sendmail's configuration file, e.g.
`/etc/aliases'."
  :type '(choice (const :tag "Sendmail default" nil) file)
Stephen Eglen's avatar
Stephen Eglen committed
198
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
199

200
;;;###autoload
201
(defcustom mail-personal-alias-file (purecopy "~/.mailrc")
202
  "If non-nil, the name of the user's personal mail alias file.
203 204
This file typically should be in same format as the `.mailrc' file used by
the `Mail' or `mailx' program.
Stephen Eglen's avatar
Stephen Eglen committed
205 206 207
This file need not actually exist."
  :type '(choice (const nil) file)
  :group 'sendmail)
208

209
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
210
(defcustom mail-setup-hook nil
211
  "Normal hook, run each time a new outgoing message is initialized."
Stephen Eglen's avatar
Stephen Eglen committed
212
  :type 'hook
213
  :options '(fortune-to-signature spook mail-abbrevs-setup)
Stephen Eglen's avatar
Stephen Eglen committed
214
  :group 'sendmail)
Karl Heuer's avatar
Karl Heuer committed
215

216
;;;###autoload
217
(defvar mail-aliases t
Richard M. Stallman's avatar
Richard M. Stallman committed
218
  "Alist of mail address aliases,
219
or t meaning should be initialized from your mail aliases file.
Kenichi Handa's avatar
Kenichi Handa committed
220 221
\(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
can specify a different file name.)
222
The alias definitions in the file have this form:
Richard M. Stallman's avatar
Richard M. Stallman committed
223
    alias ALIAS MEANING")
224

225
(defvar mail-alias-modtime nil
226
  "The modification time of your mail alias file when it was last examined.")
227

228
;;;###autoload
229
(defcustom mail-yank-prefix "> "
230
  "Prefix insert on lines of yanked message being replied to.
Glenn Morris's avatar
Glenn Morris committed
231
If this is nil, use indentation, as specified by `mail-indentation-spaces'."
Stephen Eglen's avatar
Stephen Eglen committed
232 233 234
  :type '(choice (const nil) string)
  :group 'sendmail)

235
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
236
(defcustom mail-indentation-spaces 3
237
  "Number of spaces to insert at the beginning of each cited line.
Stephen Eglen's avatar
Stephen Eglen committed
238 239 240
Used by `mail-yank-original' via `mail-indent-citation'."
  :type 'integer
  :group 'sendmail)
241

Glenn Morris's avatar
Glenn Morris committed
242
;; FIXME make it really obsolete.
243
(defvar mail-yank-hooks nil
244 245 246 247 248 249 250 251
  "Obsolete hook for modifying a citation just inserted in the mail buffer.
Each hook function can find the citation between (point) and (mark t).
And each hook function should leave point and mark around the citation
text as modified.

This is a normal hook, misnamed for historical reasons.
It is semi-obsolete and mail agents should no longer use it.")

252
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
253
(defcustom mail-citation-hook nil
254
  "Hook for modifying a citation just inserted in the mail buffer.
255 256 257 258 259
Each hook function can find the citation between (point) and (mark t),
and should leave point and mark around the citation text as modified.
The hook functions can find the header of the cited message
in the variable `mail-citation-header', whether or not this is included
in the cited portion of the message.
260

261
If this hook is entirely empty (nil), a default action is taken
Stephen Eglen's avatar
Stephen Eglen committed
262 263 264
instead of no action."
  :type 'hook
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
265

266 267 268 269 270
(defvar mail-citation-header nil
  "While running `mail-citation-hook', this variable holds the message header.
This enables the hook functions to see the whole message header
regardless of what part of it (if any) is included in the cited text.")

271
;;;###autoload
272
(defcustom mail-citation-prefix-regexp
273
  (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
274
  "Regular expression to match a citation prefix plus whitespace.
275 276 277 278 279
It should match whatever sort of citation prefixes you want to handle,
with whitespace before and after; it should also match just whitespace.
The default value matches citations like `foo-bar>' plus whitespace."
  :type 'regexp
  :group 'sendmail
280
  :version "24.1")
281

Richard M. Stallman's avatar
Richard M. Stallman committed
282
(defvar mail-abbrevs-loaded nil)
283 284
(defvar mail-mode-map
  (let ((map (make-sparse-keymap)))
285
    (define-key map "\M-\t" 'completion-at-point)
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    (define-key map "\C-c?" 'describe-mode)
    (define-key map "\C-c\C-f\C-t" 'mail-to)
    (define-key map "\C-c\C-f\C-b" 'mail-bcc)
    (define-key map "\C-c\C-f\C-f" 'mail-fcc)
    (define-key map "\C-c\C-f\C-c" 'mail-cc)
    (define-key map "\C-c\C-f\C-s" 'mail-subject)
    (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
    (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to)    ; author
    (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
    (define-key map "\C-c\C-t" 'mail-text)
    (define-key map "\C-c\C-y" 'mail-yank-original)
    (define-key map "\C-c\C-r" 'mail-yank-region)
    (define-key map [remap split-line] 'mail-split-line)
    (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
    (define-key map "\C-c\C-w" 'mail-signature)
    (define-key map "\C-c\C-c" 'mail-send-and-exit)
    (define-key map "\C-c\C-s" 'mail-send)
    (define-key map "\C-c\C-i" 'mail-attach-file)
Glenn Morris's avatar
Glenn Morris committed
304 305
    ;; FIXME add this? "b" = bury buffer.  It's in the menu-bar.
;;;    (define-key map "\C-c\C-b" 'mail-dont-send)
306 307 308 309

    (define-key map [menu-bar mail]
      (cons "Mail" (make-sparse-keymap "Mail")))

310 311 312
    (define-key map [menu-bar mail attachment]
      '("Attach File" . mail-add-attachment))

313 314 315 316
    (define-key map [menu-bar mail fill]
      '("Fill Citation" . mail-fill-yanked-message))

    (define-key map [menu-bar mail yank]
Glenn Morris's avatar
Glenn Morris committed
317
      '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

    (define-key map [menu-bar mail signature]
      '("Insert Signature" . mail-signature))

    (define-key map [menu-bar mail mail-sep]
      '("--"))

    (define-key map [menu-bar mail cancel]
      '("Cancel" . mail-dont-send))

    (define-key map [menu-bar mail send-stay]
      '("Send, Keep Editing" . mail-send))

    (define-key map [menu-bar mail send]
      '("Send Message" . mail-send-and-exit))

    (define-key map [menu-bar headers]
      (cons "Headers" (make-sparse-keymap "Move to Header")))

    (define-key map [menu-bar headers text]
      '("Text" . mail-text))

    (define-key map [menu-bar headers expand-aliases]
      '("Expand Aliases" . expand-mail-aliases))

    (define-key map [menu-bar headers mail-reply-to]
Glenn Morris's avatar
Glenn Morris committed
344
      '("Mail-Reply-To" . mail-mail-reply-to))
345 346

    (define-key map [menu-bar headers mail-followup-to]
Glenn Morris's avatar
Glenn Morris committed
347
      '("Mail-Followup-To" . mail-mail-followup-to))
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

    (define-key map [menu-bar headers reply-to]
      '("Reply-To" . mail-reply-to))

    (define-key map [menu-bar headers bcc]
      '("Bcc" . mail-bcc))

    (define-key map [menu-bar headers fcc]
      '("Fcc" . mail-fcc))

    (define-key map [menu-bar headers cc]
      '("Cc" . mail-cc))

    (define-key map [menu-bar headers subject]
      '("Subject" . mail-subject))

    (define-key map [menu-bar headers to]
      '("To" . mail-to))

    map))
Richard M. Stallman's avatar
Richard M. Stallman committed
368

369
(autoload 'build-mail-aliases "mailalias"
Glenn Morris's avatar
Glenn Morris committed
370
  "Read mail aliases from personal aliases file and set `mail-aliases'.
371
By default, this is the file specified by `mail-personal-alias-file'." t)
372

Jim Blandy's avatar
Jim Blandy committed
373
;;;###autoload
374
(defcustom mail-signature t
375
  "Text inserted at end of mail buffer when a message is initialized.
376 377 378 379 380 381
If t, it means to insert the contents of the file `mail-signature-file'.
If a string, that string is inserted.
 (To make a proper signature, the string should begin with \\n\\n-- \\n,
  which is the standard way to delimit a signature in a message.)
Otherwise, it should be an expression; it is evaluated
and should insert whatever you want to insert."
382
  :type '(choice (const :tag "None" nil)
383 384 385
		 (const :tag "Use `.signature' file" t)
		 (string :tag "String to insert")
		 (sexp :tag "Expression to evaluate"))
Stephen Eglen's avatar
Stephen Eglen committed
386
  :group 'sendmail)
387
(put 'mail-signature 'risky-local-variable t)
388

389
;;;###autoload
390
(defcustom mail-signature-file (purecopy "~/.signature")
391
  "File containing the text inserted at end of mail buffer."
Stephen Eglen's avatar
Stephen Eglen committed
392 393
  :type 'file
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
394

395
;;;###autoload
396
(defcustom mail-default-directory (purecopy "~/")
397 398 399 400 401
  "Value of `default-directory' for Mail mode buffers.
This directory is used for auto-save files of Mail mode buffers.

Note that Message mode does not use this variable; it auto-saves
in `message-auto-save-directory'."
402
  :type '(directory :tag "Directory")
403
  :group 'sendmail
404
  :version "22.1")
405

406
(defvar mail-reply-action nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
407 408
(defvar mail-send-actions nil
  "A list of actions to be performed upon successful sending of a message.")
409
(defvar mail-return-action nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
410

411
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
412
(defcustom mail-default-headers nil
413
  "A string containing header lines, to be inserted in outgoing messages.
414 415
It can contain newlines, and should end in one.  It is inserted
before you edit the message, so you can edit or delete the lines."
Stephen Eglen's avatar
Stephen Eglen committed
416 417
  :type '(choice (const nil) string)
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
418

Stephen Eglen's avatar
Stephen Eglen committed
419
(defcustom mail-bury-selects-summary t
420
  "If non-nil, try to show Rmail summary buffer after returning from mail.
421
The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
422
the Rmail summary buffer before returning, if it exists and this variable
Stephen Eglen's avatar
Stephen Eglen committed
423 424 425
is non-nil."
  :type 'boolean
  :group 'sendmail)
426

427
(defcustom mail-send-nonascii 'mime
428
  "Specify whether to allow sending non-ASCII characters in mail.
429 430
If t, that means do allow it.  nil means don't allow it.
`query' means ask the user each time.
431 432
`mime' means add an appropriate MIME header if none already present.
The default is `mime'.
433
Including non-ASCII characters in a mail message can be problematical
Stephen Eglen's avatar
Stephen Eglen committed
434
for the recipient, who may not know how to decode them properly."
435
  :type '(choice (const t) (const nil) (const query) (const mime))
Stephen Eglen's avatar
Stephen Eglen committed
436
  :group 'sendmail)
437

438
(defcustom mail-use-dsn nil
439
  "Ask MTA for notification of failed, delayed or successful delivery.
440 441 442 443 444 445
Note that only some MTAs (currently only recent versions of Sendmail)
support Delivery Status Notification."
  :group 'sendmail
  :type '(repeat (radio (const :tag "Failure" failure)
			(const :tag "Delay" delay)
			(const :tag "Success" success)))
446
  :version "22.1")
447

448 449
;; Note: could use /usr/ucb/mail instead of sendmail;
;; options -t, and -v if not interactive.
Glenn Morris's avatar
Glenn Morris committed
450
(defvar mail-mailer-swallows-blank-line nil
451
  "Set this non-nil if the system's mailer runs the header and body together.
Glenn Morris's avatar
Glenn Morris committed
452 453 454 455 456 457 458
The actual value should be an expression to evaluate that returns
non-nil if the problem will actually occur.
\(As far as we know, this is not an issue on any system still supported
by Emacs.)")

(put 'mail-mailer-swallows-blank-line 'risky-local-variable t) ; gets evalled
(make-obsolete-variable 'mail-mailer-swallows-blank-line
459 460
			"no need to set this on any modern system."
                        "24.1" 'set)
461

462
(defvar mail-mode-syntax-table
Glenn Morris's avatar
Glenn Morris committed
463
  ;; define-derived-mode will make it inherit from text-mode-syntax-table.
464
  (let ((st (make-syntax-table)))
Glenn Morris's avatar
Glenn Morris committed
465 466 467 468
    ;; FIXME this is probably very obsolete now ("percent hack").
    ;; sending.texi used to say:
    ;;   Mail mode defines the character `%' as a word separator; this
    ;;   is helpful for using the word commands to edit mail addresses.
469 470 471
    (modify-syntax-entry ?% ". " st)
    st)
  "Syntax table used while in `mail-mode'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
472

473
(defvar mail-font-lock-keywords
474
  (eval-when-compile
475
    (let* ((cite-chars "[>|}]")
476
	   (cite-prefix "[:alpha:]")
477
	   (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
Simon Marshall's avatar
Simon Marshall committed
478
      (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
479
	    '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
480
	    '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
481 482 483
	      (1 font-lock-comment-face)
;;	      (2 font-lock-type-face nil t)
	      )
484
	    ;; Use EVAL to delay in case `mail-header-separator' gets changed.
Simon Marshall's avatar
Simon Marshall committed
485
	    '(eval .
486
	      (let ((separator (if (zerop (length mail-header-separator))
487
				   " \\`\\' "
488 489
				 (regexp-quote mail-header-separator))))
		(cons (concat "^" separator "$") 'font-lock-warning-face)))
490 491 492
	    ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
	    `(,cite-chars
	      (,(concat "\\=[ \t]*"
493 494
			"\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
			"\\(" cite-chars "[ \t]*\\)\\)+\\)"
Simon Marshall's avatar
Simon Marshall committed
495
			"\\(.*\\)")
496
	       (beginning-of-line) (end-of-line)
497 498
	       (1 font-lock-comment-delimiter-face nil t)
	       (5 font-lock-comment-face nil t)))
499
	    '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
500
	      . font-lock-string-face))))
501 502
  "Additional expressions to highlight in Mail mode.")

503

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
;;;###autoload
(defun sendmail-query-once ()
  "Query for `send-mail-function' and send mail with it.
This also saves the value of `send-mail-function' via Customize."
  (let* ((mail-buffer (current-buffer))
	 ;; Compute default mail sender, preferring smtpmail if it's
	 ;; already configured.
	 (default (cond
		   ((and (boundp 'smtpmail-smtp-server)
			 smtpmail-smtp-server)
		    'smtpmail-send-it)
		   ((or (and window-system (eq system-type 'darwin))
			(eq system-type 'windows-nt))
		    'mailclient-send-it)
		   ((and sendmail-program
			 (executable-find sendmail-program))
		    'sendmail-send-it)))
	 (send-function (if (eq default 'smtpmail-send-it)
			    'smtpmail-send-it)))
    (unless send-function
      ;; Query the user.
      (with-temp-buffer
	(rename-buffer "*Mail Help*" t)
	(erase-buffer)
	(insert "Emacs has not been set up for sending mail.\n
Type `y' to configure and use Emacs as a mail client,
or `n' to use your system's default mailer.\n
To change your decision later, customize `send-mail-function'.\n")
	(goto-char (point-min))
	(display-buffer (current-buffer))
	(if (y-or-n-p "Set up Emacs for sending SMTP mail? ")
	    ;; FIXME: We should check and correct the From: field too.
	    (setq send-function 'smtpmail-send-it)
	  (setq send-function default))))
    (when send-function
      (customize-save-variable 'send-mail-function send-function)
      ;; HACK: Message mode stupidly has `message-send-mail-function',
      ;; so we must update it too or sending again in the current
      ;; Emacs session will still call `sendmail-query-once'.
      (and (boundp 'message-send-mail-function)
	   (eq message-send-mail-function 'sendmail-query-once)
	   (customize-set-variable 'message-send-mail-function
				   send-function))
      (funcall send-function))))

549
(defun sendmail-sync-aliases ()
Kenichi Handa's avatar
Kenichi Handa committed
550 551 552 553 554
  (when mail-personal-alias-file
    (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
      (or (equal mail-alias-modtime modtime)
	  (setq mail-alias-modtime modtime
		mail-aliases t)))))
555

556 557 558 559 560 561 562 563 564 565 566 567

;;;###autoload
(define-mail-user-agent 'sendmail-user-agent
  'sendmail-user-agent-compose
  'mail-send-and-exit)

;;;###autoload
(defun sendmail-user-agent-compose (&optional to subject other-headers
				    continue switch-function yank-action
				    send-actions return-action
				    &rest ignored)
  (if switch-function
568
      (funcall switch-function "*mail*"))
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
  (let ((cc (cdr (assoc-string "cc" other-headers t)))
	(in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
	(body (cdr (assoc-string "body" other-headers t))))
    (or (mail continue to subject in-reply-to cc yank-action
	      send-actions return-action)
	continue
	(error "Message aborted"))
    (save-excursion
      (rfc822-goto-eoh)
      (while other-headers
	(unless (member-ignore-case (car (car other-headers))
				    '("in-reply-to" "cc" "body"))
	  (insert (car (car other-headers)) ": "
		  (cdr (car other-headers))
		  (if use-hard-newlines hard-newline "\n")))
	(setq other-headers (cdr other-headers)))
      (when body
	(forward-line 1)
	(insert body))
      t)))

(defun mail-setup (to subject in-reply-to cc replybuffer
		   actions return-action)
592
  (or mail-default-reply-to
593
      (setq mail-default-reply-to (getenv "REPLYTO")))
594
  (sendmail-sync-aliases)
Glenn Morris's avatar
Glenn Morris committed
595 596 597 598 599
  (when (eq mail-aliases t)
    (setq mail-aliases nil)
    (and mail-personal-alias-file
	 (file-exists-p mail-personal-alias-file)
	 (build-mail-aliases)))
600
  ;; Don't leave this around from a previous message.
601
  (kill-local-variable 'buffer-file-coding-system)
602 603
  ;; This doesn't work for enable-multibyte-characters.
  ;; (kill-local-variable 'enable-multibyte-characters)
604
  (set-buffer-multibyte (default-value 'enable-multibyte-characters))
605 606
  (if current-input-method
      (inactivate-input-method))
607 608

  ;; Local variables for Mail mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
609
  (setq mail-send-actions actions)
610
  (setq mail-reply-action replybuffer)
611 612
  (setq mail-return-action return-action)

Richard M. Stallman's avatar
Richard M. Stallman committed
613
  (goto-char (point-min))
614 615
  (if mail-setup-with-from
      (mail-insert-from-field))
Richard M. Stallman's avatar
Richard M. Stallman committed
616 617 618
  (insert "To: ")
  (save-excursion
    (if to
619 620 621 622 623
	;; Here removed code to extract names from within <...>
	;; on the assumption that mail-strip-quoted-names
	;; has been called and has done so.
	(let ((fill-prefix "\t")
	      (address-start (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
624
	  (insert to "\n")
625 626 627 628
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline)))
Richard M. Stallman's avatar
Richard M. Stallman committed
629 630
      (newline))
    (if cc
631 632 633
	(let ((fill-prefix "\t")
	      (address-start (progn (insert "CC: ") (point))))
	  (insert cc "\n")
634 635 636 637
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline))))
Richard M. Stallman's avatar
Richard M. Stallman committed
638
    (if in-reply-to
639
	(let ((fill-prefix "\t")
640
	      (fill-column 78)
641 642
	      (address-start (point)))
	  (insert "In-reply-to: " in-reply-to "\n")
643 644 645 646
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline))))
Richard M. Stallman's avatar
Richard M. Stallman committed
647 648 649 650 651 652
    (insert "Subject: " (or subject "") "\n")
    (if mail-default-headers
	(insert mail-default-headers))
    (if mail-default-reply-to
	(insert "Reply-to: " mail-default-reply-to "\n"))
    (if mail-self-blind
653
	(insert "BCC: " user-mail-address "\n"))
Richard M. Stallman's avatar
Richard M. Stallman committed
654 655
    (if mail-archive-file-name
	(insert "FCC: " mail-archive-file-name "\n"))
656 657 658 659 660
    (put-text-property (point)
		       (progn
			 (insert mail-header-separator "\n")
			 (1- (point)))
		       'category 'mail-header-separator)
661 662
    ;; Insert the signature.  But remember the beginning of the message.
    (if to (setq to (point)))
Glenn Morris's avatar
Glenn Morris committed
663
    (if mail-signature (mail-signature t))
Richard M. Stallman's avatar
Richard M. Stallman committed
664 665
    (goto-char (point-max))
    (or (bolp) (newline)))
666
  (if to (goto-char to))
Richard M. Stallman's avatar
Richard M. Stallman committed
667 668 669
  (or to subject in-reply-to
      (set-buffer-modified-p nil))
  (run-hooks 'mail-setup-hook))
670

671
(defcustom mail-mode-hook nil
Glenn Morris's avatar
Glenn Morris committed
672 673 674
  "Hook run by Mail mode.
When composing a mail, this runs immediately after creating, or
switching to, the `*mail*' buffer.  See also `mail-setup-hook'."
675 676 677 678
  :group 'sendmail
  :type 'hook
  :options '(footnote-mode))

679
(defvar mail-mode-abbrev-table text-mode-abbrev-table)
Eli Zaretskii's avatar
Eli Zaretskii committed
680
(defvar mail-encode-mml)
Richard M. Stallman's avatar
Richard M. Stallman committed
681
;;;###autoload
682
(define-derived-mode mail-mode text-mode "Mail"
Richard M. Stallman's avatar
Richard M. Stallman committed
683 684
  "Major mode for editing mail to be sent.
Like Text Mode but with these additional commands:
Reiner Steib's avatar
Reiner Steib committed
685 686 687 688

\\[mail-send]  mail-send (send the message)
\\[mail-send-and-exit]  mail-send-and-exit (send the message and exit)

Karl Heuer's avatar
Karl Heuer committed
689
Here are commands that move to a header field (and create it if there isn't):
690 691
	 \\[mail-to]  move to To:	\\[mail-subject]  move to Subj:
	 \\[mail-bcc]  move to BCC:	\\[mail-cc]  move to CC:
692
	 \\[mail-fcc]  move to FCC:	\\[mail-reply-to] move to Reply-To:
693 694
         \\[mail-mail-reply-to]  move to Mail-Reply-To:
         \\[mail-mail-followup-to] move to Mail-Followup-To:
695
\\[mail-text]  move to message text.
Karl Heuer's avatar
Karl Heuer committed
696 697 698
\\[mail-signature]  mail-signature (insert `mail-signature-file' file).
\\[mail-yank-original]  mail-yank-original (insert current message, in Rmail).
\\[mail-fill-yanked-message]  mail-fill-yanked-message (fill what was yanked).
699 700
\\[mail-insert-file] insert a text file into the message.
\\[mail-add-attachment] attach to the message a file as binary attachment.
Eli Zaretskii's avatar
Eli Zaretskii committed
701 702
Turning on Mail mode runs the normal hooks `text-mode-hook' and
`mail-mode-hook' (in that order)."
703
  (make-local-variable 'mail-reply-action)
Richard M. Stallman's avatar
Richard M. Stallman committed
704
  (make-local-variable 'mail-send-actions)
705
  (make-local-variable 'mail-return-action)
Eli Zaretskii's avatar
Eli Zaretskii committed
706 707
  (make-local-variable 'mail-encode-mml)
  (setq mail-encode-mml nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
708
  (setq buffer-offer-save t)
709
  (make-local-variable 'font-lock-defaults)
710
  (setq font-lock-defaults '(mail-font-lock-keywords t t))
Richard M. Stallman's avatar
Richard M. Stallman committed
711
  (make-local-variable 'paragraph-separate)
712 713
  (make-local-variable 'normal-auto-fill-function)
  (setq normal-auto-fill-function 'mail-mode-auto-fill)
714
  (make-local-variable 'fill-paragraph-function)
715
  (setq fill-paragraph-function 'mail-mode-fill-paragraph)
716 717 718
  ;; Allow using comment commands to add/remove quoting (this only does
  ;; anything if mail-yank-prefix is set to a non-nil value).
  (set (make-local-variable 'comment-start) mail-yank-prefix)
Kenichi Handa's avatar
Kenichi Handa committed
719 720 721
  (if mail-yank-prefix
      (set (make-local-variable 'comment-start-skip)
	   (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
722 723
  (make-local-variable 'adaptive-fill-regexp)
  (setq adaptive-fill-regexp
724
	(concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
725
		adaptive-fill-regexp))
726
  (make-local-variable 'adaptive-fill-first-line-regexp)
727
  (setq adaptive-fill-first-line-regexp
728
	(concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
729
		adaptive-fill-first-line-regexp))
730 731
  (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
            nil 'local)
732 733 734 735
  ;; `-- ' precedes the signature.  `-----' appears at the start of the
  ;; lines that delimit forwarded messages.
  ;; Lines containing just >= 3 dashes, perhaps after whitespace,
  ;; are also sometimes used and should be separators.
736
  (setq paragraph-separate (concat (regexp-quote mail-header-separator)
737
				"$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
738
				"\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
739 740
				"--\\( \\|-+\\)$\\|"
				page-delimiter)))
741

742 743 744

(defun mail-header-end ()
  "Return the buffer location of the end of headers, as a number."
745 746 747 748 749
  (save-restriction
    (widen)
    (save-excursion
      (rfc822-goto-eoh)
      (point))))
750 751 752

(defun mail-text-start ()
  "Return the buffer location of the start of text, as a number."
753 754 755 756 757 758
  (save-restriction
    (widen)
    (save-excursion
      (rfc822-goto-eoh)
      (forward-line 1)
      (point))))
759 760 761 762 763 764 765 766 767 768 769 770 771 772

(defun mail-sendmail-delimit-header ()
  "Set up whatever header delimiter convention sendmail will use.
Concretely: replace the first blank line in the header with the separator."
  (rfc822-goto-eoh)
  (insert mail-header-separator)
  (point))

(defun mail-sendmail-undelimit-header ()
  "Remove header separator to put the message in correct form for sendmail.
Leave point at the start of the delimiter line."
  (rfc822-goto-eoh)
  (delete-region (point) (progn (end-of-line) (point))))

773 774 775
(defun mail-mode-auto-fill ()
  "Carry out Auto Fill for Mail mode.
If within the headers, this makes the new lines into continuation lines."
776
  (if (< (point) (mail-header-end))
777
      (let ((old-line-start (line-beginning-position)))
778 779 780 781
	(if (do-auto-fill)
	    (save-excursion
	      (beginning-of-line)
	      (while (not (eq (point) old-line-start))
782 783 784
		;; Use insert-before-markers in case we're inserting
		;; before the saved value of point (which is common).
		(insert-before-markers "   ")
785 786 787
		(forward-line -1))
	      t)))
    (do-auto-fill)))
788 789 790

(defun mail-mode-fill-paragraph (arg)
  ;; Do something special only if within the headers.
791
  (if (< (point) (mail-header-end))
792
      (let (beg end fieldname)
793 794
	(when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
		(setq beg (point)))
795
	(setq fieldname
796
		(downcase (buffer-substring beg (1- (match-end 0))))))
797 798 799 800 801 802 803 804 805 806 807
	(forward-line 1)
	;; Find continuation lines and get rid of their continuation markers.
	(while (looking-at "[ \t]")
	  (delete-horizontal-space)
	  (forward-line 1))
	(setq end (point-marker))
	(goto-char beg)
	;; If this field contains addresses,
	;; make sure we can fill after each address.
	(if (member fieldname
		    '("to" "cc" "bcc" "from" "reply-to"
808
		      "mail-reply-to" "mail-followup-to"
809 810 811 812 813
		      "resent-to" "resent-cc" "resent-bcc"
		      "resent-from" "resent-reply-to"))
	    (while (search-forward "," end t)
	      (or (looking-at "[ \t]")
		  (insert " "))))
814
	(fill-region-as-paragraph beg end arg)
815 816 817 818 819 820 821 822
	;; Mark all lines except the first as continuations.
	(goto-char beg)
	(forward-line 1)
	(while (< (point) end)
	  (insert "  ")
	  (forward-line 1))
	(move-marker end nil)
	t)))
823

824 825
;; User-level commands for sending.

826
(defun mail-send-and-exit (&optional arg)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
827
  "Send message like `mail-send', then, if no errors, exit from mail buffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
828 829 830
Prefix arg means don't delete this window."
  (interactive "P")
  (mail-send)
831 832
  (mail-bury arg))

833
(defun mail-dont-send (&optional arg)
834 835 836 837 838
  "Don't send the message you have been editing.
Prefix arg means don't delete this window."
  (interactive "P")
  (mail-bury arg))

839
(defun mail-bury (&optional arg)
840
  "Bury this mail buffer."
841 842 843
  (let ((newbuf (other-buffer (current-buffer)))
	(return-action mail-return-action)
	some-rmail)
844
    (bury-buffer (current-buffer))
845 846 847 848 849 850 851 852
    ;; If there is an Rmail buffer, return to it nicely
    ;; even if this message was not started by an Rmail command.
    (unless return-action
      (dolist (buffer (buffer-list))
	(if (eq (buffer-local-value 'major-mode buffer) 'rmail-mode)
	    (setq return-action `(rmail-mail-return ,newbuf)))))
    (if (and (null arg) return-action)
	(apply (car return-action) (cdr return-action))
853
      (switch-to-buffer newbuf))))
Richard M. Stallman's avatar
Richard M. Stallman committed
854

855
(defcustom mail-send-hook nil
856
  "Hook run just before sending a message."
857 858 859 860
  :type 'hook
  :options '(flyspell-mode-off)
  :group 'sendmail)

861
;;;###autoload
862 863
(defcustom mail-mailing-lists nil
"List of mailing list addresses the user is subscribed to.
864 865 866 867 868
The variable is used to trigger insertion of the \"Mail-Followup-To\"
header when sending a message to a mailing list."
  :type '(repeat string)
  :group 'sendmail)