Subject: Re: bug#79214: 30.1; rmail with two remote mailboxes and both passwords in .authinfo.gpg

From: Roman Rusch <[email-masked]>
Date: Sat, 16 Aug 2025 16:53:55 +0200

> Date: Sat, 16 Aug 2025 14:18:23 +0300
> From: Eli Zaretskii <[email-masked]>
> Cc: [email-masked]
> 
> > Date: Sun, 10 Aug 2025 20:02:29 +0200
> > From:  Roman Rusch via "Bug reports for GNU Emacs,
> >  the Swiss army knife of text editors" <[email-masked]>
> > 
> > Dear Sir or Madam,
> > 
> > I've set up emacs rmail to withdraw the emails from two remote
> > Mailboxes (with gnu movemail) and store it in one local inbox by
> > having two entrances in rmail-inbox list like below:
> > 
> > rmail-inbox-list '("pops://XXX"
> >  		   "pops://YYY")
> > 
> > The password for both accounts are correctly stored in a .authinfo.gpg
> > file, as described in the Info-page "Auth-source" for Emacs. The two
> > passwords are different.
> > 
> > The bug is that the command to get the emails always triggers an
> > "authentication failed" for the second account - whatever the order in
> > the above list is. But the authentication always works when in
> > rmail-inbox-list only one account is defined.
> > 
> > It took me a while to figure out that rmail is NOT reading in the
> > password from the .authinfo.gpg file at all for the second account,
> > because it already has read in one before and thinks it does not have
> > to do so anymore.  As a consequence it uses the password of the first
> > account also for the second one, which causes the "authentication
> > failed" error.
> > 
> > I believe I've found the reason for the above problem in the file
> > "rmail.el" -> "defun rmail-get-remote-password". By commenting out the
> > first "when" expression as shown below I could at least make it work
> > again for my case:
> > 
> >   ;;(when (not rmail-encoded-remote-password)
> >     (if (not rmail-remote-password)
> >         (setq rmail-remote-password
> >               (let ((found (nth 0 (auth-source-search
> >                                    :max 1 :user user :host host
> >                                    :require '(:secret)))))
> >                 (if found
> >                     (auth-info-password found)
> >                   (read-passwd (if imap
> >                                    "IMAP password: "
> >                                  "POP password: "))))))
> >     (rmail-set-remote-password rmail-remote-password)
> >     (setq rmail-remote-password nil);;)
> >   (rmail-encode-string rmail-encoded-remote-password (emacs-pid))
> >   )
> 
> Thanks.  I don't use multiple POP inboxes, so I wonder whether you
> could implement a simple change along the lines below and test it in
> your environment:
> 
>  . introduce 2 new variables, rmail-remote-password-host and
>    rmail-remote-password-user
>  . in rmail-get-remote-password record the values of the USER and HOST
>    arguments in the above 2 variables
>  . replace this condition in rmail-get-remote-password:
> 
>      (when (not rmail-encoded-remote-password)
> 
>    with
> 
>      (when (or (not rmail-encoded-remote-password)
>                (not (equal user rmail-remote-password-user))
> 	       (not (equal host rmail-remote-password-host)))
> 
> This should cause Rmail to ask you for the password, but only when it
> needs to fetch email from an inbox different from the one used before.
> Which I think is what you want.
> 
Hi Eli,

Thank you for your email. I can confirm your proposal works with my
environment. Below is what I did around the defun in rmail.el, without
the docstring:

(setq rmail-remote-password-host ""
      rmail-remote-password-user "")

(defun rmail-get-remote-password (imap user host)
  (when (or (not rmail-encoded-remote-password)
               (not (equal user rmail-remote-password-user))
	       (not (equal host rmail-remote-password-host)))
    (setq rmail-remote-password-host host
          rmail-remote-password-user user)
    (if (not rmail-remote-password)
        (setq rmail-remote-password
              (let ((found (nth 0 (auth-source-search
                                   :max 1 :user user :host host
                                   :require '(:secret)))))
                (if found
                    (auth-info-password found)
                  (read-passwd (if imap
                                   "IMAP password: "
                                 "POP password: "))))))
    (rmail-set-remote-password rmail-remote-password)
    (setq rmail-remote-password nil))
  (rmail-encode-string rmail-encoded-remote-password (emacs-pid))
  )

--
Regards, Roman