(require 'sendmail) (require 'mail-utils)
(defcustom mailclient-place-body-on-clipboard-flag
(fboundp 'w32-set-clipboard-data)
"If non-nil, put the e-mail body on the clipboard in mailclient.
This is useful on systems where only short mailto:// URLs are
supported. Defaults to non-nil on Windows, nil otherwise."
:type 'boolean
:group 'mail)
(defun mailclient-encode-string-as-url (string)
"Convert STRING to a URL, using utf-8 as encoding."
(apply (function concat)
(mapcar
(lambda (char)
(cond
((eq char ?\x20) "%20") ((eq char ?\n) "%0D%0A") ((string-match "[-a-zA-Z0-9_:/.@]" (char-to-string char))
(char-to-string char)) (t (format "%%%02x" char)))) (append (encode-coding-string string 'utf-8)))))
(defvar mailclient-delim-static "?")
(defun mailclient-url-delim ()
(let ((current mailclient-delim-static))
(setq mailclient-delim-static "&")
current))
(defun mailclient-gather-addresses (str &optional drop-first-name)
(let ((field (mail-fetch-field str nil t)))
(if field
(save-excursion
(let ((first t)
(result ""))
(mapc
(lambda (recp)
(setq result
(concat
result
(if (and drop-first-name
first)
""
(concat (mailclient-url-delim) str "="))
(mailclient-encode-string-as-url
recp)))
(setq first nil))
(split-string
(mail-strip-quoted-names field) "\, *"))
result)))))
(defun mailclient-send-it ()
"Pass current buffer on to the system's mail client.
Suitable value for `send-mail-function'.
The mail client is taken to be the handler of mailto URLs."
(require 'mail-utils)
(let ((case-fold-search nil)
delimline
(mailbuf (current-buffer)))
(unwind-protect
(with-temp-buffer
(insert-buffer-substring mailbuf)
(mail-sendmail-undelimit-header)
(setq delimline (point-marker))
(if mail-aliases
(expand-mail-aliases (point-min) delimline))
(goto-char (point-min))
(while (and (re-search-forward "\n\n\n*" delimline t)
(< (point) delimline))
(replace-match "\n"))
(let ((case-fold-search t))
(setq mailclient-delim-static "?")
(browse-url
(concat
(save-excursion
(narrow-to-region (point-min) delimline)
(concat
"mailto:"
(mailclient-gather-addresses "To"
'drop-first-name)
(mailclient-gather-addresses "cc" )
(mailclient-gather-addresses "bcc" )
(mailclient-gather-addresses "Resent-To" )
(mailclient-gather-addresses "Resent-cc" )
(mailclient-gather-addresses "Resent-bcc" )
(mailclient-gather-addresses "Reply-To" )
(let ((subj (mail-fetch-field "Subject" nil t)))
(widen) (if subj (concat (mailclient-url-delim) "subject="
(mailclient-encode-string-as-url subj))
""))))
(concat
(mailclient-url-delim) "body="
(mailclient-encode-string-as-url
(if mailclient-place-body-on-clipboard-flag
(progn
(clipboard-kill-ring-save
(+ 1 delimline) (point-max))
(concat
"*** E-Mail body has been placed on clipboard, "
"please paste them here! ***"))
(buffer-substring (+ 1 delimline) (point-max))))))))))))
(provide 'mailclient)