Mercurial > emacs
annotate lisp/mail/mailclient.el @ 112430:78c4c9559156
* lisp/emacs-lisp/copyright.el (copyright-fix-years): Use copyright-find-copyright.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sat, 22 Jan 2011 14:13:17 -0800 |
parents | ef719132ddfa |
children |
rev | line source |
---|---|
65664 | 1 ;;; mailclient.el --- mail sending via system's mail client. -*- byte-compile-dynamic: t -*- |
2 | |
112218
376148b31b5e
Add 2011 to FSF/AIST copyright years.
Glenn Morris <rgm@gnu.org>
parents:
106815
diff
changeset
|
3 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation |
65664 | 4 |
5 ;; Author: David Reitter <david.reitter@gmail.com> | |
6 ;; Keywords: mail | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
94674
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79712
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
65664 | 11 ;; it under the terms of the GNU General Public License as published by |
94674
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79712
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79712
diff
changeset
|
13 ;; (at your option) any later version. |
65664 | 14 |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
94674
ef65fa4dca3b
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79712
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
65664 | 22 |
23 ;;; Commentary: | |
24 | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
25 ;; This package allows to hand over a buffer to be sent off |
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
26 ;; via the system's designated e-mail client. |
65664 | 27 ;; Note that the e-mail client will display the contents of the buffer |
28 ;; again for editing. | |
29 ;; The e-mail client is taken to be whoever handles a mailto: URL | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
30 ;; via `browse-url'. |
65664 | 31 ;; Mailto: URLs are composed according to RFC2368. |
32 | |
33 ;; MIME bodies are not supported - we rather expect the mail client | |
34 ;; to encode the body and add, for example, a digital signature. | |
35 ;; The mailto URL RFC calls for "short text messages that are | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
36 ;; actually the content of automatic processing." |
65664 | 37 ;; So mailclient.el is ideal for situations where an e-mail is |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
38 ;; generated automatically, and the user can edit it in the |
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
39 ;; mail client (e.g. bug-reports). |
65664 | 40 |
41 ;; To activate: | |
42 ;; (setq send-mail-function 'mailclient-send-it) ; if you use `mail' | |
43 | |
44 ;;; Code: | |
45 | |
46 | |
47 (require 'sendmail) ;; for mail-sendmail-undelimit-header | |
48 (require 'mail-utils) ;; for mail-fetch-field | |
111698
b624f0531f64
(mailclient-send-it): Bind `browse-url-mailto-function' to nil to
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
106815
diff
changeset
|
49 (require 'browse-url) |
65664 | 50 |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
51 (defcustom mailclient-place-body-on-clipboard-flag |
65664 | 52 (fboundp 'w32-set-clipboard-data) |
53 "If non-nil, put the e-mail body on the clipboard in mailclient. | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
54 This is useful on systems where only short mailto:// URLs are |
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
55 supported. Defaults to non-nil on Windows, nil otherwise." |
65664 | 56 :type 'boolean |
57 :group 'mail) | |
58 | |
59 (defun mailclient-encode-string-as-url (string) | |
60 "Convert STRING to a URL, using utf-8 as encoding." | |
61 (apply (function concat) | |
62 (mapcar | |
63 (lambda (char) | |
64 (cond | |
65 ((eq char ?\x20) "%20") ;; space | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
66 ((eq char ?\n) "%0D%0A") ;; newline |
65664 | 67 ((string-match "[-a-zA-Z0-9_:/.@]" (char-to-string char)) |
68 (char-to-string char)) ;; printable | |
69 (t ;; everything else | |
70 (format "%%%02x" char)))) ;; escape | |
71 ;; Convert string to list of chars | |
72 (append (encode-coding-string string 'utf-8))))) | |
73 | |
74 (defvar mailclient-delim-static "?") | |
75 (defun mailclient-url-delim () | |
76 (let ((current mailclient-delim-static)) | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
77 (setq mailclient-delim-static "&") |
65664 | 78 current)) |
79 | |
80 (defun mailclient-gather-addresses (str &optional drop-first-name) | |
81 (let ((field (mail-fetch-field str nil t))) | |
82 (if field | |
83 (save-excursion | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
84 (let ((first t) |
65664 | 85 (result "")) |
86 (mapc | |
87 (lambda (recp) | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
88 (setq result |
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
89 (concat |
65664 | 90 result |
91 (if (and drop-first-name | |
92 first) | |
93 "" | |
94 (concat (mailclient-url-delim) str "=")) | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
95 (mailclient-encode-string-as-url |
65664 | 96 recp))) |
97 (setq first nil)) | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
98 (split-string |
65664 | 99 (mail-strip-quoted-names field) "\, *")) |
100 result))))) | |
101 | |
102257
4b59c52d2e8e
mailclient.el: declare clipboard-kill-ring-save
David Reitter <david.reitter@gmail.com>
parents:
100908
diff
changeset
|
102 (declare-function clipboard-kill-ring-save "menu-bar.el" (beg end)) |
4b59c52d2e8e
mailclient.el: declare clipboard-kill-ring-save
David Reitter <david.reitter@gmail.com>
parents:
100908
diff
changeset
|
103 |
65664 | 104 ;;;###autoload |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
105 (defun mailclient-send-it () |
65664 | 106 "Pass current buffer on to the system's mail client. |
107 Suitable value for `send-mail-function'. | |
108 The mail client is taken to be the handler of mailto URLs." | |
109 (require 'mail-utils) | |
110 (let ((case-fold-search nil) | |
111 delimline | |
112 (mailbuf (current-buffer))) | |
113 (unwind-protect | |
114 (with-temp-buffer | |
115 (insert-buffer-substring mailbuf) | |
116 ;; Move to header delimiter | |
117 (mail-sendmail-undelimit-header) | |
118 (setq delimline (point-marker)) | |
119 (if mail-aliases | |
120 (expand-mail-aliases (point-min) delimline)) | |
121 (goto-char (point-min)) | |
122 ;; ignore any blank lines in the header | |
123 (while (and (re-search-forward "\n\n\n*" delimline t) | |
124 (< (point) delimline)) | |
125 (replace-match "\n")) | |
111698
b624f0531f64
(mailclient-send-it): Bind `browse-url-mailto-function' to nil to
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
106815
diff
changeset
|
126 (let ((case-fold-search t) |
b624f0531f64
(mailclient-send-it): Bind `browse-url-mailto-function' to nil to
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
106815
diff
changeset
|
127 ;; Use the external browser function to send the |
b624f0531f64
(mailclient-send-it): Bind `browse-url-mailto-function' to nil to
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
106815
diff
changeset
|
128 ;; message. |
b624f0531f64
(mailclient-send-it): Bind `browse-url-mailto-function' to nil to
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
106815
diff
changeset
|
129 (browse-url-mailto-function nil)) |
65664 | 130 ;; initialize limiter |
131 (setq mailclient-delim-static "?") | |
132 ;; construct and call up mailto URL | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
133 (browse-url |
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
134 (concat |
65664 | 135 (save-excursion |
136 (narrow-to-region (point-min) delimline) | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
137 (concat |
65664 | 138 "mailto:" |
139 ;; some of the headers according to RFC822 | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
140 (mailclient-gather-addresses "To" |
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
141 'drop-first-name) |
65664 | 142 (mailclient-gather-addresses "cc" ) |
143 (mailclient-gather-addresses "bcc" ) | |
144 (mailclient-gather-addresses "Resent-To" ) | |
145 (mailclient-gather-addresses "Resent-cc" ) | |
146 (mailclient-gather-addresses "Resent-bcc" ) | |
147 (mailclient-gather-addresses "Reply-To" ) | |
148 ;; The From field is not honored for now: it's | |
149 ;; not necessarily configured. The mail client | |
150 ;; knows the user's address(es) | |
151 ;; (mailclient-gather-addresses "From" ) | |
152 ;; subject line | |
153 (let ((subj (mail-fetch-field "Subject" nil t))) | |
154 (widen) ;; so we can read the body later on | |
155 (if subj ;; if non-blank | |
156 ;; the mail client will deal with | |
157 ;; warning the user etc. | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
158 (concat (mailclient-url-delim) "subject=" |
65664 | 159 (mailclient-encode-string-as-url subj)) |
160 "")))) | |
161 ;; body | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
162 (concat |
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
163 (mailclient-url-delim) "body=" |
65664 | 164 (mailclient-encode-string-as-url |
165 (if mailclient-place-body-on-clipboard-flag | |
166 (progn | |
68565
877f39b663ef
(mailclient-place-body-on-clipboard-flag): Fix typo in docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
65685
diff
changeset
|
167 (clipboard-kill-ring-save |
65664 | 168 (+ 1 delimline) (point-max)) |
169 (concat | |
170 "*** E-Mail body has been placed on clipboard, " | |
103994
bc30507da7f8
(mailclient-send-it): Fix message.
Glenn Morris <rgm@gnu.org>
parents:
102257
diff
changeset
|
171 "please paste it here! ***")) |
65664 | 172 ;; else |
173 (buffer-substring (+ 1 delimline) (point-max)))))))))))) | |
174 | |
175 (provide 'mailclient) | |
176 | |
177 ;;; mailclient.el ends here |