]> code.delx.au - gnu-emacs-elpa/blob - smtpmail-async.el
Handle errors file by file instead of returning on first error.
[gnu-emacs-elpa] / smtpmail-async.el
1 ;;; smtpmail-async.el --- Send e-mail with smtpmail.el asynchronously -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2012-2016 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Created: 18 Jun 2012
7
8 ;; Keywords: email async
9 ;; X-URL: https://github.com/jwiegley/emacs-async
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 \f
26 ;;; Commentary:
27
28 ;; Send e-mail with smtpmail.el asynchronously. To use:
29 ;;
30 ;; (require 'smtpmail-async)
31 ;;
32 ;; (setq send-mail-function 'async-smtpmail-send-it
33 ;; message-send-mail-function 'async-smtpmail-send-it)
34 ;;
35 ;; This assumes you already have smtpmail.el working.
36
37 ;;; Code:
38
39 (defgroup smtpmail-async nil
40 "Send e-mail with smtpmail.el asynchronously"
41 :group 'smptmail)
42
43 (require 'async)
44 (require 'smtpmail)
45 (require 'message)
46
47 (defvar async-smtpmail-before-send-hook nil
48 "Hook running in the child emacs in `async-smtpmail-send-it'.
49 It is called just before calling `smtpmail-send-it'.")
50
51 (defun async-smtpmail-send-it ()
52 (let ((to (message-field-value "To"))
53 (buf-content (buffer-substring-no-properties
54 (point-min) (point-max))))
55 (message "Delivering message to %s..." to)
56 (async-start
57 `(lambda ()
58 (require 'smtpmail)
59 (with-temp-buffer
60 (insert ,buf-content)
61 (set-buffer-multibyte nil)
62 ;; Pass in the variable environment for smtpmail
63 ,(async-inject-variables
64 "\\`\\(smtpmail\\|async-smtpmail\\|\\(user-\\)?mail\\)-\\|auth-sources\\|epg"
65 nil "\\`\\(mail-header-format-function\\|smtpmail-address-buffer\\|mail-mode-abbrev-table\\)")
66 (run-hooks 'async-smtpmail-before-send-hook)
67 (smtpmail-send-it)))
68 (lambda (&optional _ignore)
69 (message "Delivering message to %s...done" to)))))
70
71 (provide 'smtpmail-async)
72
73 ;;; smtpmail-async.el ends here