forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |