Merge branch '4.4' into 5.0
* 4.4: Fix From/Sender handling in Emails
This commit is contained in:
commit
3b4c41fd4b
@ -14,6 +14,7 @@ namespace Symfony\Component\Mailer\EventListener;
|
|||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Symfony\Component\Mailer\Event\MessageEvent;
|
use Symfony\Component\Mailer\Event\MessageEvent;
|
||||||
use Symfony\Component\Mime\Address;
|
use Symfony\Component\Mime\Address;
|
||||||
|
use Symfony\Component\Mime\Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manipulates the Envelope of a Message.
|
* Manipulates the Envelope of a Message.
|
||||||
@ -43,6 +44,13 @@ class EnvelopeListener implements EventSubscriberInterface
|
|||||||
{
|
{
|
||||||
if ($this->sender) {
|
if ($this->sender) {
|
||||||
$event->getEnvelope()->setSender($this->sender);
|
$event->getEnvelope()->setSender($this->sender);
|
||||||
|
|
||||||
|
$message = $event->getMessage();
|
||||||
|
if ($message instanceof Message) {
|
||||||
|
if (!$message->getHeaders()->has('Sender') && !$message->getHeaders()->has('From')) {
|
||||||
|
$message->getHeaders()->addMailboxHeader('Sender', $this->sender->getAddress());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->recipients) {
|
if ($this->recipients) {
|
||||||
|
@ -63,10 +63,6 @@ abstract class AbstractTransport implements TransportInterface
|
|||||||
$envelope = $event->getEnvelope();
|
$envelope = $event->getEnvelope();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$envelope->getRecipients()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$message = new SentMessage($message, $envelope);
|
$message = new SentMessage($message, $envelope);
|
||||||
$this->doSend($message);
|
$this->doSend($message);
|
||||||
|
|
||||||
|
@ -74,7 +74,10 @@ class Message extends RawMessage
|
|||||||
$headers = clone $this->headers;
|
$headers = clone $this->headers;
|
||||||
|
|
||||||
if (!$headers->has('From')) {
|
if (!$headers->has('From')) {
|
||||||
throw new LogicException('An email must have a "From" header.');
|
if (!$headers->has('Sender')) {
|
||||||
|
throw new LogicException('An email must have a "From" or a "Sender" header.');
|
||||||
|
}
|
||||||
|
$headers->addMailboxListHeader('From', [$headers->get('Sender')->getAddress()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$headers->addTextHeader('MIME-Version', '1.0');
|
$headers->addTextHeader('MIME-Version', '1.0');
|
||||||
@ -119,8 +122,12 @@ class Message extends RawMessage
|
|||||||
|
|
||||||
public function ensureValidity()
|
public function ensureValidity()
|
||||||
{
|
{
|
||||||
if (!$this->headers->has('From')) {
|
if (!$this->headers->has('To')) {
|
||||||
throw new LogicException('An email must have a "From" header.');
|
throw new LogicException('An email must have a "To" header.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
|
||||||
|
throw new LogicException('An email must have a "From" or a "Sender" header.');
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::ensureValidity();
|
parent::ensureValidity();
|
||||||
@ -133,7 +140,7 @@ class Message extends RawMessage
|
|||||||
} elseif ($this->headers->has('From')) {
|
} elseif ($this->headers->has('From')) {
|
||||||
$sender = $this->headers->get('From')->getAddresses()[0];
|
$sender = $this->headers->get('From')->getAddresses()[0];
|
||||||
} else {
|
} else {
|
||||||
throw new LogicException('An email must have a "From" or a "Sender" header to compute a Messsage ID.');
|
throw new LogicException('An email must have a "From" or a "Sender" header.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
|
return bin2hex(random_bytes(16)).strstr($sender->getAddress(), '@');
|
||||||
|
@ -99,6 +99,7 @@ class SMimeSignerTest extends SMimeTestCase
|
|||||||
{
|
{
|
||||||
$message = (new Email())
|
$message = (new Email())
|
||||||
->date(new \DateTime('2019-04-07 10:36:30', new \DateTimeZone('Europe/Paris')))
|
->date(new \DateTime('2019-04-07 10:36:30', new \DateTimeZone('Europe/Paris')))
|
||||||
|
->to('fabien@symfony.com')
|
||||||
->addBcc('fabien@symfony.com', 's.stok@rollerscapes.net')
|
->addBcc('fabien@symfony.com', 's.stok@rollerscapes.net')
|
||||||
->subject('I am your sign of fear')
|
->subject('I am your sign of fear')
|
||||||
->from('noreply@example.com')
|
->from('noreply@example.com')
|
||||||
@ -115,8 +116,9 @@ class SMimeSignerTest extends SMimeTestCase
|
|||||||
$message = new Email((new Headers())
|
$message = new Email((new Headers())
|
||||||
->addDateHeader('Date', new \DateTime('2019-04-07 10:36:30', new \DateTimeZone('Europe/Paris')))
|
->addDateHeader('Date', new \DateTime('2019-04-07 10:36:30', new \DateTimeZone('Europe/Paris')))
|
||||||
->addMailboxListHeader('From', ['fabien@symfony.com'])
|
->addMailboxListHeader('From', ['fabien@symfony.com'])
|
||||||
|
->addMailboxListHeader('To', ['fabien@symfony.com'])
|
||||||
);
|
);
|
||||||
$message->html($content = 'html content <img src="cid:test.gif">');
|
$message->html('html content <img src="cid:test.gif">');
|
||||||
$message->text('text content');
|
$message->text('text content');
|
||||||
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r'));
|
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r'));
|
||||||
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
|
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
|
||||||
|
@ -251,62 +251,62 @@ class EmailTest extends TestCase
|
|||||||
$att = new DataPart($file = fopen(__DIR__.'/Fixtures/mimetypes/test', 'r'));
|
$att = new DataPart($file = fopen(__DIR__.'/Fixtures/mimetypes/test', 'r'));
|
||||||
$img = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
|
$img = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$this->assertEquals($text, $e->getBody());
|
$this->assertEquals($text, $e->getBody());
|
||||||
$this->assertEquals('text content', $e->getTextBody());
|
$this->assertEquals('text content', $e->getTextBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html('html content');
|
$e->html('html content');
|
||||||
$this->assertEquals($html, $e->getBody());
|
$this->assertEquals($html, $e->getBody());
|
||||||
$this->assertEquals('html content', $e->getHtmlBody());
|
$this->assertEquals('html content', $e->getHtmlBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html('html content');
|
$e->html('html content');
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$this->assertEquals(new AlternativePart($text, $html), $e->getBody());
|
$this->assertEquals(new AlternativePart($text, $html), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html('html content', 'iso-8859-1');
|
$e->html('html content', 'iso-8859-1');
|
||||||
$e->text('text content', 'iso-8859-1');
|
$e->text('text content', 'iso-8859-1');
|
||||||
$this->assertEquals('iso-8859-1', $e->getTextCharset());
|
$this->assertEquals('iso-8859-1', $e->getTextCharset());
|
||||||
$this->assertEquals('iso-8859-1', $e->getHtmlCharset());
|
$this->assertEquals('iso-8859-1', $e->getHtmlCharset());
|
||||||
$this->assertEquals(new AlternativePart(new TextPart('text content', 'iso-8859-1'), new TextPart('html content', 'iso-8859-1', 'html')), $e->getBody());
|
$this->assertEquals(new AlternativePart(new TextPart('text content', 'iso-8859-1'), new TextPart('html content', 'iso-8859-1', 'html')), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$this->assertEquals(new MixedPart($text, $att), $e->getBody());
|
$this->assertEquals(new MixedPart($text, $att), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
$e->html('html content');
|
$e->html('html content');
|
||||||
$this->assertEquals(new MixedPart($html, $att), $e->getBody());
|
$this->assertEquals(new MixedPart($html, $att), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
$this->assertEquals(new MixedPart($att), $e->getBody());
|
$this->assertEquals(new MixedPart($att), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html('html content');
|
$e->html('html content');
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att), $e->getBody());
|
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html('html content');
|
$e->html('html content');
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
$e->attach($image, 'test.gif');
|
$e->attach($image, 'test.gif');
|
||||||
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att, $img), $e->getBody());
|
$this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att, $img), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
$e->attach($image, 'test.gif');
|
$e->attach($image, 'test.gif');
|
||||||
$this->assertEquals(new MixedPart($text, $att, $img), $e->getBody());
|
$this->assertEquals(new MixedPart($text, $att, $img), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html($content = 'html content <img src="test.gif">');
|
$e->html($content = 'html content <img src="test.gif">');
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
@ -314,7 +314,7 @@ class EmailTest extends TestCase
|
|||||||
$fullhtml = new TextPart($content, 'utf-8', 'html');
|
$fullhtml = new TextPart($content, 'utf-8', 'html');
|
||||||
$this->assertEquals(new MixedPart(new AlternativePart($text, $fullhtml), $att, $img), $e->getBody());
|
$this->assertEquals(new MixedPart(new AlternativePart($text, $fullhtml), $att, $img), $e->getBody());
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html($content = 'html content <img src="cid:test.gif">');
|
$e->html($content = 'html content <img src="cid:test.gif">');
|
||||||
$e->text('text content');
|
$e->text('text content');
|
||||||
$e->attach($file);
|
$e->attach($file);
|
||||||
@ -334,7 +334,7 @@ class EmailTest extends TestCase
|
|||||||
fwrite($r, $content);
|
fwrite($r, $content);
|
||||||
rewind($r);
|
rewind($r);
|
||||||
|
|
||||||
$e = (new Email())->from('me@example.com');
|
$e = (new Email())->from('me@example.com')->to('you@example.com');
|
||||||
$e->html($r);
|
$e->html($r);
|
||||||
// embedding the same image twice results in one image only in the email
|
// embedding the same image twice results in one image only in the email
|
||||||
$e->embed($image, 'test.gif');
|
$e->embed($image, 'test.gif');
|
||||||
@ -373,6 +373,7 @@ class EmailTest extends TestCase
|
|||||||
|
|
||||||
$e = new Email();
|
$e = new Email();
|
||||||
$e->from('fabien@symfony.com');
|
$e->from('fabien@symfony.com');
|
||||||
|
$e->to('you@example.com');
|
||||||
$e->text($r);
|
$e->text($r);
|
||||||
$e->html($r);
|
$e->html($r);
|
||||||
$name = __DIR__.'/Fixtures/mimetypes/test';
|
$name = __DIR__.'/Fixtures/mimetypes/test';
|
||||||
|
@ -21,7 +21,7 @@ class MessageConverterTest extends TestCase
|
|||||||
public function testToEmail()
|
public function testToEmail()
|
||||||
{
|
{
|
||||||
$file = file_get_contents(__DIR__.'/Fixtures/mimetypes/test.gif');
|
$file = file_get_contents(__DIR__.'/Fixtures/mimetypes/test.gif');
|
||||||
$email = (new Email())->from('fabien@symfony.com');
|
$email = (new Email())->from('fabien@symfony.com')->to('you@example.com');
|
||||||
$this->assertSame($email, MessageConverter::toEmail($email));
|
$this->assertSame($email, MessageConverter::toEmail($email));
|
||||||
|
|
||||||
$this->assertConversion((clone $email)->text('text content'));
|
$this->assertConversion((clone $email)->text('text content'));
|
||||||
|
@ -22,7 +22,7 @@ class MessagePartTest extends TestCase
|
|||||||
{
|
{
|
||||||
public function testConstructor()
|
public function testConstructor()
|
||||||
{
|
{
|
||||||
$p = new MessagePart((new Email())->from('fabien@symfony.com')->text('content'));
|
$p = new MessagePart((new Email())->from('fabien@symfony.com')->to('you@example.com')->text('content'));
|
||||||
$this->assertStringContainsString('content', $p->getBody());
|
$this->assertStringContainsString('content', $p->getBody());
|
||||||
$this->assertStringContainsString('content', $p->bodyToString());
|
$this->assertStringContainsString('content', $p->bodyToString());
|
||||||
$this->assertStringContainsString('content', implode('', iterator_to_array($p->bodyToIterable())));
|
$this->assertStringContainsString('content', implode('', iterator_to_array($p->bodyToIterable())));
|
||||||
|
Reference in New Issue
Block a user