Gnus + Gmail (Part II - Adding Multiple SMTP Accounts)
Unfortunately there is no native method to add multiple SMTP servers on Gnus. But after taking a quick look at the wiki I found this link. I changed it a little bit, so the passwords are searched at the ~/.authinfo file instead of being in your .gnus file.
Here is the code found on my .gnus file:
(defvar smtp-accounts
'((ssl "mymail@gmail.com" "smtp.gmail.com" 587 "key" nil)
(ssl "mymail@otherserver.com" "smtp.otherserver.com" 25 "key" nil)))
This lists my SMTP accounts, one line for each server, as you can see both the servers are using SSL.
(require 'smtpmail)
(setq send-mail-function 'smtpmail-send-it
message-send-mail-function 'smtpmail-send-it
mail-from-style nil
user-full-name "Rodrigo S. Wanderley"
user-mail-address "mymail@gmail.com"
message-signature-file "~/emacs/signature"
smtpmail-debug-info t
smtpmail-debug-verb t)
The code above just sets some default values. My mail signature is found on the file ~/emacs/signature, I also specify my user name user-full-name and my e-mail address user-mail-address so that Gnus can fill the From header field automatically for me.
The Debug options is also nice so that you get some feedback about what is happening while Gnus is sending the e-mail for you.
"Set related SMTP variables for supplied parameters."
(setq smtpmail-smtp-server server
smtpmail-smtp-service port
smtpmail-auth-credentials "~/.authinfo"
smtpmail-starttls-credentials nil)
(message "Setting SMTP server to `%s:%s'."
server port address))
(defun set-smtp-ssl (server port key cert)
"Set related SMTP and SSL variables for supplied parameters."
(setq starttls-use-gnutls t
starttls-gnutls-program "gnutls-cli"
starttls-extra-arguments nil
smtpmail-smtp-server server
smtpmail-smtp-service port
smtpmail-starttls-credentials (list (list server port key cert))
smtpmail-auth-credentials "~/.authinfo")
(message
"Setting SMTP server to `%s:%s'. (SSL enabled.)"
server port address))
Those are two functions used to send mail, one with and one without SSL support. Note that smtpmail-auth-credentials is telling Gnus where to find the username and password.
"Change the SMTP server according to the current from line."
(save-excursion
(loop with from = (save-restriction
(message-narrow-to-headers)
(message-fetch-field "from"))
for (acc-type address . auth-spec) in smtp-accounts
when (string-match address from)
do (cond
((eql acc-type 'plain)
(return (apply 'set-smtp-plain auth-spec)))
((eql acc-type 'ssl)
(return (apply 'set-smtp-ssl auth-spec)))
(t (error "Unrecognized SMTP account type: `%s'." acc-type)))
finally (error "Cannot interfere SMTP information."))))
(add-hook 'message-send-hook 'change-smtp)
Finally we create a hook for when me message is going to be sent. This hook basically tries to match the From header with the e-mail address you specified at smtp-account variable and chooses the server accordingly.
