Changing SMTP authentication in mutt based on From e-mail address

emailmuttsmtp

I use mutt as my e-mail reader, and nbSMTP as my SMTP client. I check e-mail from a half dozen accounts and sort them into miscellaneous folders in my maildir. Some of the folders are account-specific, but some contain e-mails from multiple accounts in one folder

A number of the accounts are through Gmail, which has the unfortunate security feature of preventing users from sending from addresses that aren't theirs. Thus if I tell nbSTMP to authenticate as user1@gmail.com and send an e-mail with a From: user2@gmail.com header, Gmail will rewrite the header to From: user1@gmail.com.

I've worked around this somewhat by adding folder hooks for the folders that are account-specific; when I switch to one of those folders the hook changes my sendmail command to pass nbSMTP the appropriate command-line arguments:

folder-hook =work 'set from="work@example.com"'
folder-hook =work 'set sendmail="/usr/bin/nbsmtp -f work@example.com -U work -P password"'

However, this doesn't work for the folders that hold mail from multiple accounts, and it doesn't work if I try to e.g. send e-mail from work@example.com when I haven't switched to the work folder (since the folder hook hasn't run). Is there a way to control how nbSMTP authenticates based on the From header in the outgoing e-mail? I don't think nbSMTP has the flexibility to handle it itself, so I'm pretty sure it will have to be some kind of mutt configuration like the folder-hook method I'm currently using, but I haven't figured out how to do it. If necessary I can probably switch SMTP clients, if there's another that does provide the necessary functionality

Best Answer

You could switch to esmtp, there it is pretty trivial:

identity myself@gmail.com
        hostname smtp.googlemail.com:25
        username "myself@gmail.com"
        password "secret"
        starttls enabled
        default

identity mysecondaccount@gmail.com
        hostname smtp.googlemail.com:25
        username "mysecondaccount@gmail.com"
        password "evenmoresecret"
        starttls enabled
        default
Related Question