Re: Help sending UTF-8 in mail subject
"Sergey Poznyakoff" <[email protected]> Sun, 05 Apr 2026 19:23:24 +0200
| Newsgroups | gmane.comp.gnu.mailutils.bugs |
|---|---|
| Organization | GNU.org.ua |
| Message-ID | <[email protected]> |
--1804289383-1775409804=:10061 Content-Type: text/plain Content-ID: <[email protected]> Hi, > UTF-8 in the e-mail body does not appear to cause problems, but as > soon as a UTF-8 character appears in the subject, I get the following error: Thanks for reporting. I have pushed the attached patch to fix that. It will take care to encode 8-bit header values, if LC_ALL is properly set. Best regards, Sergey --1804289383-1775409804=:10061 Content-Type: text/x-diff; name="0001-mail-encode-8-bit-headers-when-sending.patch" Content-ID: <[email protected]> Content-Description: Content-Transfer-Encoding: quoted-printable From 3d92f39a7de831348b550cd42fc6cd745cdae58f Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff <[email protected]> Date: Sun, 5 Apr 2026 20:15:21 +0300 Subject: [PATCH] mail: encode 8-bit headers when sending * libmailutils/string/strcount.c (mu_mem_8bit_count): Fix. * mail/send.c (add_header): Change arguments. Copy name and value. If the latter contains 8-bit characters and default charset can be determined, encode the value as per RFC 2047. All uses changed. --- libmailutils/string/strcount.c | 2 +- mail/send.c | 54 ++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/libmailutils/string/strcount.c b/libmailutils/string/strcount.c index 6aade5800..eacf972df 100644 --- a/libmailutils/string/strcount.c +++ b/libmailutils/string/strcount.c @@ -82,7 +82,7 @@ mu_mem_8bit_count (char const *str, size_t len) { size_t n =3D 0; while (len--) - if (*str & 0xf0) + if (*str++ & 0x80) n++; return n; } diff --git a/mail/send.c b/mail/send.c index 775903cbb..b01de87f5 100644 --- a/mail/send.c +++ b/mail/send.c @@ -59,26 +59,54 @@ list_headers (void *item, void *data) return 0; } -static void -add_header (char *name, char *value, int mode) +static int +add_header (char const *name, size_t namelen, char const *value, int mode) { struct add_header *hp; - + size_t vlen, n8; + char *vp =3D NULL; + if (!add_header_list) { int rc =3D mu_list_create (&add_header_list); if (rc) { mu_error (_("Cannot create header list: %s"), mu_strerror (rc)); - exit (1); + return -1; } } hp =3D mu_alloc (sizeof (*hp)); hp->mode =3D mode; - hp->name =3D name; - hp->value =3D value; + hp->name =3D namelen =3D=3D 0 ? strdup (name) : strndup (name, namelen); + if (!hp->name) + mu_alloc_die (); + + vlen =3D strlen (value); + if ((n8 =3D mu_mem_8bit_count (value, vlen)) > 0) + { + char *charset =3D util_get_charset (); + if (charset) + { + char *output; + char const *encoding =3D (n8 > vlen / 2) ? "base64" : "quoted-printable"; + int rc =3D mu_rfc2047_encode (charset, encoding, value, &output); + if (rc =3D=3D 0) + vp =3D output; + else + { + mu_error (_("can't encode %s header: %s"), hp->name, + mu_strerror (rc)); + free (hp->name); + free (hp); + return -1; + } + } + } + + hp->value =3D vp ? vp : mu_strdup (value); mu_list_append (add_header_list, hp); + return 0; } void @@ -86,7 +114,6 @@ send_append_header (char const *text) { char *p; size_t len; - char *name; p =3D strchr (text, ':'); if (!p) @@ -95,19 +122,16 @@ send_append_header (char const *text) return; } len =3D p - text; - name =3D mu_alloc (len + 1); - memcpy (name, text, len); - name[len] =3D 0; for (p++; *p && mu_isspace (*p); p++) ; - add_header (name, mu_strdup (p), COMPOSE_APPEND); + add_header (text, len, p, COMPOSE_APPEND); } void send_append_header2 (char const *name, char const *value, int mode) { - add_header (mu_strdup (name), mu_strdup (value), mode); + add_header (name, 0, value, mode); } int @@ -125,9 +149,9 @@ mail_sendheader (int argc, char **argv) else { size_t len =3D strlen (argv[1]); - if (len > 0 && argv[1][len - 1] =3D=3D ':') - argv[1][len - 1] =3D 0; - add_header (mu_strdup (argv[1]), mu_strdup (argv[2]), COMPOSE_APPEND); + if (len > 0 && argv[1][len - 1] =3D=3D ':') + --len; + add_header (argv[1], 0, argv[2], COMPOSE_APPEND); } return 0; } -- 2.35.1 --1804289383-1775409804=:10061--