Handling Multiple SMTP Servers (August 31, 2011)

Here's how I deal with multiple SMTP servers. I have a function that runs just before sending an e-mail (after hitting C-c C-c in the message buffer). It asks where the e-mail is coming from and sets the right SMTP server.

In the example below inputting google will pick the google SMTP server. Everything else will be sent via kanis.fr.

The last setq removes the "From" header when composing an e-mail. This is required since I query this value just before sending the message. Using the default value would send the wrong header.

(defun private-gnus-prompt-address ()
  "Prompt for sender address before sending e-mail
  Optionally cover address name to avoid spam"
  (interactive)

  ;; never user agent for e-mail
  (setq message-send-mail-real-function nil)

  (setq user-mail-address (read-string "From: " "google")
  (cond
   ((string= user-mail-address "google")
    (setq
     user-mail-address "ivan@gmail.com"
     smtpmail-smtp-server "smtp.gmail.com"
     smtpmail-smtp-service 587))
   (t
    (setq
     smtpmail-smtp-server "kanis.fr"
     smtpmail-smtp-service 2525
     mail-host-address "kanis.fr")))
  (if (not (string-match "@" user-mail-address))
      (setq user-mail-address
            (concat user-mail-address "@" mail-host-address))))

(add-hook 'message-send-hook 'private-gnus-prompt-address)

(setq
  ;; Prevent "From" headers when composing
  message-required-mail-headers
  '(Subject Date (optional . In-Reply-To) Message-ID (optional . User-Agent))
  message-required-headers '((optional . References)))

back