From 9e179ab11f4c38413b246ff6503d5391288414ce Mon Sep 17 00:00:00 2001 From: WoodyAtHome Date: Mon, 16 May 2022 10:09:36 +0200 Subject: [PATCH] net.smtp: handle UTF-8 subjects according to RFC 1342 (#14410) --- vlib/net/smtp/smtp.v | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/vlib/net/smtp/smtp.v b/vlib/net/smtp/smtp.v index 19936854cf..d4b4c563ca 100644 --- a/vlib/net/smtp/smtp.v +++ b/vlib/net/smtp/smtp.v @@ -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 {