[Mail] Refactor SMTP configuration

This commit is contained in:
dbo 2019-02-02 15:14:22 +01:00
parent c6f28090df
commit 1066e266fc
Signed by: dbo
GPG Key ID: 4F63DB96D45AA9C6
3 changed files with 53 additions and 55 deletions

52
gnus.el
View File

@ -129,58 +129,6 @@
(advice-add 'mml-attach-file :around #'db/mml-attach-file--go-to-eob)
;;; SMTP configuration
(require 'smtpmail)
(require 'starttls)
(defadvice smtpmail-send-it (around display-trace-buffer disable)
"If an error is signalled, display the process buffer."
(condition-case signals-data
ad-do-it
(error (shrink-window-if-larger-than-buffer
(display-buffer (get-buffer (format "*trace of SMTP session to %s*"
smtpmail-smtp-server))))
(signal (car signals-data) (cdr signals-data)))))
(setq send-mail-function 'smtpmail-send-it
smtpmail-stream-type 'starttls
smtpmail-smtp-service 587
starttls-use-gnutls t
starttls-extra-arguments '("--strict-tofu")
smtpmail-smtp-server (nth 3 (car db/mail-accounts))
smtpmail-smtp-user (nth 6 (car db/mail-accounts)))
(defun db/set-smtp-server-from-header (orig-fun &rest args)
"Choose smtp-settings dynamically, based on the From: header
entry of the current mail."
(require 'mail-extr)
(let* ((from (or (save-restriction
(message-narrow-to-headers)
(mail-fetch-field "From"))
user-mail-address))
(address (cadr (mail-extract-address-components from)))
(account (assoc address db/mail-accounts)))
(message "Using address: %s" address)
(if account
(progn
(message "Sending with account for %s" address)
;; XXX: these calls to `nth should be abstracted away
(let ((smtpmail-smtp-server (nth 3 account))
(smtpmail-stream-type (nth 4 account))
(smtpmail-smtp-service (nth 5 account))
(smtpmail-smtp-user (nth 6 account)))
(apply orig-fun args)))
(progn
(message "Sending with default account settings")
(apply orig-fun args)))))
(advice-add 'smtpmail-via-smtp
:around #'db/set-smtp-server-from-header)
(setq smtpmail-debug-info t)
;;;
t

32
init.el
View File

@ -1226,7 +1226,8 @@ parameters for one particular email address."
db/signencrypt-message-when-possible
db/gnus-save-newsrc-with-whitespace-1
db/gnus-summary-open-Link
db/gnus-html-mime-part-to-org))
db/gnus-html-mime-part-to-org
db/set-smtp-server-from-header))
(use-package bbdb
:commands (bbdb-search-name bbab-initialize bbdb-mua-auto-update-init bbdb-save)
@ -1528,8 +1529,7 @@ parameters for one particular email address."
gnus-group-mode-map)
(bind-key "C-<return>" #'db/gnus-summary-open-Link gnus-summary-mode-map)
(bind-key "C-<return>" #'db/gnus-summary-open-Link gnus-article-mode-map)
))
(bind-key "C-<return>" #'db/gnus-summary-open-Link gnus-article-mode-map)))
(use-package mm-decode
:init (setq mm-text-html-renderer 'shr
@ -1585,6 +1585,32 @@ parameters for one particular email address."
:init (progn
(setq notmuch-fcc-dirs nil)))
(use-package smtpmail
:defer t
:init (setq send-mail-function 'smtpmail-send-it
smtpmail-stream-type 'starttls
smtpmail-smtp-service 587
smtpmail-debug-info t)
:config (progn
;; Dynamically set smtpmail variables when sending mail
(advice-add 'smtpmail-via-smtp
:around #'db/set-smtp-server-from-header)
;; Show trace buffer when something goes wrong
(defadvice smtpmail-send-it (around display-trace-buffer disable)
"If an error is signalled, display the process buffer."
(condition-case signals-data
ad-do-it
(error (shrink-window-if-larger-than-buffer
(display-buffer (get-buffer (format "*trace of SMTP session to %s*"
smtpmail-smtp-server))))
(signal (car signals-data) (cdr signals-data)))))))
(use-package starttls
:defer t
:init (setq starttls-use-gnutls t
starttls-extra-arguments '("--strict-tofu")))
;; * Crypto

View File

@ -108,5 +108,29 @@ METHOD specifies the encrypt method used. Can be either
(with-current-buffer "*Shell Command Output*"
(kill-ring-save (point-min) (point-max)))))
(defun db/set-smtp-server-from-header (orig-fun &rest args)
"Choose smtp-settings dynamically, based on the From: header
entry of the current mail."
(require 'mail-extr)
(let* ((from (or (save-restriction
(message-narrow-to-headers)
(mail-fetch-field "From"))
user-mail-address))
(address (cadr (mail-extract-address-components from)))
(account (assoc address db/mail-accounts)))
(message "Using address: %s" address)
(if account
(progn
(message "Sending with account for %s" address)
;; XXX: these calls to `nth should be abstracted away
(let ((smtpmail-smtp-server (nth 3 account))
(smtpmail-stream-type (nth 4 account))
(smtpmail-smtp-service (nth 5 account))
(smtpmail-smtp-user (nth 6 account)))
(apply orig-fun args)))
(progn
(message "Sending with default account settings")
(apply orig-fun args)))))
(provide 'db-mail)
;;; db-mail ends here