Skip to content

Commit

Permalink
[Mime] add DraftEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored and fabpot committed Dec 22, 2021
1 parent 4b598f4 commit 5d6b4a0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
45 changes: 45 additions & 0 deletions src/Symfony/Component/Mime/DraftEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mime;

use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Part\AbstractPart;

/**
* @author Kevin Bond <[email protected]>
*/
class DraftEmail extends Email
{
public function __construct(Headers $headers = null, AbstractPart $body = null)
{
parent::__construct($headers, $body);

$this->getHeaders()->addTextHeader('X-Unsent', '1');
}

/**
* Override default behavior as draft emails do not require From/Sender/Date/Message-ID headers.
* These are added by the client that actually sends the email.
*/
public function getPreparedHeaders(): Headers
{
$headers = clone $this->getHeaders();

if (!$headers->has('MIME-Version')) {
$headers->addTextHeader('MIME-Version', '1.0');
}

$headers->remove('Bcc');

return $headers;
}
}
15 changes: 12 additions & 3 deletions src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,22 @@ public function getBody(): AbstractPart

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.');
$this->ensureBodyValid();

if ('1' === $this->getHeaders()->getHeaderBody('X-Unsent')) {
throw new LogicException('Cannot send messages marked as "draft".');
}

parent::ensureValidity();
}

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

/**
* Generates an AbstractPart based on the raw body of a message.
*
Expand All @@ -415,7 +424,7 @@ public function ensureValidity()
*/
private function generateBody(): AbstractPart
{
$this->ensureValidity();
$this->ensureBodyValid();

[$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts();

Expand Down
58 changes: 58 additions & 0 deletions src/Symfony/Component/Mime/Tests/DraftEmailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mime\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\DraftEmail;
use Symfony\Component\Mime\Exception\LogicException;

/**
* @author Kevin Bond <[email protected]>
*/
final class DraftEmailTest extends TestCase
{
public function testCanHaveJustBody()
{
$email = (new DraftEmail())->text('some text')->toString();

$this->assertStringContainsString('some text', $email);
$this->assertStringContainsString('MIME-Version: 1.0', $email);
$this->assertStringContainsString('X-Unsent: 1', $email);
}

public function testBccIsRemoved()
{
$email = (new DraftEmail())->text('some text')->bcc('[email protected]')->toString();

$this->assertStringNotContainsString('[email protected]', $email);
}

public function testMustHaveBody()
{
$this->expectException(LogicException::class);

(new DraftEmail())->toString();
}

public function testEnsureValidityAlwaysFails()
{
$email = (new DraftEmail())
->to('[email protected]')
->from('[email protected]')
->text('some text')
;

$this->expectException(LogicException::class);

$email->ensureValidity();
}
}

0 comments on commit 5d6b4a0

Please sign in to comment.