Subject: bug#77866: 30.0.50; message.el handling of non-ascii names in addresses

From: Robert Pluim <[email-masked]>
Date: Thu, 17 Apr 2025 15:50:10 +0200

>>>>> On Thu, 17 Apr 2025 13:54:21 +0100, Al Haji-Ali <[email-masked]> said:

    Abdo> This is a bit difficult for me to reproduce as a user in a
    Abdo> clean instance of Emacs, since I am seeing it in notmuch,
    Abdo> which uses message.el, and I don't know how to use
    Abdo> message.el without my setup of notmuch.  However, I am sure
    Abdo> that I've identified the reason for the bug(?) in message.el
    Abdo> and I hope my description below is sufficient.

    Abdo> I have an email with a header field like this:
    Abdo> CC: "Blanco, Raúl" <[email-masked]>

    Abdo> with a non-ASCII character in the name. When I call
    Abdo> `message-send-mail`, on 29.1 I get an error `split-string:
    Abdo> Wrong type argument: stringp, nil`

    Abdo> and on 30.0.50, I get the error
    Abdo> `Email address can't be parsed`

    Abdo> The reason is that `message-send-mail` calls
    Abdo> `mail-encode-encoded-word-buffer`, which encodes that part
    Abdo> of the message as:

    Abdo> CC: "Blanco, =?utf-8?Q?Ra=C3=BAl=22?= <[email-masked]>

    Abdo> So that the quotation marks are no longer paired, and
    Abdo> `mail-header-parse-addresses` splits the address into two
    Abdo> ill-formed ones.

Thanks for the test code, it made finding the issue easy.

The root cause of this is that `mail-encode-encoded-word-buffer' is
using the `mime' method to encode the value, instead of the
`address-mime' method. Thatʼs because youʼve written the "Cc" header
name as "CC", and the lookup for the method based on the header name
is being done case-sensitively. Patch below fixes it for me (it also
helps if you put a newline at the end of the string youʼre testing)

diff --git a/lisp/mail/rfc2047.el b/lisp/mail/rfc2047.el
index 66760a6595b..db6c0423a54 100644
--- a/lisp/mail/rfc2047.el
+++ b/lisp/mail/rfc2047.el
@@ -265,7 +265,8 @@ rfc2047-encode-message-header
 		 (if (= (length charsets) 1)
 		     (cons (mm-charset-to-coding-system (car charsets))
 			   mm-coding-system-priorities)
-		   mm-coding-system-priorities)))
+		   mm-coding-system-priorities))
+                (case-fold-search t))
 	    (while (setq elem (pop alist))
 	      (when (or (and (stringp (car elem))
 			     (looking-at (car elem)))

Robert
--