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

Fixes #73 : allow Message to not has "To" header #160

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 '';
}

$to = $headers->get('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);
}
}