Subject: bug#78996: 29.4; Invalid authinfo credentials when sending mail through SMTP

From: Eli Zaretskii <[email-masked]>
Date: Sat, 19 Jul 2025 18:05:18 +0300

> From: 8dcc <[email-masked]>
> Cc: [email-masked]
> Date: Sat, 19 Jul 2025 16:25:09 +0200
> 
> > My reading of smtpmail.el is that you should set the value of
> > smtpmail-smtp-user to control the selection of credentials by login
> > name.  Did you try that?
> 
> I have just tried that, and it only seems to work if I set it
> globally. Specifically, I added the following hook:
> 
>     (add-hook
>      'message-send-hook
>      (lambda ()
>        (let ((from (mail-fetch-field "from")))
>          (when from
>            (let* ((components (mail-extract-address-components from))
>                   (name (car components))
>                   (addr (cadr components)))
>              (when name
>                (setq-local user-full-name name))
>              (when addr
>                (setq-local user-mail-address addr
>                            smtpmail-smtp-user addr)))))))
> 
> If I modify the "From" address in the `message-mode' buffer, it does in
> fact locally change `smtpmail-smtp-user' to the correct value
> ([email-masked]). I have an entry in my '~/.authinfo.gpg' with that
> address after the "login" keyword, but it still authenticates with the
> first entry.
> 
> If I manually evaluate:
> 
>     (setq smtpmail-smtp-user "[email-masked]")
> 
> It does work fine. Is this by design? Why doesn't it work if I use
> `setq-local'?

Probably because smtpmail uses a different buffer for accessing the
authinfo?  (That's a guess.)

But I don't understand why you cannot set the value globally.  It
isn't like you need to send mail using several different credentials
at the same time, is it?