sendmail.el 76 KB

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