Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailer from sender fixes #36467

Merged
merged 1 commit into from Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Message;

/**
* Manipulates the Envelope of a Message.
Expand Down Expand Up @@ -43,6 +44,13 @@ public function onMessage(MessageEvent $event): void
{
if ($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) {
Expand Down
4 changes: 0 additions & 4 deletions src/Symfony/Component/Mailer/Transport/AbstractTransport.php
Expand Up @@ -63,10 +63,6 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
$envelope = $event->getEnvelope();
}

if (!$envelope->getRecipients()) {
return null;
}

$message = new SentMessage($message, $envelope);
$this->doSend($message);

Expand Down
15 changes: 11 additions & 4 deletions src/Symfony/Component/Mime/Message.php
Expand Up @@ -74,7 +74,10 @@ public function getPreparedHeaders(): Headers
$headers = clone $this->headers;

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');
Expand Down Expand Up @@ -119,8 +122,12 @@ public function toIterable(): iterable

public function ensureValidity()
{
if (!$this->headers->has('From')) {
throw new LogicException('An email must have a "From" header.');
if (!$this->headers->has('To')) {
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();
Expand All @@ -133,7 +140,7 @@ public function generateMessageId(): string
} elseif ($this->headers->has('From')) {
$sender = $this->headers->get('From')->getAddresses()[0];
} 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(), '@');
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Mime/Tests/Crypto/SMimeSignerTest.php
Expand Up @@ -99,6 +99,7 @@ public function testSignedMessageWithBcc()
{
$message = (new Email())
->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')
->subject('I am your sign of fear')
->from('noreply@example.com')
Expand All @@ -115,8 +116,9 @@ public function testSignedMessageWithAttachments()
$message = new Email((new Headers())
->addDateHeader('Date', new \DateTime('2019-04-07 10:36:30', new \DateTimeZone('Europe/Paris')))
->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->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r'));
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
Expand Down
27 changes: 14 additions & 13 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Expand Up @@ -251,70 +251,70 @@ public function testGenerateBody()
$att = new DataPart($file = fopen(__DIR__.'/Fixtures/mimetypes/test', 'r'));
$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');
$this->assertEquals($text, $e->getBody());
$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');
$this->assertEquals($html, $e->getBody());
$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->text('text content');
$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->text('text content', 'iso-8859-1');
$this->assertEquals('iso-8859-1', $e->getTextCharset());
$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());

$e = (new Email())->from('me@example.com');
$e = (new Email())->from('me@example.com')->to('you@example.com');
$e->attach($file);
$e->text('text content');
$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->html('html content');
$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);
$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->text('text content');
$e->attach($file);
$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->text('text content');
$e->attach($file);
$e->attach($image, 'test.gif');
$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->attach($file);
$e->attach($image, 'test.gif');
$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->text('text content');
$e->attach($file);
$e->attach($image, 'test.gif');
$fullhtml = new TextPart($content, 'utf-8', 'html');
$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->text('text content');
$e->attach($file);
Expand All @@ -334,7 +334,7 @@ public function testGenerateBody()
fwrite($r, $content);
rewind($r);

$e = (new Email())->from('me@example.com');
$e = (new Email())->from('me@example.com')->to('you@example.com');
$e->html($r);
// embedding the same image twice results in one image only in the email
$e->embed($image, 'test.gif');
Expand Down Expand Up @@ -373,6 +373,7 @@ public function testSerialize()

$e = new Email();
$e->from('fabien@symfony.com');
$e->to('you@example.com');
$e->text($r);
$e->html($r);
$name = __DIR__.'/Fixtures/mimetypes/test';
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mime/Tests/MessageConverterTest.php
Expand Up @@ -21,7 +21,7 @@ class MessageConverterTest extends TestCase
public function testToEmail()
{
$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->assertConversion((clone $email)->text('text content'));
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mime/Tests/Part/MessagePartTest.php
Expand Up @@ -22,7 +22,7 @@ class MessagePartTest extends TestCase
{
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->bodyToString());
$this->assertStringContainsString('content', implode('', iterator_to_array($p->bodyToIterable())));
Expand Down