1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Communicator (https://github.com/waltertamboer/communicator) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/waltertamboer/communicator for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2017 Communicator (https://github.com/waltertamboer/communicator) |
7
|
|
|
* @license https://github.com/waltertamboer/communicator/blob/master/LICENSE.md MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Communicator\Transport\Email\Adapter; |
11
|
|
|
|
12
|
|
|
use Communicator\Message; |
13
|
|
|
use Communicator\Recipient\RecipientInterface; |
14
|
|
|
use Zend\Mail\Message as ZendMessage; |
15
|
|
|
use Zend\Mail\Transport\TransportInterface; |
16
|
|
|
use Zend\Mime\Message as MimeMessage; |
17
|
|
|
use Zend\Mime\Part; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* An e-mail transport that makes use of Zend\Mail. |
21
|
|
|
*/ |
22
|
|
|
final class ZendMail extends AbstractAdapter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The mailer used to send messages. |
26
|
|
|
* |
27
|
|
|
* @var TransportInterface |
28
|
|
|
*/ |
29
|
|
|
private $transport; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes a new instance of this class. |
33
|
|
|
* |
34
|
|
|
* @param TransportInterface $transport |
35
|
|
|
*/ |
36
|
|
|
public function __construct(TransportInterface $transport) |
37
|
|
|
{ |
38
|
|
|
$this->transport = $transport; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sends a message to the given recipient. |
43
|
|
|
* |
44
|
|
|
* @param RecipientInterface $recipient The recipient that should receive the message. |
45
|
|
|
* @param Message $message The message that should be sent. |
46
|
|
|
* @param string $subject The subject of the message. |
47
|
|
|
* @param string $text The plain text message. |
48
|
|
|
* @param null|string $html An optional HTML version of the message. |
49
|
|
|
*/ |
50
|
|
|
protected function sendToRecipient( |
51
|
|
|
RecipientInterface $recipient, |
52
|
|
|
Message $message, |
53
|
|
|
string $subject, |
54
|
|
|
string $text, |
55
|
|
|
?string $html |
56
|
|
|
): void { |
57
|
|
|
$addresses = $this->getAddresses($recipient, $message); |
58
|
|
|
|
59
|
|
|
foreach ($addresses as $address) { |
60
|
|
|
/** @var ZendMessage $emailMessage */ |
61
|
|
|
$emailMessage = new ZendMessage(); |
62
|
|
|
$emailMessage->setSubject($subject); |
63
|
|
|
$emailMessage->setTo($address); |
64
|
|
|
$emailMessage->setBody($text); |
65
|
|
|
$emailMessage->setEncoding('UTF-8'); |
66
|
|
|
|
67
|
|
|
if ($this->getFromAddress() !== null) { |
68
|
|
|
$emailMessage->setFrom($this->getFromAddress(), $this->getFromName()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$textPart = new Part($text); |
72
|
|
|
$textPart->type = 'text/plain'; |
73
|
|
|
|
74
|
|
|
$bodyPart = new MimeMessage(); |
75
|
|
|
$bodyPart->addPart($textPart); |
76
|
|
|
|
77
|
|
|
if ($html) { |
|
|
|
|
78
|
|
|
$htmlPart = new Part($html); |
79
|
|
|
$htmlPart->type = 'text/html'; |
80
|
|
|
|
81
|
|
|
$bodyPart->addPart($htmlPart); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->transport->send($emailMessage); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: