From e588719eec972f8b76fe6b3c38e50e60236aa5a0 Mon Sep 17 00:00:00 2001 From: Arnt Gulbrandsen Date: Wed, 23 Oct 2024 15:36:34 +0200 Subject: [PATCH] Send unencoded UTF8 in subject/body text when SMTPUTF8 allows that. --- src/PHPMailer.php | 9 ++++++++- test/PHPMailer/PHPMailerTest.php | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/PHPMailer.php b/src/PHPMailer.php index 5417d507..2e74a70c 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -2926,7 +2926,9 @@ class PHPMailer $bodyEncoding = $this->Encoding; $bodyCharSet = $this->CharSet; //Can we do a 7-bit downgrade? - if (static::ENCODING_8BIT === $bodyEncoding && !$this->has8bitChars($this->Body)) { + if ($this->UseSMTPUTF8) { + $bodyEncoding = static::ENCODING_8BIT; + } else if (static::ENCODING_8BIT === $bodyEncoding && !$this->has8bitChars($this->Body)) { $bodyEncoding = static::ENCODING_7BIT; //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit $bodyCharSet = static::CHARSET_ASCII; @@ -3572,6 +3574,11 @@ class PHPMailer */ public function encodeHeader($str, $position = 'text') { + $position = strtolower($position); + if($this->UseSMTPUTF8 && !("comment" === $position)) { + return trim(static::normalizeBreaks($str)); + } + $matchcount = 0; switch (strtolower($position)) { case 'phrase': diff --git a/test/PHPMailer/PHPMailerTest.php b/test/PHPMailer/PHPMailerTest.php index 15693a0a..87e5888b 100644 --- a/test/PHPMailer/PHPMailerTest.php +++ b/test/PHPMailer/PHPMailerTest.php @@ -1254,6 +1254,17 @@ EOT; $this->Mail->addAddress('spın̈altap@spın̈altap.invalid', ''); $this->Mail->preSend(); self::assertStringContainsString("spın̈altap@spın̈altap.invalid", $this->Mail->createHeader()); + + //Sending unencoded UTF8 is legal when SMTPUTF8 is used, + //except that body parts have to be encoded if they + //accidentally contain any lines that match the MIME boundary + //lines. It also looks good, so let's do it. + + $this->Mail->Subject = 'Spın̈al Tap'; + self::assertStringContainsString("Spın̈al", $this->Mail->createHeader()); + $this->Mail->Body = 'Spın̈al Tap'; + $this->Mail->preSend(); + self::assertStringContainsString("Spın̈al", $this->Mail->createBody()); } /**