sendmail.el 73 KB
Newer Older
1
;;; sendmail.el --- mail sending commands for Emacs
Eric S. Raymond's avatar
Eric S. Raymond committed
2

3
;; Copyright (C) 1985-1986, 1992-1996, 1998, 2000-2014 Free Software
4
;; 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 144 145 146 147
(defcustom send-mail-function
  ;; Assume smtpmail is the preferred choice if it's already configured.
  (if (and (boundp 'smtpmail-smtp-server)
           smtpmail-smtp-server)
      'smtpmail-send-it 'sendmail-query-once)
Dave Love's avatar
Dave Love committed
148
  "Function to call to send the current buffer as mail.
149
The headers should be delimited by a line which is
150 151
not a valid RFC822 header or continuation line,
that matches the variable `mail-header-separator'.
Dave Love's avatar
Dave Love committed
152 153
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
154
  :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
155
		(function-item sendmail-query-once :tag "Query the user")
Dave Love's avatar
Dave Love committed
156 157
		(function-item smtpmail-send-it :tag "Use SMTPmail package")
		(function-item feedmail-send-it :tag "Use Feedmail package")
158
		(function-item mailclient-send-it :tag "Use Mailclient package")
Dave Love's avatar
Dave Love committed
159
		function)
160
  :version "24.1"
Dave Love's avatar
Dave Love committed
161
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
162 163

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

169 170
;; Set up mail-header-separator for use as a category text property.
(put 'mail-header-separator 'rear-nonsticky '(category))
171 172 173 174 175 176
;; 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)
177

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

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

Stephen Eglen's avatar
Stephen Eglen committed
194
(defcustom mail-alias-file nil
Glenn Morris's avatar
Glenn Morris committed
195
  "If non-nil, the name of a file to use instead of the sendmail default.
Richard M. Stallman's avatar
Richard M. Stallman committed
196 197
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
198 199 200 201
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
202
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
203

204
;;;###autoload
205
(defcustom mail-personal-alias-file (purecopy "~/.mailrc")
206
  "If non-nil, the name of the user's personal mail alias file.
207 208
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
209 210 211
This file need not actually exist."
  :type '(choice (const nil) file)
  :group 'sendmail)
212

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

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

229
(defvar mail-alias-modtime nil
230
  "The modification time of your mail alias file when it was last examined.")
231

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

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

246
(defvar mail-yank-hooks nil
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.
Stefan Monnier's avatar
Stefan Monnier committed
252 253
It is obsolete and mail agents should no longer use it.")
(make-obsolete-variable 'mail-yank-hooks 'mail-citation-hook "19.34")
254

255
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
256
(defcustom mail-citation-hook nil
257
  "Hook for modifying a citation just inserted in the mail buffer.
258 259 260 261 262
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.
263

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

269 270 271 272 273
(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.")

274
;;;###autoload
275
(defcustom mail-citation-prefix-regexp
276
  (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
277
  "Regular expression to match a citation prefix plus whitespace.
278 279 280 281 282
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
283
  :version "24.1")
284

Richard M. Stallman's avatar
Richard M. Stallman committed
285
(defvar mail-abbrevs-loaded nil)
286 287
(defvar mail-mode-map
  (let ((map (make-sparse-keymap)))
288
    (define-key map "\M-\t" 'completion-at-point)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    (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)
306
    (define-key map "\C-c\C-i" 'mail-insert-file)
Glenn Morris's avatar
Glenn Morris committed
307 308
    ;; FIXME add this? "b" = bury buffer.  It's in the menu-bar.
;;;    (define-key map "\C-c\C-b" 'mail-dont-send)
309 310 311 312

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

313 314 315
    (define-key map [menu-bar mail attachment]
      '("Attach File" . mail-add-attachment))

316 317 318 319
    (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
320
      '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

    (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
347
      '("Mail-Reply-To" . mail-mail-reply-to))
348 349

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

    (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
371

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

Jim Blandy's avatar
Jim Blandy committed
376
;;;###autoload
377
(defcustom mail-signature t
378
  "Text inserted at end of mail buffer when a message is initialized.
379 380 381 382 383 384
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."
385
  :type '(choice (const :tag "None" nil)
386 387 388
		 (const :tag "Use `.signature' file" t)
		 (string :tag "String to insert")
		 (sexp :tag "Expression to evaluate"))
Stephen Eglen's avatar
Stephen Eglen committed
389
  :group 'sendmail)
390
(put 'mail-signature 'risky-local-variable t)
391

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

398
;;;###autoload
399
(defcustom mail-default-directory (purecopy "~/")
400 401 402 403 404
  "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'."
405
  :type '(directory :tag "Directory")
406
  :group 'sendmail
407
  :version "22.1")
408

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

414
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
415
(defcustom mail-default-headers nil
416
  "A string containing header lines, to be inserted in outgoing messages.
417 418
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
419 420
  :type '(choice (const nil) string)
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
421

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

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

441
(defcustom mail-use-dsn nil
442
  "Ask MTA for notification of failed, delayed or successful delivery.
443 444 445 446 447 448
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)))
449
  :version "22.1")
450

451 452
;; Note: could use /usr/ucb/mail instead of sendmail;
;; options -t, and -v if not interactive.
Glenn Morris's avatar
Glenn Morris committed
453
(defvar mail-mailer-swallows-blank-line nil
454
  "Set this non-nil if the system's mailer runs the header and body together.
Glenn Morris's avatar
Glenn Morris committed
455 456 457 458 459 460 461
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
462 463
			"no need to set this on any modern system."
                        "24.1" 'set)
464

465
(defvar mail-mode-syntax-table
Glenn Morris's avatar
Glenn Morris committed
466
  ;; define-derived-mode will make it inherit from text-mode-syntax-table.
467
  (let ((st (make-syntax-table)))
Glenn Morris's avatar
Glenn Morris committed
468 469 470 471
    ;; 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.
472 473 474
    (modify-syntax-entry ?% ". " st)
    st)
  "Syntax table used while in `mail-mode'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
475

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

506

507 508 509 510
;;;###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."
511 512 513 514
  ;; If send-mail-function is already setup, we're incorrectly called
  ;; a second time, probably because someone's using an old value
  ;; of send-mail-function.
  (when (eq send-mail-function 'sendmail-query-once)
515 516 517 518 519 520 521 522 523 524 525 526 527 528
    (sendmail-query-user-about-smtp))
  (funcall send-mail-function))

(defun sendmail-query-user-about-smtp ()
  (let* ((options `(("mail client" . mailclient-send-it)
		    ,@(when (and sendmail-program
				 (executable-find sendmail-program))
			'(("transport" . sendmail-send-it)))
		    ("smtp" . smtpmail-send-it)))
	 (choice
	  ;; Query the user.
	  (with-temp-buffer
	    (rename-buffer "*Emacs Mail Setup Help*" t)
	    (insert "\
529 530 531 532
 Emacs is about to send an email message, but it has not been
 configured for sending email.  To tell Emacs how to send email:

 - Type `"
533 534
		    (propertize "mail client" 'face 'bold)
		    "' to start your default email client and
535
   pass it the message text.\n\n")
536 537 538
	    (and sendmail-program
		 (executable-find sendmail-program)
		 (insert "\
539
 - Type `"
540 541
			 (propertize "transport" 'face 'bold)
			 "' to invoke the system's mail transport agent
542
   (the `"
543 544 545
			 sendmail-program
			 "' program).\n\n"))
	    (insert "\
546
 - Type `"
547 548
		    (propertize "smtp" 'face 'bold)
		    "' to send mail directly to an \"outgoing mail\" server.
549
   (Emacs may prompt you for SMTP settings).
550

551 552
 Emacs will record your selection and will use it thereafter.
 To change it later, customize the option `send-mail-function'.\n")
553 554 555 556 557 558 559
	    (goto-char (point-min))
	    (display-buffer (current-buffer))
	    (let ((completion-ignore-case t))
	      (completing-read "Send mail via: "
			       options nil 'require-match)))))
    (customize-save-variable 'send-mail-function
			     (cdr (assoc-string choice options t)))))
560

561
(defun sendmail-sync-aliases ()
Kenichi Handa's avatar
Kenichi Handa committed
562 563 564 565 566
  (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)))))
567

568 569 570 571 572 573 574 575 576 577 578 579

;;;###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
580
      (funcall switch-function "*mail*"))
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
  (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)
604
  (or mail-default-reply-to
605
      (setq mail-default-reply-to (getenv "REPLYTO")))
606
  (sendmail-sync-aliases)
Glenn Morris's avatar
Glenn Morris committed
607 608 609 610 611
  (when (eq mail-aliases t)
    (setq mail-aliases nil)
    (and mail-personal-alias-file
	 (file-exists-p mail-personal-alias-file)
	 (build-mail-aliases)))
612
  ;; Don't leave this around from a previous message.
613
  (kill-local-variable 'buffer-file-coding-system)
614 615
  ;; This doesn't work for enable-multibyte-characters.
  ;; (kill-local-variable 'enable-multibyte-characters)
616
  (set-buffer-multibyte (default-value 'enable-multibyte-characters))
617
  (if current-input-method
618
      (deactivate-input-method))
619 620

  ;; Local variables for Mail mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
621
  (setq mail-send-actions actions)
622
  (setq mail-reply-action replybuffer)
623 624
  (setq mail-return-action return-action)

Richard M. Stallman's avatar
Richard M. Stallman committed
625
  (goto-char (point-min))
626 627
  (if mail-setup-with-from
      (mail-insert-from-field))
Richard M. Stallman's avatar
Richard M. Stallman committed
628 629 630
  (insert "To: ")
  (save-excursion
    (if to
631 632 633 634 635
	;; 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
636
	  (insert to "\n")
637 638 639 640
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline)))
Richard M. Stallman's avatar
Richard M. Stallman committed
641 642
      (newline))
    (if cc
643 644 645
	(let ((fill-prefix "\t")
	      (address-start (progn (insert "CC: ") (point))))
	  (insert cc "\n")
646 647 648 649
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline))))
Richard M. Stallman's avatar
Richard M. Stallman committed
650
    (if in-reply-to
651
	(let ((fill-prefix "\t")
652
	      (fill-column 78)
653 654
	      (address-start (point)))
	  (insert "In-reply-to: " in-reply-to "\n")
655 656 657 658
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline))))
Richard M. Stallman's avatar
Richard M. Stallman committed
659 660 661 662 663 664
    (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
665
	(insert "BCC: " user-mail-address "\n"))
Richard M. Stallman's avatar
Richard M. Stallman committed
666 667
    (if mail-archive-file-name
	(insert "FCC: " mail-archive-file-name "\n"))
668 669 670 671 672
    (put-text-property (point)
		       (progn
			 (insert mail-header-separator "\n")
			 (1- (point)))
		       'category 'mail-header-separator)
673 674
    ;; Insert the signature.  But remember the beginning of the message.
    (if to (setq to (point)))
Glenn Morris's avatar
Glenn Morris committed
675
    (if mail-signature (mail-signature t))
Richard M. Stallman's avatar
Richard M. Stallman committed
676 677
    (goto-char (point-max))
    (or (bolp) (newline)))
678
  (if to (goto-char to))
Richard M. Stallman's avatar
Richard M. Stallman committed
679 680 681
  (or to subject in-reply-to
      (set-buffer-modified-p nil))
  (run-hooks 'mail-setup-hook))
682

683
(defcustom mail-mode-hook nil
Glenn Morris's avatar
Glenn Morris committed
684 685 686
  "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'."
687 688 689 690
  :group 'sendmail
  :type 'hook
  :options '(footnote-mode))

691
(defvar mail-mode-abbrev-table text-mode-abbrev-table)
Eli Zaretskii's avatar
Eli Zaretskii committed
692
(defvar mail-encode-mml)
Richard M. Stallman's avatar
Richard M. Stallman committed
693
;;;###autoload
694
(define-derived-mode mail-mode text-mode "Mail"
Richard M. Stallman's avatar
Richard M. Stallman committed
695 696
  "Major mode for editing mail to be sent.
Like Text Mode but with these additional commands:
Reiner Steib's avatar
Reiner Steib committed
697 698 699 700

\\[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
701
Here are commands that move to a header field (and create it if there isn't):
702 703
	 \\[mail-to]  move to To:	\\[mail-subject]  move to Subj:
	 \\[mail-bcc]  move to BCC:	\\[mail-cc]  move to CC:
704
	 \\[mail-fcc]  move to FCC:	\\[mail-reply-to] move to Reply-To:
705 706
         \\[mail-mail-reply-to]  move to Mail-Reply-To:
         \\[mail-mail-followup-to] move to Mail-Followup-To:
707
\\[mail-text]  move to message text.
Karl Heuer's avatar
Karl Heuer committed
708 709 710
\\[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).
711 712
\\[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
713 714
Turning on Mail mode runs the normal hooks `text-mode-hook' and
`mail-mode-hook' (in that order)."
715
  (make-local-variable 'mail-reply-action)
Richard M. Stallman's avatar
Richard M. Stallman committed
716
  (make-local-variable 'mail-send-actions)
717
  (make-local-variable 'mail-return-action)
Eli Zaretskii's avatar
Eli Zaretskii committed
718 719
  (make-local-variable 'mail-encode-mml)
  (setq mail-encode-mml nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
720
  (setq buffer-offer-save t)
721
  (make-local-variable 'font-lock-defaults)
722
  (setq font-lock-defaults '(mail-font-lock-keywords t t))
Richard M. Stallman's avatar
Richard M. Stallman committed
723
  (make-local-variable 'paragraph-separate)
724 725
  (make-local-variable 'normal-auto-fill-function)
  (setq normal-auto-fill-function 'mail-mode-auto-fill)
726
  (make-local-variable 'fill-paragraph-function)
727
  (setq fill-paragraph-function 'mail-mode-fill-paragraph)
728 729 730
  ;; 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
731 732 733
  (if mail-yank-prefix
      (set (make-local-variable 'comment-start-skip)
	   (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
734
  (make-local-variable 'adaptive-fill-regexp)
Glenn Morris's avatar
Glenn Morris committed
735
  ;; Also update the paragraph-separate entry if you change this.
736
  (setq adaptive-fill-regexp
737
	(concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
738
		adaptive-fill-regexp))
739
  (make-local-variable 'adaptive-fill-first-line-regexp)
740
  (setq adaptive-fill-first-line-regexp
741
	(concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
742
		adaptive-fill-first-line-regexp))
743 744
  (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
            nil 'local)
745 746 747 748
  ;; `-- ' 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.
749 750
  (setq paragraph-separate
	(concat (regexp-quote mail-header-separator)
751 752
		;; This is based on adaptive-fill-regexp (presumably
		;; the idea is to allow navigation etc of cited paragraphs).
753 754 755 756
		"$\\|\t*[-–!|#%;>*·•‣⁃◦ ]+$"
		"\\|[ \t]*[-[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
		"--\\( \\|-+\\)$\\|"
		page-delimiter)))
757

758 759 760

(defun mail-header-end ()
  "Return the buffer location of the end of headers, as a number."
761 762 763 764 765
  (save-restriction
    (widen)
    (save-excursion
      (rfc822-goto-eoh)
      (point))))
766 767 768

(defun mail-text-start ()
  "Return the buffer location of the start of text, as a number."
769 770 771 772 773 774
  (save-restriction
    (widen)
    (save-excursion
      (rfc822-goto-eoh)
      (forward-line 1)
      (point))))
775 776 777 778 779 780 781 782 783 784 785 786 787 788

(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))))

789 790 791
(defun mail-mode-auto-fill ()
  "Carry out Auto Fill for Mail mode.
If within the headers, this makes the new lines into continuation lines."
792
  (if (< (point) (mail-header-end))
793
      (let ((old-line-start (line-beginning-position)))
794 795 796 797
	(if (do-auto-fill)
	    (save-excursion
	      (beginning-of-line)
	      (while (not (eq (point) old-line-start))
798 799 800
		;; Use insert-before-markers in case we're inserting
		;; before the saved value of point (which is common).
		(insert-before-markers "   ")
801 802 803
		(forward-line -1))
	      t)))
    (do-auto-fill)))
804 805 806

(defun mail-mode-fill-paragraph (arg)
  ;; Do something special only if within the headers.
807
  (if (< (point) (mail-header-end))
808
      (let (beg end fieldname)
809 810
	(when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
		(setq beg (point)))
811
	(setq fieldname
812
		(downcase (buffer-substring beg (1- (match-end 0))))))
813 814 815 816 817 818 819 820 821 822 823
	(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"
824
		      "mail-reply-to" "mail-followup-to"
825 826 827 828 829
		      "resent-to" "resent-cc" "resent-bcc"
		      "resent-from" "resent-reply-to"))
	    (while (search-forward "," end t)
	      (or (looking-at "[ \t]")
		  (insert " "))))
830
	(fill-region-as-paragraph beg end arg)
831 832 833 834 835 836 837 838
	;; 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)))
839

840 841
;; User-level commands for sending.

842
(defun mail-send-and-exit (&optional arg)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
843
  "Send message like `mail-send', then, if no errors, exit from mail buffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
844 845 846
Prefix arg means don't delete this window."
  (interactive "P")
  (mail-send)
847 848
  (mail-bury arg))

849
(defun mail-dont-send (&optional arg)
850 851 852 853 854
  "Don't send the message you have been editing.
Prefix arg means don't delete this window."
  (interactive "P")
  (mail-bury arg))

855