Skip to content

Commit 7a583ff

Browse files
committed
Fix From/Sender handling in Emails
1 parent 6dde9dc commit 7a583ff

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

Message.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ public function getPreparedHeaders(): Headers
7474
$headers = clone $this->headers;
7575

7676
if (!$headers->has('From')) {
77-
throw new LogicException('An email must have a "From" header.');
77+
if (!$headers->has('Sender')) {
78+
throw new LogicException('An email must have a "From" or a "Sender" header.');
79+
}
80+
$headers->addMailboxListHeader('From', [$headers->get('Sender')->getAddress()]);
7881
}
7982

8083
$headers->addTextHeader('MIME-Version', '1.0');
@@ -119,8 +122,12 @@ public function toIterable(): iterable
119122

120123
public function ensureValidity()
121124
{
122-
if (!$this->headers->has('From')) {
123-
throw new LogicException('An email must have a "From" header.');
125+
if (!$this->headers->has('To')) {
126+
throw new LogicException('An email must have a "To" header.');
127+
}
128+
129+
if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
130+
throw new LogicException('An email must have a "From" or a "Sender" header.');
124131
}
125132

126133
parent::ensureValidity();
@@ -133,7 +140,7 @@ public function generateMessageId(): string
133140
} elseif ($this->headers->has('From')) {
134141
$sender = $this->headers->get('From')->getAddresses()[0];
135142
} else {
136-
throw new LogicException('An email must have a "From" or a "Sender" header to compute a Messsage ID.');
143+
throw new LogicException('An email must have a "From" or a "Sender" header.');
137144
}
138145

139146
return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');

Tests/Crypto/SMimeSignerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function testSignedMessageWithBcc()
9999
{
100100
$message = (new Email())
101101
->date(new \DateTime('2019-04-07 10:36:30', new \DateTimeZone('Europe/Paris')))
102+
102103
103104
->subject('I am your sign of fear')
104105
@@ -115,8 +116,9 @@ public function testSignedMessageWithAttachments()
115116
$message = new Email((new Headers())
116117
->addDateHeader('Date', new \DateTime('2019-04-07 10:36:30', new \DateTimeZone('Europe/Paris')))
117118
->addMailboxListHeader('From', ['[email protected]'])
119+
->addMailboxListHeader('To', ['[email protected]'])
118120
);
119-
$message->html($content = 'html content <img src="cid:test.gif">');
121+
$message->html('html content <img src="cid:test.gif">');
120122
$message->text('text content');
121123
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r'));
122124
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif');

Tests/EmailTest.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,70 +251,70 @@ public function testGenerateBody()
251251
$att = new DataPart($file = fopen(__DIR__.'/Fixtures/mimetypes/test', 'r'));
252252
$img = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
253253

254-
$e = (new Email())->from('[email protected]');
254+
$e = (new Email())->from('[email protected]')->to('[email protected]');
255255
$e->text('text content');
256256
$this->assertEquals($text, $e->getBody());
257257
$this->assertEquals('text content', $e->getTextBody());
258258

259-
$e = (new Email())->from('[email protected]');
259+
$e = (new Email())->from('[email protected]')->to('[email protected]');
260260
$e->html('html content');
261261
$this->assertEquals($html, $e->getBody());
262262
$this->assertEquals('html content', $e->getHtmlBody());
263263

264-
$e = (new Email())->from('[email protected]');
264+
$e = (new Email())->from('[email protected]')->to('[email protected]');
265265
$e->html('html content');
266266
$e->text('text content');
267267
$this->assertEquals(new AlternativePart($text, $html), $e->getBody());
268268

269-
$e = (new Email())->from('[email protected]');
269+
$e = (new Email())->from('[email protected]')->to('[email protected]');
270270
$e->html('html content', 'iso-8859-1');
271271
$e->text('text content', 'iso-8859-1');
272272
$this->assertEquals('iso-8859-1', $e->getTextCharset());
273273
$this->assertEquals('iso-8859-1', $e->getHtmlCharset());
274274
$this->assertEquals(new AlternativePart(new TextPart('text content', 'iso-8859-1'), new TextPart('html content', 'iso-8859-1', 'html')), $e->getBody());
275275

276-
$e = (new Email())->from('[email protected]');
276+
$e = (new Email())->from('[email protected]')->to('[email protected]');
277277
$e->attach($file);
278278
$e->text('text content');
279279
$this->assertEquals(new MixedPart($text, $att), $e->getBody());
280280

281-
$e = (new Email())->from('[email protected]');
281+
$e = (new Email())->from('[email protected]')->to('[email protected]');
282282
$e->attach($file);
283283
$e->html('html content');
284284
$this->assertEquals(new MixedPart($html, $att), $e->getBody());
285285

286-
$e = (new Email())->from('[email protected]');
286+
$e = (new Email())->from('[email protected]')->to('[email protected]');
287287
$e->attach($file);
288288
$this->assertEquals(new MixedPart($att), $e->getBody());
289289

290-
$e = (new Email())->from('[email protected]');
290+
$e = (new Email())->from('[email protected]')->to('[email protected]');
291291
$e->html('html content');
292292
$e->text('text content');
293293
$e->attach($file);
294294
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att), $e->getBody());
295295

296-
$e = (new Email())->from('[email protected]');
296+
$e = (new Email())->from('[email protected]')->to('[email protected]');
297297
$e->html('html content');
298298
$e->text('text content');
299299
$e->attach($file);
300300
$e->attach($image, 'test.gif');
301301
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att, $img), $e->getBody());
302302

303-
$e = (new Email())->from('[email protected]');
303+
$e = (new Email())->from('[email protected]')->to('[email protected]');
304304
$e->text('text content');
305305
$e->attach($file);
306306
$e->attach($image, 'test.gif');
307307
$this->assertEquals(new MixedPart($text, $att, $img), $e->getBody());
308308

309-
$e = (new Email())->from('[email protected]');
309+
$e = (new Email())->from('[email protected]')->to('[email protected]');
310310
$e->html($content = 'html content <img src="test.gif">');
311311
$e->text('text content');
312312
$e->attach($file);
313313
$e->attach($image, 'test.gif');
314314
$fullhtml = new TextPart($content, 'utf-8', 'html');
315315
$this->assertEquals(new MixedPart(new AlternativePart($text, $fullhtml), $att, $img), $e->getBody());
316316

317-
$e = (new Email())->from('[email protected]');
317+
$e = (new Email())->from('[email protected]')->to('[email protected]');
318318
$e->html($content = 'html content <img src="cid:test.gif">');
319319
$e->text('text content');
320320
$e->attach($file);
@@ -334,7 +334,7 @@ public function testGenerateBody()
334334
fwrite($r, $content);
335335
rewind($r);
336336

337-
$e = (new Email())->from('[email protected]');
337+
$e = (new Email())->from('[email protected]')->to('[email protected]');
338338
$e->html($r);
339339
// embedding the same image twice results in one image only in the email
340340
$e->embed($image, 'test.gif');
@@ -373,6 +373,7 @@ public function testSerialize()
373373

374374
$e = new Email();
375375
$e->from('[email protected]');
376+
$e->to('[email protected]');
376377
$e->text($r);
377378
$e->html($r);
378379
$name = __DIR__.'/Fixtures/mimetypes/test';

Tests/MessageConverterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MessageConverterTest extends TestCase
2121
public function testToEmail()
2222
{
2323
$file = file_get_contents(__DIR__.'/Fixtures/mimetypes/test.gif');
24-
$email = (new Email())->from('[email protected]');
24+
$email = (new Email())->from('[email protected]')->to('[email protected]');
2525
$this->assertSame($email, MessageConverter::toEmail($email));
2626

2727
$this->assertConversion((clone $email)->text('text content'));

Tests/Part/MessagePartTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MessagePartTest extends TestCase
2222
{
2323
public function testConstructor()
2424
{
25-
$p = new MessagePart((new Email())->from('[email protected]')->text('content'));
25+
$p = new MessagePart((new Email())->from('[email protected]')->to('[email protected]')->text('content'));
2626
$this->assertStringContainsString('content', $p->getBody());
2727
$this->assertStringContainsString('content', $p->bodyToString());
2828
$this->assertStringContainsString('content', implode('', iterator_to_array($p->bodyToIterable())));

0 commit comments

Comments
 (0)