Skip to content

Commit

Permalink
Merge branch '4.4'
Browse files Browse the repository at this point in the history
* 4.4:
  cs fix
  [Mailer] Check email validity before opening an SMTP connection
  • Loading branch information
nicolas-grekas committed Sep 6, 2019
2 parents 005140d + f988fe9 commit d183d1e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
14 changes: 11 additions & 3 deletions Email.php
Expand Up @@ -399,6 +399,15 @@ public function getBody(): AbstractPart
return $this->generateBody();
}

public function ensureValidity()
{
if (null === $this->text && null === $this->html && !$this->attachments) {
throw new LogicException('A message must have a text or an HTML part or attachments.');
}

parent::ensureValidity();
}

/**
* Generates an AbstractPart based on the raw body of a message.
*
Expand All @@ -421,10 +430,9 @@ public function getBody(): AbstractPart
*/
private function generateBody(): AbstractPart
{
$this->ensureValidity();

[$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts();
if (null === $this->text && null === $this->html && !$attachmentParts) {
throw new LogicException('A message must have a text or an HTML part or attachments.');
}

$part = null === $this->text ? null : new TextPart($this->text, $this->textCharset);
if (null !== $htmlPart) {
Expand Down
9 changes: 9 additions & 0 deletions Message.php
Expand Up @@ -123,6 +123,15 @@ public function toIterable(): iterable
yield from $body->toIterable();
}

public function ensureValidity()
{
if (!$this->headers->has('From')) {
throw new LogicException('An email must have a "From" header.');
}

parent::ensureValidity();
}

private function generateMessageId(string $email): string
{
return bin2hex(random_bytes(16)).strstr($email, '@');
Expand Down
9 changes: 9 additions & 0 deletions RawMessage.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Mime;

use Symfony\Component\Mime\Exception\LogicException;

/**
* @author Fabien Potencier <fabien@symfony.com>
*/
Expand Down Expand Up @@ -51,6 +53,13 @@ public function toIterable(): iterable
$this->message = $message;
}

/**
* @throws LogicException if the message is not valid
*/
public function ensureValidity()
{
}

/**
* @internal
*/
Expand Down
27 changes: 13 additions & 14 deletions Tests/EmailTest.php
Expand Up @@ -251,76 +251,75 @@ 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();
$e = (new Email())->from('me@example.com');
$e->text('text content');
$this->assertEquals($text, $e->getBody());
$this->assertEquals('text content', $e->getTextBody());

$e = new Email();
$e = (new Email())->from('me@example.com');
$e->html('html content');
$this->assertEquals($html, $e->getBody());
$this->assertEquals('html content', $e->getHtmlBody());

$e = new Email();
$e = (new Email())->from('me@example.com');
$e->html('html content');
$e->text('text content');
$this->assertEquals(new AlternativePart($text, $html), $e->getBody());

$e = new Email();
$e = (new Email())->from('me@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();
$e = (new Email())->from('me@example.com');
$e->attach($file);
$e->text('text content');
$this->assertEquals(new MixedPart($text, $att), $e->getBody());

$e = new Email();
$e = (new Email())->from('me@example.com');
$e->attach($file);
$e->html('html content');
$this->assertEquals(new MixedPart($html, $att), $e->getBody());

$e = new Email();
$e = (new Email())->from('me@example.com');
$e->attach($file);
$this->assertEquals(new MixedPart($att), $e->getBody());

$e = new Email();
$e = (new Email())->from('me@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();
$e = (new Email())->from('me@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();
$e = (new Email())->from('me@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();
$e = (new Email())->from('me@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();
$e = (new Email())->from('me@example.com');
$e->html($content = 'html content <img src="cid:test.gif">');
$e->text('text content');
$e->attach($file);
$e->attach($image, 'test.gif');
$fullhtml = new TextPart($content, 'utf-8', 'html');
$inlinedimg = (new DataPart($image, 'test.gif'))->asInline();
$body = $e->getBody();
$this->assertInstanceOf(MixedPart::class, $body);
$this->assertCount(2, $related = $body->getParts());
Expand All @@ -336,7 +335,7 @@ public function testGenerateBody()
fwrite($r, $content);
rewind($r);

$e = new Email();
$e = (new Email())->from('me@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

0 comments on commit d183d1e

Please sign in to comment.