GNUs email multiple outgoing accounts from the same server without imap

3.3k views Asked by At

I use GNUs and multiple email addresses, including changing the outgoing SMTP based on the FROM on the message. SO far so good. Now, though, I have multiple accounts on the same server, so the usual .authinfo doesn't work for me. It seems like the answer should be similar to http://www.cataclysmicmutation.com/2010/11/multiple-gmail-accounts-in-gnus/ but I am NOT using gmail, and I am NOT using imap. I am using SMTP and SSL. How can I extend that solution? (Also: I am using gnus-posting-styles to help send mail with the appropriate address, for what that's worth)

Below are the relevant parts of my .gnus. Note that I am trying to get the webdev@[ME].com working, which is in addition to [ME]@[ME].com (both the same server). What do I need in here, and in my authinfo, to make this work?

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;; multiple outgoing accounts ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; http://www.mostlymaths.net/2010/12/emacs-30-day-challenge-using-gnus-to.html
;; also see ~/.authinfo
(defvar smtp-accounts
  '(
    (ssl "[ME]@[ME].com" "mail.[ME].com"
     26 "[ME]@[ME].com" secret)
    ;; (ssl "webdev@[ME].com" "mail.[ME].com"
    ;;   26 "webdev@[ME].com" secret)
    (ssl "[ME]@gmail.com" "smtp.gmail.com"
     587 "[ME]@gmail.com" secret)
    (ssl "[ME]@gatech.edu" "mail.gatech.edu"
          587 "[ME]@gatech.edu" secret)
    ))

;; Now lets configure smtpmail.el with your name and functions to send
;; mail using your smtp accounts by changing the from field
(require 'smtpmail)
(setq send-mail-function 'smtpmail-send-it
      message-send-mail-function 'smtpmail-send-it
      mail-from-style nil user-full-name "[ME] S. "
      smtpmail-debug-info t smtpmail-debug-verb t)

(defun set-smtp (mech server port user password)
  "Set related SMTP variables for supplied parameters."
  (setq smtpmail-smtp-server server smtpmail-smtp-service port
    smtpmail-auth-credentials (list (list server port user
                          password)) smtpmail-auth-supported (list mech)
                          smtpmail-starttls-credentials nil)
  (message "Setting SMTP server to `%s:%s' for user `%s'."
       server port user))

(defun set-smtp-ssl (server port user password &optional 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-auth-credentials (list (list server port user
                          password)) smtpmail-starttls-credentials (list (list
                                                  server port key cert)))
  (message
   "Setting SMTP server to `%s:%s' for user `%s'. (SSL
enabled.)" server port user))

(defun change-smtp ()
  "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 (auth-mech address . auth-spec) in smtp-accounts
      when (string-match address from) do (cond
                           ((memq auth-mech '(cram-md5 plain login))
                        (return (apply 'set-smtp (cons auth-mech auth-spec))))
                           ((eql auth-mech 'ssl)
                        (return (apply 'set-smtp-ssl auth-spec)))
                           (t (error "Unrecognized SMTP auth. mechanism:
`%s'." auth-mech))) finally (error "Cannot infer SMTP information."))))

;; The previous function will complain if you fill the from field with
;; an account not present in smtp-accounts.

(defvar %smtpmail-via-smtp (symbol-function 'smtpmail-via-smtp))

(defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
  (with-current-buffer smtpmail-text-buffer
    (change-smtp))
  (funcall (symbol-value '%smtpmail-via-smtp) recipient
       smtpmail-text-buffer))

;; This wraps send mail via smtp mail, to be able to send multiple
;; messages with smtpmail.

;; Reply-to with same address it was sent to
    (setq gnus-posting-styles
      '(((header "to" "[ME]@gmail.com")
         (address "[ME]@gmail.com"))
    ((header "to" "[ME]@gatech.edu")
         (address "[ME]@gatech.edu"))
    ((header "to" "[ME]@[ME].com")
         (address "[ME]@[ME].com"))
    ;; ((header "to" "webdev@[ME].com")
        ;;  (address "webdev@[ME].com"))
    ;; ((header "cc" "webdev@[ME].com")
        ;;  (address "webdev@[ME].com"))
    ((header "cc" "[ME]@[ME].com")
         (address "[ME]@[ME].com"))
    ((header "cc" "[ME]@gatech.edu")
         (address "[ME]@gatech.edu"))
    ((header "cc" "[ME]@gmail.com")
         (address "[ME]@gmail.com"))))

SOLUTION

The answer I was looking for came below. For completeness, I'm posting what my end-result solution looked like using my /etc/host file, .authinfo, and .gnus.

;; excerpt from .gnus
(defvar smtp-accounts
  '(
    (ssl "[email protected]" "mail.onedomain.com"
     26 "[email protected]" secret)
    (ssl "[email protected]" "mail.onedomain2.com" ;; <-- This is the new alias in my /etc/hosts
         26 "[email protected]" secret) ;; <-- Added these two lines
    ))

;; excerpt from .authinfo
machine mail.onedomain.com login [email protected] port 26 password MyPASS
machine mail.onedomain2.com login [email protected] port 26 password "MyOtherPASS"

;; excerpt from /etc/hosts. The IP was obtained by pinging my mail.onedomain.com
69.89.31.60 mail.onedomain2.com onedomain2
4

There are 4 answers

2
Jeffrey DeLeo On BEST ANSWER

I have a similar set-up, where I use posting-styles in gnus to choose between several email accounts. I used to do it as mentioned, defining different aliases for the same host, having authinfo entries. It looked like this:

Contents of ~/.authinfo.gpg:
machine imap.gmail-alias1.com login [email protected] password pass port 993
machine imap.gmail-alias2.com login [email protected] password pass port 993
machine smtp.gmail1.com login [email protected] port 587 password
machine smtp.gmail2.com login [email protected] port 587 password

Partial contents of /etc/hosts:
# So I can use multiple gmail accounts in authinfo
# should be same address as imap.gmail.com
173.194.70.108 imap.gmail-alias1.com
173.194.70.108 imap.gmail-alias2.com

74.125.136.108 smtp.gmail1.com
74.125.136.108 smtp.gmail2.com

BUT I switched to using postfix to send the mail. What you can do is set postfix up so that it can (for example) receive mail for [email protected], [email protected], [email protected] and send all messages to smtp.gmail.com, while using appropriate account credentials in each case. This requires you to install the Postfix program, and set up the files */etc/postfix/sasl_passwd* and */etc/postfix/sender_relay*.

0
ksexton On

I have multiple SMTP accounts on the same mail server and simply define a hosts file entry with a different "domain" for each mail account.

1
semente On

The X-Message-SMTP-Method header field for message-mode might help as well.

Example:

X-Message-SMTP-Method: smtp smtp.fsf.org 587 other-user

More information:

  1. https://www.gnu.org/software/emacs/manual/html_node/message/Mail-Variables.html
  2. https://www.gnu.org/software/emacs/manual/html_node/gnus/Posting-Styles.html
0
Stig Brautaset On

I think a better solution is just to put all your smtp auth details in ~/.authinfo.gpg and (re)set smtpmail-smtp-user to the user account you want to send from. smtpmail.el will then use this entry's password, even if there are multiple entries with the same server. From smtpmail.el:

(defcustom smtpmail-smtp-user nil
  "User name to use when looking up credentials in the authinfo file.
If non-nil, only consider credentials for the specified user."
  :version "24.1"
  :type '(choice (const nil) string)
  :group 'smtpmail)