| @@ -23,9 +23,9 @@ | ||
| 23 | 23 | { |
| 24 | 24 | private $headers; |
| 25 | 25 | private $body; |
| 26 | 26 | |
| 27 | - public function __construct(Headers $headers = null, AbstractPart $body = null) | |
| 27 | + public function __construct(?Headers $headers = null, ?AbstractPart $body = null) | |
| 28 | 28 | { |
| 29 | 29 | $this->headers = $headers ? clone $headers : new Headers(); |
| 30 | 30 | $this->body = $body; |
| 31 | 31 | } |
| @@ -41,9 +41,9 @@ | ||
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * @return $this |
| 44 | 44 | */ |
| 45 | - public function setBody(AbstractPart $body = null) | |
| 45 | + public function setBody(?AbstractPart $body = null) | |
| 46 | 46 | { |
| 47 | 47 | $this->body = $body; |
| 48 | 48 | |
| 49 | 49 | return $this; |
| @@ -123,13 +123,20 @@ | ||
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | public function ensureValidity() |
| 126 | 126 | { |
| 127 | - if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) { | |
| 127 | + $to = (null !== $header = $this->headers->get('To')) ? $header->getBody() : null; | |
| 128 | + $cc = (null !== $header = $this->headers->get('Cc')) ? $header->getBody() : null; | |
| 129 | + $bcc = (null !== $header = $this->headers->get('Bcc')) ? $header->getBody() : null; | |
| 130 | + | |
| 131 | + if (!$to && !$cc && !$bcc) { | |
| 128 | 132 | throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.'); |
| 129 | 133 | } |
| 130 | 134 | |
| 131 | - if (!$this->headers->has('From') && !$this->headers->has('Sender')) { | |
| 135 | + $from = (null !== $header = $this->headers->get('From')) ? $header->getBody() : null; | |
| 136 | + $sender = (null !== $header = $this->headers->get('Sender')) ? $header->getBody() : null; | |
| 137 | + | |
| 138 | + if (!$from && !$sender) { | |
| 132 | 139 | throw new LogicException('An email must have a "From" or a "Sender" header.'); |
| 133 | 140 | } |
| 134 | 141 | |
| 135 | 142 | parent::ensureValidity(); |
| @@ -139,9 +146,12 @@ | ||
| 139 | 146 | { |
| 140 | 147 | if ($this->headers->has('Sender')) { |
| 141 | 148 | $sender = $this->headers->get('Sender')->getAddress(); |
| 142 | 149 | } elseif ($this->headers->has('From')) { |
| 143 | - $sender = $this->headers->get('From')->getAddresses()[0]; | |
| 150 | + if (!$froms = $this->headers->get('From')->getAddresses()) { | |
| 151 | + throw new LogicException('A "From" header must have at least one email address.'); | |
| 152 | + } | |
| 153 | + $sender = $froms[0]; | |
| 144 | 154 | } else { |
| 145 | 155 | throw new LogicException('An email must have a "From" or a "Sender" header.'); |
| 146 | 156 | } |
| 147 | 157 | |