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\Transport; |
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 AbstractTransport |
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 over the transport. |
43
|
|
|
* |
44
|
|
|
* @param array $recipients A list with all recipients that should receive the message. |
45
|
|
|
* @param Message $message The message to send. |
46
|
|
|
* @param string $subject The subject for this message. |
47
|
|
|
* @param string $text The plain text template. |
48
|
|
|
* @param null|string $html The HTML template. |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function send(array $recipients, Message $message, string $subject, string $text, ?string $html): void |
52
|
|
|
{ |
53
|
|
|
/** @var RecipientInterface $recipient */ |
54
|
|
|
foreach ($recipients as $recipient) { |
55
|
|
|
$this->sendToRecipient($recipient, $message, $subject, $text, $html); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Sends a message to the given recipient. |
61
|
|
|
* |
62
|
|
|
* @param RecipientInterface $recipient The recipient that should receive the message. |
63
|
|
|
* @param Message $message The message that should be sent. |
64
|
|
|
* @param string $subject The subject of the message. |
65
|
|
|
* @param string $text The plain text message. |
66
|
|
|
* @param null|string $html An optional HTML version of the message. |
67
|
|
|
*/ |
68
|
|
|
private function sendToRecipient( |
69
|
|
|
RecipientInterface $recipient, |
70
|
|
|
Message $message, |
71
|
|
|
string $subject, |
72
|
|
|
string $text, |
73
|
|
|
?string $html |
74
|
|
|
): void { |
75
|
|
|
$addresses = $this->getAddresses($recipient, $message); |
76
|
|
|
|
77
|
|
|
foreach ($addresses as $address) { |
78
|
|
|
/** @var ZendMessage $emailMessage */ |
79
|
|
|
$emailMessage = new ZendMessage(); |
80
|
|
|
$emailMessage->setSubject($subject); |
81
|
|
|
$emailMessage->setTo($address); |
82
|
|
|
$emailMessage->setBody($text); |
83
|
|
|
$emailMessage->setEncoding('UTF-8'); |
84
|
|
|
|
85
|
|
|
if ($this->getFromAddress() !== null) { |
86
|
|
|
$emailMessage->setFrom($this->getFromAddress(), $this->getFromName()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$textPart = new Part($text); |
90
|
|
|
$textPart->type = 'text/plain'; |
91
|
|
|
|
92
|
|
|
$bodyPart = new MimeMessage(); |
93
|
|
|
$bodyPart->addPart($textPart); |
94
|
|
|
|
95
|
|
|
if ($html) { |
|
|
|
|
96
|
|
|
$htmlPart = new Part($html); |
97
|
|
|
$htmlPart->type = 'text/html'; |
98
|
|
|
|
99
|
|
|
$bodyPart->addPart($htmlPart); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->transport->send($emailMessage); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: