Skip to content

Commit 1f8fcea

Browse files
committed
Format line lenth and continuous chars according to rfc 5322
1 parent 15544b4 commit 1f8fcea

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Classes/Channel/EmailChannel.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public function send(Recipient $recipient, string $subject, string $text, array
8383
$mail->setSubject(htmlspecialchars_decode($subject));
8484
$plaintext = preg_replace(array('/\s{2,}/', '/[\t]/', '/###IMAGE:(.+?)###/', '/###PLAIN:(.+?)###/'), ' ', strip_tags($text));
8585
$text = $this->embedResources($text, $mail);
86+
87+
$text = $this->formatInternetMessage($text);
88+
$plaintext = $this->formatInternetMessage($plaintext);
89+
8690
$mail->setBody($text, 'text/html', 'utf-8');
8791
$mail->addPart($plaintext, 'text/plain', 'utf-8');
8892
foreach ($attachedResources as $resource) {
@@ -158,6 +162,24 @@ private function embedPlainResourceCallback(array $matches): string
158162
return $plain;
159163
}
160164

165+
/**
166+
* Format according to https://datatracker.ietf.org/doc/html/rfc5322#section-2.1.1
167+
*/
168+
private function formatInternetMessage(string $text): string
169+
{
170+
// Break the text into lines with a maximum of 78 characters
171+
$text = wordwrap($text, 78, PHP_EOL, true);
172+
173+
// Ensure no line or continuous sequence exceeds 998 characters
174+
$lines = explode(PHP_EOL, $text);
175+
foreach ($lines as &$line) {
176+
if (strlen($line) > 998) {
177+
$line = chunk_split($line, 998, PHP_EOL);
178+
}
179+
}
180+
return implode(PHP_EOL, $lines);
181+
}
182+
161183
public function createSwiftAttachmentFromPersistentResource(PersistentResource $resource): ?Swift_Attachment
162184
{
163185
if (!is_string($resource->getSha1())) {

0 commit comments

Comments
 (0)