net.smtp: handle UTF-8 subjects according to RFC 1342 (#14410)

WoodyAtHome 2022-05-16 10:09:36 +02:00 committed by Jef Roosens
parent ff02e5667b
commit 9e179ab11f
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 8 additions and 1 deletions

View File

@ -228,13 +228,20 @@ fn (mut c Client) send_data() ? {
fn (mut c Client) send_body(cfg Mail) ? {
is_html := cfg.body_type == .html
date := cfg.date.custom_format('ddd, D MMM YYYY HH:mm ZZ')
nonascii_subject := cfg.subject.bytes().any(it < u8(` `) || it > u8(`~`))
mut sb := strings.new_builder(200)
sb.write_string('From: $cfg.from\r\n')
sb.write_string('To: <$cfg.to>\r\n')
sb.write_string('Cc: <$cfg.cc>\r\n')
sb.write_string('Bcc: <$cfg.bcc>\r\n')
sb.write_string('Date: $date\r\n')
sb.write_string('Subject: $cfg.subject\r\n')
if nonascii_subject {
// handle UTF-8 subjects according RFC 1342
sb.write_string('Subject: =?utf-8?B?' + base64.encode_str(cfg.subject) + '?=\r\n')
} else {
sb.write_string('Subject: $cfg.subject\r\n')
}
if is_html {
sb.write_string('Content-Type: text/html; charset=UTF-8')
} else {