Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/#160-allow-messages-without-a-to-header' into d…
Browse files Browse the repository at this point in the history
…evelop

Close #160
  • Loading branch information
Ocramius committed Mar 1, 2018
2 parents d3010b2 + f403b56 commit 8ca640a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Transport/Sendmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,15 @@ protected function prepareRecipients(Mail\Message $message)
{
$headers = $message->getHeaders();

if (! $headers->has('to')) {
throw new Exception\RuntimeException('Invalid email; contains no "To" header');
$hasTo = $headers->has('to');
if (! $hasTo && ! $headers->has('cc') && ! $headers->has('bcc')) {
throw new Exception\RuntimeException(
'Invalid email; contains no at least one of "To", "Cc", and "Bcc" header'
);
}

if (! $hasTo) {
return '';
}

/** @var Mail\Header\To $to */
Expand Down
33 changes: 33 additions & 0 deletions test/Transport/SendmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,37 @@ public function testTrimmedParameters()

$this->assertSame('-R hdrs', $r->getValue($this->transport));
}

public function testAllowMessageWithEmptyToHeaderButHasCcHeader()
{
$message = new Message();
$message->addCc('matthew@zend.com')
->setSender('ralph.schindler@zend.com', 'Ralph Schindler')
->setSubject('Testing Zend\Mail\Transport\Sendmail')
->setBody('This is only a test.');

$this->transport->send($message);
}

public function testAllowMessageWithEmptyToHeaderButHasBccHeader()
{
$message = new Message();
$message->addBcc('zf-crteam@lists.zend.com', 'CR-Team, ZF Project')
->setSender('ralph.schindler@zend.com', 'Ralph Schindler')
->setSubject('Testing Zend\Mail\Transport\Sendmail')
->setBody('This is only a test.');

$this->transport->send($message);
}

public function testDoNotAllowMessageWithoutToAndCcAndBccHeaders()
{
$message = new Message();
$message->setSender('ralph.schindler@zend.com', 'Ralph Schindler')
->setSubject('Testing Zend\Mail\Transport\Sendmail')
->setBody('This is only a test.');

$this->expectException(RuntimeException::class);
$this->transport->send($message);
}
}

0 comments on commit 8ca640a

Please sign in to comment.