@@ -506,17 +506,26 @@ impl MessageBuilder<'_> {
506506 ) ) ;
507507
508508 if let Some ( from) = self . from {
509- headers. push_str ( & format ! ( "\r \n From: {}" , sanitize_header_value( from) ) ) ;
509+ headers. push_str ( & format ! (
510+ "\r \n From: {}" ,
511+ encode_header_value( & sanitize_header_value( from) )
512+ ) ) ;
510513 }
511514
512515 if let Some ( cc) = self . cc {
513- headers. push_str ( & format ! ( "\r \n Cc: {}" , sanitize_header_value( cc) ) ) ;
516+ headers. push_str ( & format ! (
517+ "\r \n Cc: {}" ,
518+ encode_header_value( & sanitize_header_value( cc) )
519+ ) ) ;
514520 }
515521
516522 // The Gmail API reads the Bcc header to route to those recipients,
517523 // then strips it before delivery.
518524 if let Some ( bcc) = self . bcc {
519- headers. push_str ( & format ! ( "\r \n Bcc: {}" , sanitize_header_value( bcc) ) ) ;
525+ headers. push_str ( & format ! (
526+ "\r \n Bcc: {}" ,
527+ encode_header_value( & sanitize_header_value( bcc) )
528+ ) ) ;
520529 }
521530
522531 format ! ( "{}\r \n \r \n {}" , headers, body)
@@ -1316,6 +1325,27 @@ mod tests {
13161325 assert ! ( !raw. contains( "Solar — Quote Request" ) ) ;
13171326 }
13181327
1328+ #[ test]
1329+ fn test_message_builder_encodes_non_ascii_optional_headers ( ) {
1330+ let raw = MessageBuilder {
1331+ 1332+ subject : "Hello" ,
1333+ from : Some ( "\" 日本語名前\" <[email protected] >" ) , 1334+ cc : Some ( "\" 日本語名前\" <[email protected] >" ) , 1335+ bcc : Some ( "\" 日本語名前\" <[email protected] >" ) , 1336+ threading : None ,
1337+ html : false ,
1338+ }
1339+ . build ( "Body" ) ;
1340+
1341+ assert ! ( raw. contains( "From: =?UTF-8?B?" ) ) ;
1342+ assert ! ( raw. contains( "Cc: =?UTF-8?B?" ) ) ;
1343+ assert ! ( raw. contains( "Bcc: =?UTF-8?B?" ) ) ;
1344+ assert ! ( !raw
. contains
( "From: \" 日本語名前\" <[email protected] >" ) ) ; 1345+ assert ! ( !raw
. contains
( "Cc: \" 日本語名前\" <[email protected] >" ) ) ; 1346+ assert ! ( !raw
. contains
( "Bcc: \" 日本語名前\" <[email protected] >" ) ) ; 1347+ }
1348+
13191349 #[ test]
13201350 fn test_message_builder_sanitizes_crlf_injection ( ) {
13211351 let raw = MessageBuilder {
0 commit comments