webmail: When completing a recipient address, quote the "name" if necessary for proper interpretation.

Especially relevant when the name contains a comma, e.g. "lastname, firstname".
Or when it contains parentheses, e.g. "(organization)".

When sending to an address with a comma that isn't quoted, we would actually
interpret it as two addresses: One without an "@" before the comma, and the
second part after the comma with half of the name and the email addrss. This
resulted in an error message.

When sending to a recipient with unquoted parentheses in the name, those
parentheses would be interpreted as an generic email header comment, and left
out.

For issue #305 by mattfbacon.
This commit is contained in:
Mechiel Lukkien
2025-03-07 15:48:24 +01:00
parent 9a8bb1134b
commit 1c58d38280
2 changed files with 26 additions and 10 deletions

View File

@ -1417,12 +1417,28 @@ func addressString(a message.Address, smtputf8 bool) string {
host = dom.ASCII
}
}
s := "<" + a.User + "@" + host + ">"
if a.Name != "" {
// todo: properly encoded/escaped name
s = a.Name + " " + s
if a.Name == "" {
return "<" + a.User + "@" + host + ">"
}
return s
// We only quote the name if we have to. ../rfc/5322:679
const atom = "!#$%&'*+-/=?^_`{|}~"
name := a.Name
for _, c := range a.Name {
if c == '\t' || c == ' ' || c >= 0x80 || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || strings.ContainsAny(string(c), atom) {
continue
}
// We need to quote.
q := `"`
for _, c := range a.Name {
if c == '\\' || c == '"' {
q += `\`
}
q += string(c)
}
q += `"`
name = q
}
return name + " <" + a.User + "@" + host + ">"
}
// MailboxSetSpecialUse sets the special use flags of a mailbox.