sendmail.el 68.1 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, 1993, 1994, 1995, 1996, 1998, 2000,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2001, 2002, 2003, 2004, 2005, 2006, 2007 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 11 12
;; This file is part of GNU Emacs.

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

;; 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
Erik Naggum's avatar
Erik Naggum committed
22
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
23 24
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
25

26 27 28 29 30
;;; 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
31
;;; Code:
32
(eval-when-compile
33 34
  ;; Necessary to avoid recursive `require's.
  (provide 'sendmail)
35 36 37
  (require 'rmail)
  (require 'mailalias))

Dave Love's avatar
Dave Love committed
38 39
(autoload 'rfc2047-encode-string "rfc2047")

Stephen Eglen's avatar
Stephen Eglen committed
40 41 42 43
(defgroup sendmail nil
  "Mail sending commands for Emacs."
  :prefix "mail-"
  :group 'mail)
Richard M. Stallman's avatar
Richard M. Stallman committed
44

45 46
(defcustom mail-setup-with-from t
  "Non-nil means insert `From:' field when setting up the message."
47
  :type 'boolean
48 49 50
  :group 'sendmail
  :version "22.1")

51 52 53 54 55 56 57 58 59 60
(defcustom sendmail-program
  (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")
    (t "fakemail"))			;In ../etc, to interface to /bin/mail.
  "Program used to send messages."
  :group 'mail
  :type 'file)

61
;;;###autoload
62 63
(defcustom mail-from-style 'angles
  "Specifies how \"From:\" fields look.
64 65 66 67 68 69

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:
70
	Elvis Parsley <king@grassland.com>
71 72 73 74 75 76 77
If `system-default', allows the mailer to insert its default From field
derived from the envelope-from address.

In old versions of Emacs, the `system-default' setting also caused
Emacs to pass the proper email address from `user-mail-address'
to the mailer to specify the envelope-from address.  But that is now
controlled by a separate variable, `mail-specify-envelope-from'."
78 79
  :type '(choice (const nil) (const parens) (const angles)
		 (const system-default))
80
  :version "20.3"
Stephen Eglen's avatar
Stephen Eglen committed
81
  :group 'sendmail)
82

83
;;;###autoload
84
(defcustom mail-specify-envelope-from nil
85
  "If non-nil, specify the envelope-from address when sending mail.
86
The value used to specify it is whatever is found in
87
the variable `mail-envelope-from', with `user-mail-address' as fallback.
88

89
On most systems, specifying the envelope-from address is a
90 91 92
privileged operation.  This variable affects sendmail and
smtpmail -- if you use feedmail to send mail, see instead the
variable `feedmail-deduce-envelope-from'."
93 94 95 96
  :version "21.1"
  :type 'boolean
  :group 'sendmail)

97
(defcustom mail-envelope-from nil
98
  "If non-nil, designate the envelope-from address when sending mail.
99 100 101 102 103
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)."
104
  :version "21.1"
105
  :type '(choice (string :tag "From-name")
106
		 (const :tag "Use From: header from message" header)
107
		 (const :tag "Use `user-mail-address'" nil))
108 109
  :group 'sendmail)

Richard M. Stallman's avatar
Richard M. Stallman committed
110
;;;###autoload
111 112
(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
113
This is done when the message is initialized,
Stephen Eglen's avatar
Stephen Eglen committed
114 115 116
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
117 118

;;;###autoload
119 120
(defcustom mail-interactive nil
  "Non-nil means when sending a message wait for and display errors.
Stephen Eglen's avatar
Stephen Eglen committed
121 122 123
nil means let mailer mail back a message to report errors."
  :type 'boolean
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
124

125 126 127 128 129 130 131 132
(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"
                        "mail-followup-to") "\\(?:")
          ":")
  "Delete these headers from old message when it's inserted in a reply."
Stephen Eglen's avatar
Stephen Eglen committed
133 134
  :type 'regexp
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
135

136
;; Prevent problems with `window-system' not having the correct value
137 138 139 140 141 142 143 144
;; when loaddefs.el is loaded. `custom-reevaluate-setting' needs the
;; standard value.
;;;###autoload
(put 'send-mail-function 'standard-value
     '((if (and window-system (memq system-type '(darwin windows-nt)))
	   'mailclient-send-it
	 'sendmail-send-it)))

Richard M. Stallman's avatar
Richard M. Stallman committed
145 146
;; Useful to set in site-init.el
;;;###autoload
147
(defcustom send-mail-function
148 149 150
  (if (and window-system (memq system-type '(darwin windows-nt)))
      'mailclient-send-it
    'sendmail-send-it)
Dave Love's avatar
Dave Love committed
151
  "Function to call to send the current buffer as mail.
152
The headers should be delimited by a line which is
153 154
not a valid RFC822 header or continuation line,
that matches the variable `mail-header-separator'.
Dave Love's avatar
Dave Love committed
155 156
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
157 158 159
  :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
		(function-item smtpmail-send-it :tag "Use SMTPmail package")
		(function-item feedmail-send-it :tag "Use Feedmail package")
160
		(function-item mailclient-send-it :tag "Use Mailclient package")
Dave Love's avatar
Dave Love committed
161 162
		function)
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
163 164

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

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

Richard M. Stallman's avatar
Richard M. Stallman committed
179
;;;###autoload
180 181
(defcustom mail-archive-file-name nil
  "Name of file to write all outgoing messages in, or nil for none.
Stephen Eglen's avatar
Stephen Eglen committed
182 183 184
This can be an inbox file or an Rmail file."
  :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

194
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
195
(defcustom mail-alias-file nil
196
  "If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
Richard M. Stallman's avatar
Richard M. Stallman committed
197 198
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.
Stephen Eglen's avatar
Stephen Eglen committed
199 200 201
This variable has no effect unless your system uses sendmail as its mailer."
  :type '(choice (const nil) file)
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
202

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

212
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
213
(defcustom mail-setup-hook nil
Karl Heuer's avatar
Karl Heuer committed
214
  "Normal hook, run each time a new outgoing mail message is initialized.
Stephen Eglen's avatar
Stephen Eglen committed
215 216
The function `mail-setup' runs this hook."
  :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.
Richard M. Stallman's avatar
Richard M. Stallman 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
Stephen Eglen's avatar
Stephen Eglen committed
233
(defcustom mail-yank-prefix nil
234
  "Prefix insert on lines of yanked message being replied to.
Stephen Eglen's avatar
Stephen Eglen committed
235 236 237 238
nil means use indentation."
  :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 252 253 254
  "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.")

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 "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
276
  "Regular expression to match a citation prefix plus whitespace.
277 278 279 280 281 282 283
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
  :version "20.3")

Richard M. Stallman's avatar
Richard M. Stallman committed
284
(defvar mail-abbrevs-loaded nil)
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
(defvar mail-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\M-\t" 'mail-complete)
    (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-v" 'mail-sent-via)
    (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)

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

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

    (define-key map [menu-bar mail yank]
      '("Cite Original" . mail-yank-original))

    (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 sent-via]
      '("Sent Via" . mail-sent-via))

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

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

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

370
(autoload 'build-mail-aliases "mailalias"
371
  "Read mail aliases from user's personal aliases file and set `mail-aliases'."
372 373 374 375 376 377 378 379 380
  nil)

(autoload 'expand-mail-aliases "mailalias"
  "Expand all mail aliases in suitable header fields found between BEG and END.
Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
Optional second arg EXCLUDE may be a regular expression defining text to be
removed from alias expansions."
  nil)

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

397
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
398
(defcustom mail-signature-file "~/.signature"
399
  "File containing the text inserted at end of mail buffer."
Stephen Eglen's avatar
Stephen Eglen committed
400 401
  :type 'file
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
402

403 404
;;;###autoload
(defcustom mail-default-directory "~/"
405
  "Directory for mail buffers.
406 407 408
Value of `default-directory' for mail buffers.
This directory is used for auto-save files of mail buffers."
  :type '(directory :tag "Directory")
409
  :group 'sendmail
410
  :version "22.1")
411

412
(defvar mail-reply-action nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
413 414
(defvar mail-send-actions nil
  "A list of actions to be performed upon successful sending of a message.")
415
(put 'mail-reply-action 'permanent-local t)
416
(put 'mail-send-actions 'permanent-local t)
Richard M. Stallman's avatar
Richard M. Stallman committed
417

418
;;;###autoload
Stephen Eglen's avatar
Stephen Eglen committed
419
(defcustom mail-default-headers nil
420
  "A string containing header lines, to be inserted in outgoing messages.
Richard M. Stallman's avatar
Richard M. Stallman committed
421
It is inserted before you edit the message,
Stephen Eglen's avatar
Stephen Eglen committed
422 423 424
so you can edit or delete these lines."
  :type '(choice (const nil) string)
  :group 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
425

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

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

447
(defcustom mail-use-dsn nil
448
  "Ask MTA for notification of failed, delayed or successful delivery.
449 450 451 452 453 454
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)))
455
  :version "22.1")
456

457 458 459
;; Note: could use /usr/ucb/mail instead of sendmail;
;; options -t, and -v if not interactive.
(defvar mail-mailer-swallows-blank-line
460
  (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
461
	   (file-readable-p "/etc/sendmail.cf")
462 463 464 465 466
           (with-temp-buffer
             (insert-file-contents "/etc/sendmail.cf")
             (goto-char (point-min))
             (let ((case-fold-search nil))
               (re-search-forward "^OR\\>" nil t))))
467 468 469 470 471
      ;; According to RFC822, "The field-name must be composed of printable
      ;; ASCII characters (i.e. characters that have decimal values between
      ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
      ;; space, or colon.
      '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
472 473 474 475 476
  "Set this non-nil if the system's mailer runs the header and body together.
\(This problem exists on Sunos 4 when sendmail is run in remote mode.)
The value should be an expression to test whether the problem will
actually occur.")

477 478 479 480 481 482
(defvar mail-mode-syntax-table
  (let ((st (make-syntax-table)))
    ;; define-derived-mode will make it inherit from text-mode-syntax-table.
    (modify-syntax-entry ?% ". " st)
    st)
  "Syntax table used while in `mail-mode'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
483

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

514

515
(defun sendmail-sync-aliases ()
516 517 518 519 520
  (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)))))
521

Richard M. Stallman's avatar
Richard M. Stallman committed
522
(defun mail-setup (to subject in-reply-to cc replybuffer actions)
523
  (or mail-default-reply-to
524
      (setq mail-default-reply-to (getenv "REPLYTO")))
525
  (sendmail-sync-aliases)
526 527 528
  (if (eq mail-aliases t)
      (progn
	(setq mail-aliases nil)
529 530 531
	(when mail-personal-alias-file
	  (if (file-exists-p mail-personal-alias-file)
	      (build-mail-aliases)))))
532
  ;; Don't leave this around from a previous message.
533
  (kill-local-variable 'buffer-file-coding-system)
534 535 536
  ;; This doesn't work for enable-multibyte-characters.
  ;; (kill-local-variable 'enable-multibyte-characters)
  (set-buffer-multibyte default-enable-multibyte-characters)
537 538
  (if current-input-method
      (inactivate-input-method))
Richard M. Stallman's avatar
Richard M. Stallman committed
539
  (setq mail-send-actions actions)
540
  (setq mail-reply-action replybuffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
541
  (goto-char (point-min))
542 543
  (if mail-setup-with-from
      (mail-insert-from-field))
Richard M. Stallman's avatar
Richard M. Stallman committed
544 545 546
  (insert "To: ")
  (save-excursion
    (if to
547 548 549 550 551
	;; 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)))
552
	  (insert to "\n")
553 554 555 556
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline)))
Richard M. Stallman's avatar
Richard M. Stallman committed
557 558
      (newline))
    (if cc
559 560
	(let ((fill-prefix "\t")
	      (address-start (progn (insert "CC: ") (point))))
561
	  (insert cc "\n")
562 563 564 565
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline))))
Richard M. Stallman's avatar
Richard M. Stallman committed
566
    (if in-reply-to
567
	(let ((fill-prefix "\t")
568
	      (fill-column 78)
569
	      (address-start (point)))
570
	  (insert "In-reply-to: " in-reply-to "\n")
571 572 573 574
	  (fill-region-as-paragraph address-start (point-max))
	  (goto-char (point-max))
	  (unless (bolp)
	    (newline))))
575
    (insert "Subject: " (or subject "") "\n")
Richard M. Stallman's avatar
Richard M. Stallman committed
576 577 578
    (if mail-default-headers
	(insert mail-default-headers))
    (if mail-default-reply-to
579
	(insert "Reply-to: " mail-default-reply-to "\n"))
Richard M. Stallman's avatar
Richard M. Stallman committed
580
    (if mail-self-blind
581
	(insert "BCC: " user-mail-address "\n"))
Richard M. Stallman's avatar
Richard M. Stallman committed
582
    (if mail-archive-file-name
583
	(insert "FCC: " mail-archive-file-name "\n"))
584 585
    (put-text-property (point)
		       (progn
586
			 (insert mail-header-separator "\n")
587 588
			 (1- (point)))
		       'category 'mail-header-separator)
589 590
    ;; Insert the signature.  But remember the beginning of the message.
    (if to (setq to (point)))
Jim Blandy's avatar
Jim Blandy committed
591
    (cond ((eq mail-signature t)
592
	   (if (file-exists-p mail-signature-file)
593
	       (progn
594
		 (insert "\n\n-- \n")
595
		 (insert-file-contents mail-signature-file))))
596 597 598 599
	  ((stringp mail-signature)
	   (insert mail-signature))
	  (t
	   (eval mail-signature)))
Richard M. Stallman's avatar
Richard M. Stallman committed
600 601
    (goto-char (point-max))
    (or (bolp) (newline)))
602
  (if to (goto-char to))
Richard M. Stallman's avatar
Richard M. Stallman committed
603 604 605
  (or to subject in-reply-to
      (set-buffer-modified-p nil))
  (run-hooks 'mail-setup-hook))
606

607 608 609 610 611 612
(defcustom mail-mode-hook nil
  "Hook run by Mail mode."
  :group 'sendmail
  :type 'hook
  :options '(footnote-mode))

613
(defvar mail-mode-abbrev-table text-mode-abbrev-table)
Richard M. Stallman's avatar
Richard M. Stallman committed
614
;;;###autoload
615
(define-derived-mode mail-mode text-mode "Mail"
Richard M. Stallman's avatar
Richard M. Stallman committed
616 617
  "Major mode for editing mail to be sent.
Like Text Mode but with these additional commands:
Reiner Steib's avatar
Reiner Steib committed
618 619 620 621

\\[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
622
Here are commands that move to a header field (and create it if there isn't):
623 624
	 \\[mail-to]  move to To:	\\[mail-subject]  move to Subj:
	 \\[mail-bcc]  move to BCC:	\\[mail-cc]  move to CC:
625
	 \\[mail-fcc]  move to FCC:	\\[mail-reply-to] move to Reply-To:
626 627
         \\[mail-mail-reply-to]  move to Mail-Reply-To:
         \\[mail-mail-followup-to] move to Mail-Followup-To:
628
\\[mail-text]  move to message text.
Karl Heuer's avatar
Karl Heuer committed
629 630 631
\\[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).
632
\\[mail-sent-via]  mail-sent-via (add a sent-via field for each To or CC).
Eli Zaretskii's avatar
Eli Zaretskii committed
633 634
Turning on Mail mode runs the normal hooks `text-mode-hook' and
`mail-mode-hook' (in that order)."
635
  (make-local-variable 'mail-reply-action)
Richard M. Stallman's avatar
Richard M. Stallman committed
636 637
  (make-local-variable 'mail-send-actions)
  (setq buffer-offer-save t)
638
  (make-local-variable 'font-lock-defaults)
639
  (setq font-lock-defaults '(mail-font-lock-keywords t t))
Richard M. Stallman's avatar
Richard M. Stallman committed
640
  (make-local-variable 'paragraph-separate)
641 642
  (make-local-variable 'normal-auto-fill-function)
  (setq normal-auto-fill-function 'mail-mode-auto-fill)
643
  (make-local-variable 'fill-paragraph-function)
644
  (setq fill-paragraph-function 'mail-mode-fill-paragraph)
645 646 647
  ;; 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)
Stefan Monnier's avatar
Stefan Monnier committed
648 649 650
  (if mail-yank-prefix
      (set (make-local-variable 'comment-start-skip)
	   (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
651 652
  (make-local-variable 'adaptive-fill-regexp)
  (setq adaptive-fill-regexp
653
	(concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
654
		adaptive-fill-regexp))
655
  (make-local-variable 'adaptive-fill-first-line-regexp)
656
  (setq adaptive-fill-first-line-regexp
657
	(concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
658
		adaptive-fill-first-line-regexp))
659 660 661 662
  ;; `-- ' 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.
663
  (setq paragraph-separate (concat (regexp-quote mail-header-separator)
664
				"$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
665
				"\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
666 667
				"--\\( \\|-+\\)$\\|"
				page-delimiter)))
668

669 670 671

(defun mail-header-end ()
  "Return the buffer location of the end of headers, as a number."
672 673 674 675 676
  (save-restriction
    (widen)
    (save-excursion
      (rfc822-goto-eoh)
      (point))))
677 678 679

(defun mail-text-start ()
  "Return the buffer location of the start of text, as a number."
680 681 682 683 684 685
  (save-restriction
    (widen)
    (save-excursion
      (rfc822-goto-eoh)
      (forward-line 1)
      (point))))
686 687 688 689 690 691 692 693 694 695 696 697 698 699

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

700 701 702
(defun mail-mode-auto-fill ()
  "Carry out Auto Fill for Mail mode.
If within the headers, this makes the new lines into continuation lines."
703
  (if (< (point) (mail-header-end))
704 705 706 707 708
      (let ((old-line-start (save-excursion (beginning-of-line) (point))))
	(if (do-auto-fill)
	    (save-excursion
	      (beginning-of-line)
	      (while (not (eq (point) old-line-start))
709 710 711
		;; Use insert-before-markers in case we're inserting
		;; before the saved value of point (which is common).
		(insert-before-markers "   ")
712 713 714
		(forward-line -1))
	      t)))
    (do-auto-fill)))
715 716 717

(defun mail-mode-fill-paragraph (arg)
  ;; Do something special only if within the headers.
718
  (if (< (point) (mail-header-end))
719
      (let (beg end fieldname)
720 721
	(when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
		(setq beg (point)))
722
	(setq fieldname
723
		(downcase (buffer-substring beg (1- (match-end 0))))))
724 725 726 727 728 729 730 731 732 733 734
	(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"
735
		      "mail-reply-to" "mail-followup-to"
736 737 738 739 740
		      "resent-to" "resent-cc" "resent-bcc"
		      "resent-from" "resent-reply-to"))
	    (while (search-forward "," end t)
	      (or (looking-at "[ \t]")
		  (insert " "))))
741
	(fill-region-as-paragraph beg end arg)
742 743 744 745 746 747 748 749
	;; 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)))
750

751 752
;; User-level commands for sending.

753
(defun mail-send-and-exit (&optional arg)
Christopher Zaborsky's avatar
Christopher Zaborsky committed
754
  "Send message like `mail-send', then, if no errors, exit from mail buffer.
Richard M. Stallman's avatar
Richard M. Stallman committed
755 756 757
Prefix arg means don't delete this window."
  (interactive "P")
  (mail-send)
758 759
  (mail-bury arg))

760
(defun mail-dont-send (&optional arg)
761 762 763 764 765
  "Don't send the message you have been editing.
Prefix arg means don't delete this window."
  (interactive "P")
  (mail-bury arg))

766
(defun mail-bury (&optional arg)
767
  "Bury this mail buffer."
768 769
  (let ((newbuf (other-buffer (current-buffer))))
    (bury-buffer (current-buffer))
770 771 772 773 774 775 776
    (if (and (or nil
		 ;; In this case, we need to go to a different frame.
		 (window-dedicated-p (frame-selected-window))
		 ;; In this mode of operation, the frame was probably
		 ;; made for this buffer, so the user probably wants
		 ;; to delete it now.
		 (and pop-up-frames (one-window-p))
777
		 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
778
	     (not (null (delq (selected-frame) (visible-frame-list)))))
779 780 781 782 783 784
	(progn
	  (if (display-multi-frame-p)
	      (delete-frame (selected-frame))
	    ;; The previous frame is where normally they have the
	    ;; RMAIL buffer displayed.
	    (other-frame -1)))
785 786 787
      (let (rmail-flag summary-buffer)
	(and (not arg)
	     (not (one-window-p))
788 789
	     (with-current-buffer
                 (window-buffer (next-window (selected-window) 'not))
790 791
	       (setq rmail-flag (eq major-mode 'rmail-mode))
	       (setq summary-buffer
792 793
		     (and mail-bury-selects-summary
			  (boundp 'rmail-summary-buffer)
794
			  rmail-summary-buffer
795 796 797 798 799 800 801 802
			  (buffer-name rmail-summary-buffer)
			  (not (get-buffer-window rmail-summary-buffer))
			  rmail-summary-buffer))))
	(if rmail-flag
	    ;; If the Rmail buffer has a summary, show that.
	    (if summary-buffer (switch-to-buffer summary-buffer)
	      (delete-window))
	  (switch-to-buffer newbuf))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
803

804 805 806 807 808 809
(defcustom mail-send-hook nil
  "Hook run just before sending mail with `mail-send'."
  :type 'hook
  :options '(flyspell-mode-off)
  :group 'sendmail)

810 811 812 813 814 815 816 817 818 819
;;;###autoload
(defcustom mail-mailing-lists nil "\
*List of mailing list addresses the user is subscribed to.

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)


Richard M. Stallman's avatar
Richard M. Stallman committed
820 821 822 823 824 825 826
(defun mail-send ()
  "Send the message in the current buffer.
If `mail-interactive' is non-nil, wait for success indication
or error messages, and inform user.
Otherwise any failure is reported in a message back to
the user from the mailer."
  (interactive)
827 828 829 830
  (if (if buffer-file-name
	  (y-or-n-p "Send buffer contents as mail message? ")
	(or (buffer-modified-p)
	    (y-or-n-p "Message already sent; resend? ")))
831
      (let ((inhibit-read-only t)
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	    (opoint (point))
	    (ml (when mail-mailing-lists
                ;; The surrounding regexp assumes the use of
                ;; `mail-strip-quoted-names' on addresses before matching
                ;; Cannot deal with full RFC 822 freedom, but that is
                ;; unlikely to be problematic.
                (concat "\\(?:[[:space:];,]\\|\\`\\)"
                        (regexp-opt mail-mailing-lists t)
                        "\\(?:[[:space:];,]\\|\\'\\)"))))
	;; If there are mailing lists defined
	(when ml
	  (save-excursion
	    (let* ((to (mail-fetch-field "to" nil t))
		   (cc (mail-fetch-field "cc" nil t))
		   (new-header-values	; To: and Cc:
		    (mail-strip-quoted-names
		     (concat to (when cc (concat ", " cc))))))
	      ;; If message goes to known mailing list ...
	      (when (string-match ml new-header-values)
		;; Add Mail-Followup-To if none yet
		(unless (mail-fetch-field "mail-followup-to")
		  (goto-char (mail-header-end))
		  (insert "Mail-Followup-To: "
			  (let ((l))
			    (mapc
			     ;; remove duplicates
			     '(lambda (e)
				(unless (member e l)
				  (push e l)))
861 862
			     (split-string new-header-values
					   ",[[:space:]]+" t))
863
			    (mapconcat 'identity l ", "))
864
			  "\n"))
865 866 867 868 869 870
		;; Add Mail-Reply-To if none yet
		(unless (mail-fetch-field "mail-reply-to")
		  (goto-char (mail-header-end))
		  (insert "Mail-Reply-To: "
			  (or (mail-fetch-field "reply-to")
			      user-mail-address)
871
			  "\n"))))))
872
	(unless (memq mail-send-nonascii '(t mime))
873 874 875 876 877 878 879
	  (goto-char (point-min))
	  (skip-chars-forward "\0-\177")
	  (or (= (point) (point-max))
	      (if (eq mail-send-nonascii 'query)
		  (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
		      (error "Aborted"))
		(error "Message contains non-ASCII characters"))))
880 881
	;; Complain about any invalid line.
	(goto-char (point-min))
882 883 884 885 886 887 888 889
	(re-search-forward (regexp-quote mail-header-separator) (point-max) t)
	(let ((header-end (or (match-beginning 0) (point-max))))
	  (goto-char (point-min))
	  (while (< (point) header-end)
	    (unless (looking-at "[ \t]\\|.*:\\|$")
	      (push-mark opoint)
	      (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
	    (forward-line 1)))
890
	(goto-char opoint)
Richard M. Stallman's avatar
Richard M. Stallman committed
891
	(run-hooks 'mail-send-hook)
892
	(message "Sending...")
Richard M. Stallman's avatar
Richard M. Stallman committed
893 894 895 896
	(funcall send-mail-function)
	;; Now perform actions on successful sending.
	(while mail-send-actions
	  (condition-case nil
897 898
	      (apply (car (car mail-send-actions))
		     (cdr (car mail-send-actions)))
Richard M. Stallman's avatar
Richard M. Stallman committed
899 900
	    (error))
	  (setq mail-send-actions (cdr mail-send-actions)))
901
	(message "Sending...done")
Karl Heuer's avatar
Karl Heuer committed