Skip to content

Commit 081050b

Browse files
committed
Add default Message CRLF
1 parent 4d64f4e commit 081050b

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Message.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ public function setMailer(Mailer $mailer) : static
125125
return $this;
126126
}
127127

128+
protected function getCrlf() : string
129+
{
130+
if (isset($this->mailer)) {
131+
return $this->mailer->getCrlf();
132+
}
133+
return "\r\n";
134+
}
135+
128136
public function setBoundary(string $boundary = null) : static
129137
{
130138
$this->boundary = $boundary ?? \bin2hex(\random_bytes(16));
@@ -178,7 +186,7 @@ public function getHeaderLines() : array
178186

179187
protected function renderHeaders() : string
180188
{
181-
return \implode($this->mailer->getCrlf(), $this->getHeaderLines());
189+
return \implode($this->getCrlf(), $this->getHeaderLines());
182190
}
183191

184192
protected function prepareHeaders() : void
@@ -196,7 +204,7 @@ protected function prepareHeaders() : void
196204
protected function renderData() : string
197205
{
198206
$boundary = $this->getBoundary();
199-
$crlf = $this->mailer->getCrlf();
207+
$crlf = $this->getCrlf();
200208
$this->prepareHeaders();
201209
$data = $this->renderHeaders() . $crlf . $crlf;
202210
$data .= '--mixed-' . $boundary . $crlf;
@@ -260,7 +268,7 @@ protected function renderMessage(
260268
string $contentType = 'text/html'
261269
) : string {
262270
$message = \base64_encode($message);
263-
$crlf = $this->mailer->getCrlf();
271+
$crlf = $this->getCrlf();
264272
$part = '--alt-' . $this->getBoundary() . $crlf;
265273
$part .= 'Content-Type: ' . $contentType . '; charset='
266274
. $this->mailer->getCharset() . $crlf;
@@ -311,7 +319,7 @@ public function getInlineAttachments() : array
311319
protected function renderAttachments() : string
312320
{
313321
$part = '';
314-
$crlf = $this->mailer->getCrlf();
322+
$crlf = $this->getCrlf();
315323
foreach ($this->getAttachments() as $attachment) {
316324
if ( ! \is_file($attachment)) {
317325
throw new LogicException('Attachment file not found: ' . $attachment);
@@ -332,7 +340,7 @@ protected function renderAttachments() : string
332340
protected function renderInlineAttachments() : string
333341
{
334342
$part = '';
335-
$crlf = $this->mailer->getCrlf();
343+
$crlf = $this->getCrlf();
336344
foreach ($this->getInlineAttachments() as $cid => $filename) {
337345
if ( ! \is_file($filename)) {
338346
throw new LogicException('Inline attachment file not found: ' . $filename);

tests/MessageTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Tests\Email;
1111

1212
use Framework\Email\Mailers\SMTPMailer;
13+
use Framework\Email\Message;
1314
use Framework\Email\XPriority;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -275,5 +276,8 @@ public function testToString() : void
275276
$this->getRenderedResult(),
276277
(string) $this->message
277278
);
279+
$message = (string) new Message();
280+
self::assertStringContainsString('MIME-Version', $message);
281+
self::assertStringContainsString('Date', $message);
278282
}
279283
}

0 commit comments

Comments
 (0)