Pages

Showing posts with label msmtp. Show all posts
Showing posts with label msmtp. Show all posts

Tuesday, September 14, 2010

Change smtp server based on from field


See MSMTP Home Page.




;; Select the correct smtp server based on the from address.
(defun msmtp-account (&optional def)
(let* ((from
(save-restriction
(message-narrow-to-headers)
(message-fetch-field "From")))
(account (if from (catch 'match
(dolist (element msmtp-name-list)
;; (message (format "smpt chosen is %s" element))
(when (string-match (format ".*%s.*" element) from)
(throw 'match element)))) nil)))
(if account account (if def def "default"))))

(defun msmtp-change-smtp ()
(setq sendmail-program "/usr/bin/msmtp")
(setq smtpmail-starttls-credentials '(("smtp.googlemail.com" 587 nil nil)))
(setq smtpmail-smtp-server "smtp.googlemail.com")
(setq message-sendmail-envelope-from 'header)
(if (message-mail-p)
(setq message-sendmail-extra-arguments (list "-a" (msmtp-account "default")))))
(add-hook 'message-send-hook 'msmtp-change-smtp)

(setq message-required-news-headers
(remove' Message-ID message-required-news-headers))