@@ -15,31 +15,31 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_MimePart extends Swift_Mime_MimePart |
17 | 17 | { |
18 | - /** |
|
19 | - * Create a new MimePart. |
|
20 | - * |
|
21 | - * Details may be optionally passed into the constructor. |
|
22 | - * |
|
23 | - * @param string $body |
|
24 | - * @param string $contentType |
|
25 | - * @param string $charset |
|
26 | - */ |
|
27 | - public function __construct($body = null, $contentType = null, $charset = null) |
|
28 | - { |
|
29 | - \call_user_func_array( |
|
30 | - [$this, 'Swift_Mime_MimePart::__construct'], |
|
31 | - Swift_DependencyContainer::getInstance() |
|
32 | - ->createDependenciesFor('mime.part') |
|
33 | - ); |
|
18 | + /** |
|
19 | + * Create a new MimePart. |
|
20 | + * |
|
21 | + * Details may be optionally passed into the constructor. |
|
22 | + * |
|
23 | + * @param string $body |
|
24 | + * @param string $contentType |
|
25 | + * @param string $charset |
|
26 | + */ |
|
27 | + public function __construct($body = null, $contentType = null, $charset = null) |
|
28 | + { |
|
29 | + \call_user_func_array( |
|
30 | + [$this, 'Swift_Mime_MimePart::__construct'], |
|
31 | + Swift_DependencyContainer::getInstance() |
|
32 | + ->createDependenciesFor('mime.part') |
|
33 | + ); |
|
34 | 34 | |
35 | - if (!isset($charset)) { |
|
36 | - $charset = Swift_DependencyContainer::getInstance() |
|
37 | - ->lookup('properties.charset'); |
|
38 | - } |
|
39 | - $this->setBody($body); |
|
40 | - $this->setCharset($charset); |
|
41 | - if ($contentType) { |
|
42 | - $this->setContentType($contentType); |
|
43 | - } |
|
44 | - } |
|
35 | + if (!isset($charset)) { |
|
36 | + $charset = Swift_DependencyContainer::getInstance() |
|
37 | + ->lookup('properties.charset'); |
|
38 | + } |
|
39 | + $this->setBody($body); |
|
40 | + $this->setCharset($charset); |
|
41 | + if ($contentType) { |
|
42 | + $this->setContentType($contentType); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | } |
@@ -15,14 +15,14 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_SwiftException extends Exception |
17 | 17 | { |
18 | - /** |
|
19 | - * Create a new SwiftException with $message. |
|
20 | - * |
|
21 | - * @param string $message |
|
22 | - * @param int $code |
|
23 | - */ |
|
24 | - public function __construct($message, $code = 0, Exception $previous = null) |
|
25 | - { |
|
26 | - parent::__construct($message, $code, $previous); |
|
27 | - } |
|
18 | + /** |
|
19 | + * Create a new SwiftException with $message. |
|
20 | + * |
|
21 | + * @param string $message |
|
22 | + * @param int $code |
|
23 | + */ |
|
24 | + public function __construct($message, $code = 0, Exception $previous = null) |
|
25 | + { |
|
26 | + parent::__construct($message, $code, $previous); |
|
27 | + } |
|
28 | 28 | } |
@@ -15,40 +15,40 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_Attachment extends Swift_Mime_Attachment |
17 | 17 | { |
18 | - /** |
|
19 | - * Create a new Attachment. |
|
20 | - * |
|
21 | - * Details may be optionally provided to the constructor. |
|
22 | - * |
|
23 | - * @param string|Swift_OutputByteStream $data |
|
24 | - * @param string $filename |
|
25 | - * @param string $contentType |
|
26 | - */ |
|
27 | - public function __construct($data = null, $filename = null, $contentType = null) |
|
28 | - { |
|
29 | - \call_user_func_array( |
|
30 | - [$this, 'Swift_Mime_Attachment::__construct'], |
|
31 | - Swift_DependencyContainer::getInstance() |
|
32 | - ->createDependenciesFor('mime.attachment') |
|
33 | - ); |
|
18 | + /** |
|
19 | + * Create a new Attachment. |
|
20 | + * |
|
21 | + * Details may be optionally provided to the constructor. |
|
22 | + * |
|
23 | + * @param string|Swift_OutputByteStream $data |
|
24 | + * @param string $filename |
|
25 | + * @param string $contentType |
|
26 | + */ |
|
27 | + public function __construct($data = null, $filename = null, $contentType = null) |
|
28 | + { |
|
29 | + \call_user_func_array( |
|
30 | + [$this, 'Swift_Mime_Attachment::__construct'], |
|
31 | + Swift_DependencyContainer::getInstance() |
|
32 | + ->createDependenciesFor('mime.attachment') |
|
33 | + ); |
|
34 | 34 | |
35 | - $this->setBody($data, $contentType); |
|
36 | - $this->setFilename($filename); |
|
37 | - } |
|
35 | + $this->setBody($data, $contentType); |
|
36 | + $this->setFilename($filename); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Create a new Attachment from a filesystem path. |
|
41 | - * |
|
42 | - * @param string $path |
|
43 | - * @param string $contentType optional |
|
44 | - * |
|
45 | - * @return self |
|
46 | - */ |
|
47 | - public static function fromPath($path, $contentType = null) |
|
48 | - { |
|
49 | - return (new self())->setFile( |
|
50 | - new Swift_ByteStream_FileByteStream($path), |
|
51 | - $contentType |
|
52 | - ); |
|
53 | - } |
|
39 | + /** |
|
40 | + * Create a new Attachment from a filesystem path. |
|
41 | + * |
|
42 | + * @param string $path |
|
43 | + * @param string $contentType optional |
|
44 | + * |
|
45 | + * @return self |
|
46 | + */ |
|
47 | + public static function fromPath($path, $contentType = null) |
|
48 | + { |
|
49 | + return (new self())->setFile( |
|
50 | + new Swift_ByteStream_FileByteStream($path), |
|
51 | + $contentType |
|
52 | + ); |
|
53 | + } |
|
54 | 54 | } |
@@ -15,22 +15,22 @@ |
||
15 | 15 | */ |
16 | 16 | interface Swift_Transport_SmtpAgent |
17 | 17 | { |
18 | - /** |
|
19 | - * Get the IoBuffer where read/writes are occurring. |
|
20 | - * |
|
21 | - * @return Swift_Transport_IoBuffer |
|
22 | - */ |
|
23 | - public function getBuffer(); |
|
18 | + /** |
|
19 | + * Get the IoBuffer where read/writes are occurring. |
|
20 | + * |
|
21 | + * @return Swift_Transport_IoBuffer |
|
22 | + */ |
|
23 | + public function getBuffer(); |
|
24 | 24 | |
25 | - /** |
|
26 | - * Run a command against the buffer, expecting the given response codes. |
|
27 | - * |
|
28 | - * If no response codes are given, the response will not be validated. |
|
29 | - * If codes are given, an exception will be thrown on an invalid response. |
|
30 | - * |
|
31 | - * @param string $command |
|
32 | - * @param int[] $codes |
|
33 | - * @param string[] $failures An array of failures by-reference |
|
34 | - */ |
|
35 | - public function executeCommand($command, $codes = [], &$failures = null); |
|
25 | + /** |
|
26 | + * Run a command against the buffer, expecting the given response codes. |
|
27 | + * |
|
28 | + * If no response codes are given, the response will not be validated. |
|
29 | + * If codes are given, an exception will be thrown on an invalid response. |
|
30 | + * |
|
31 | + * @param string $command |
|
32 | + * @param int[] $codes |
|
33 | + * @param string[] $failures An array of failures by-reference |
|
34 | + */ |
|
35 | + public function executeCommand($command, $codes = [], &$failures = null); |
|
36 | 36 | } |
@@ -19,140 +19,140 @@ |
||
19 | 19 | */ |
20 | 20 | class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTransport |
21 | 21 | { |
22 | - /** |
|
23 | - * Connection buffer parameters. |
|
24 | - * |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - private $params = [ |
|
28 | - 'timeout' => 30, |
|
29 | - 'blocking' => 1, |
|
30 | - 'command' => '/usr/sbin/sendmail -bs', |
|
31 | - 'type' => Swift_Transport_IoBuffer::TYPE_PROCESS, |
|
32 | - ]; |
|
33 | - |
|
34 | - /** |
|
35 | - * Create a new SendmailTransport with $buf for I/O. |
|
36 | - * |
|
37 | - * @param string $localDomain |
|
38 | - */ |
|
39 | - public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
40 | - { |
|
41 | - parent::__construct($buf, $dispatcher, $localDomain, $addressEncoder); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Start the standalone SMTP session if running in -bs mode. |
|
46 | - */ |
|
47 | - public function start() |
|
48 | - { |
|
49 | - if (false !== strpos($this->getCommand(), ' -bs')) { |
|
50 | - parent::start(); |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Set the command to invoke. |
|
56 | - * |
|
57 | - * If using -t mode you are strongly advised to include -oi or -i in the flags. |
|
58 | - * For example: /usr/sbin/sendmail -oi -t |
|
59 | - * Swift will append a -f<sender> flag if one is not present. |
|
60 | - * |
|
61 | - * The recommended mode is "-bs" since it is interactive and failure notifications |
|
62 | - * are hence possible. |
|
63 | - * |
|
64 | - * @param string $command |
|
65 | - * |
|
66 | - * @return $this |
|
67 | - */ |
|
68 | - public function setCommand($command) |
|
69 | - { |
|
70 | - $this->params['command'] = $command; |
|
71 | - |
|
72 | - return $this; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Get the sendmail command which will be invoked. |
|
77 | - * |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - public function getCommand() |
|
81 | - { |
|
82 | - return $this->params['command']; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Send the given Message. |
|
87 | - * |
|
88 | - * Recipient/sender data will be retrieved from the Message API. |
|
89 | - * |
|
90 | - * The return value is the number of recipients who were accepted for delivery. |
|
91 | - * NOTE: If using 'sendmail -t' you will not be aware of any failures until |
|
92 | - * they bounce (i.e. send() will always return 100% success). |
|
93 | - * |
|
94 | - * @param string[] $failedRecipients An array of failures by-reference |
|
95 | - * |
|
96 | - * @return int |
|
97 | - */ |
|
98 | - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
99 | - { |
|
100 | - $failedRecipients = (array) $failedRecipients; |
|
101 | - $command = $this->getCommand(); |
|
102 | - $buffer = $this->getBuffer(); |
|
103 | - $count = 0; |
|
104 | - |
|
105 | - if (false !== strpos($command, ' -t')) { |
|
106 | - if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
107 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
108 | - if ($evt->bubbleCancelled()) { |
|
109 | - return 0; |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - if (false === strpos($command, ' -f')) { |
|
114 | - $command .= ' -f'.escapeshellarg($this->getReversePath($message) ?? ''); |
|
115 | - } |
|
116 | - |
|
117 | - $buffer->initialize(array_merge($this->params, ['command' => $command])); |
|
118 | - |
|
119 | - if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) { |
|
120 | - $buffer->setWriteTranslations(["\r\n" => "\n", "\n." => "\n.."]); |
|
121 | - } else { |
|
122 | - $buffer->setWriteTranslations(["\r\n" => "\n"]); |
|
123 | - } |
|
124 | - |
|
125 | - $count = \count((array) $message->getTo()) |
|
126 | - + \count((array) $message->getCc()) |
|
127 | - + \count((array) $message->getBcc()) |
|
128 | - ; |
|
129 | - $message->toByteStream($buffer); |
|
130 | - $buffer->flushBuffers(); |
|
131 | - $buffer->setWriteTranslations([]); |
|
132 | - $buffer->terminate(); |
|
133 | - |
|
134 | - if ($evt) { |
|
135 | - $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
136 | - $evt->setFailedRecipients($failedRecipients); |
|
137 | - $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
138 | - } |
|
139 | - |
|
140 | - $message->generateId(); |
|
141 | - } elseif (false !== strpos($command, ' -bs')) { |
|
142 | - $count = parent::send($message, $failedRecipients); |
|
143 | - } else { |
|
144 | - $this->throwException(new Swift_TransportException( |
|
145 | - 'Unsupported sendmail command flags ['.$command.']. '. |
|
146 | - 'Must be one of "-bs" or "-t" but can include additional flags.' |
|
147 | - )); |
|
148 | - } |
|
149 | - |
|
150 | - return $count; |
|
151 | - } |
|
152 | - |
|
153 | - /** Get the params to initialize the buffer */ |
|
154 | - protected function getBufferParams() |
|
155 | - { |
|
156 | - return $this->params; |
|
157 | - } |
|
22 | + /** |
|
23 | + * Connection buffer parameters. |
|
24 | + * |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + private $params = [ |
|
28 | + 'timeout' => 30, |
|
29 | + 'blocking' => 1, |
|
30 | + 'command' => '/usr/sbin/sendmail -bs', |
|
31 | + 'type' => Swift_Transport_IoBuffer::TYPE_PROCESS, |
|
32 | + ]; |
|
33 | + |
|
34 | + /** |
|
35 | + * Create a new SendmailTransport with $buf for I/O. |
|
36 | + * |
|
37 | + * @param string $localDomain |
|
38 | + */ |
|
39 | + public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
40 | + { |
|
41 | + parent::__construct($buf, $dispatcher, $localDomain, $addressEncoder); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Start the standalone SMTP session if running in -bs mode. |
|
46 | + */ |
|
47 | + public function start() |
|
48 | + { |
|
49 | + if (false !== strpos($this->getCommand(), ' -bs')) { |
|
50 | + parent::start(); |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Set the command to invoke. |
|
56 | + * |
|
57 | + * If using -t mode you are strongly advised to include -oi or -i in the flags. |
|
58 | + * For example: /usr/sbin/sendmail -oi -t |
|
59 | + * Swift will append a -f<sender> flag if one is not present. |
|
60 | + * |
|
61 | + * The recommended mode is "-bs" since it is interactive and failure notifications |
|
62 | + * are hence possible. |
|
63 | + * |
|
64 | + * @param string $command |
|
65 | + * |
|
66 | + * @return $this |
|
67 | + */ |
|
68 | + public function setCommand($command) |
|
69 | + { |
|
70 | + $this->params['command'] = $command; |
|
71 | + |
|
72 | + return $this; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Get the sendmail command which will be invoked. |
|
77 | + * |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + public function getCommand() |
|
81 | + { |
|
82 | + return $this->params['command']; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Send the given Message. |
|
87 | + * |
|
88 | + * Recipient/sender data will be retrieved from the Message API. |
|
89 | + * |
|
90 | + * The return value is the number of recipients who were accepted for delivery. |
|
91 | + * NOTE: If using 'sendmail -t' you will not be aware of any failures until |
|
92 | + * they bounce (i.e. send() will always return 100% success). |
|
93 | + * |
|
94 | + * @param string[] $failedRecipients An array of failures by-reference |
|
95 | + * |
|
96 | + * @return int |
|
97 | + */ |
|
98 | + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
99 | + { |
|
100 | + $failedRecipients = (array) $failedRecipients; |
|
101 | + $command = $this->getCommand(); |
|
102 | + $buffer = $this->getBuffer(); |
|
103 | + $count = 0; |
|
104 | + |
|
105 | + if (false !== strpos($command, ' -t')) { |
|
106 | + if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
107 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
108 | + if ($evt->bubbleCancelled()) { |
|
109 | + return 0; |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + if (false === strpos($command, ' -f')) { |
|
114 | + $command .= ' -f'.escapeshellarg($this->getReversePath($message) ?? ''); |
|
115 | + } |
|
116 | + |
|
117 | + $buffer->initialize(array_merge($this->params, ['command' => $command])); |
|
118 | + |
|
119 | + if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) { |
|
120 | + $buffer->setWriteTranslations(["\r\n" => "\n", "\n." => "\n.."]); |
|
121 | + } else { |
|
122 | + $buffer->setWriteTranslations(["\r\n" => "\n"]); |
|
123 | + } |
|
124 | + |
|
125 | + $count = \count((array) $message->getTo()) |
|
126 | + + \count((array) $message->getCc()) |
|
127 | + + \count((array) $message->getBcc()) |
|
128 | + ; |
|
129 | + $message->toByteStream($buffer); |
|
130 | + $buffer->flushBuffers(); |
|
131 | + $buffer->setWriteTranslations([]); |
|
132 | + $buffer->terminate(); |
|
133 | + |
|
134 | + if ($evt) { |
|
135 | + $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
136 | + $evt->setFailedRecipients($failedRecipients); |
|
137 | + $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
138 | + } |
|
139 | + |
|
140 | + $message->generateId(); |
|
141 | + } elseif (false !== strpos($command, ' -bs')) { |
|
142 | + $count = parent::send($message, $failedRecipients); |
|
143 | + } else { |
|
144 | + $this->throwException(new Swift_TransportException( |
|
145 | + 'Unsupported sendmail command flags ['.$command.']. '. |
|
146 | + 'Must be one of "-bs" or "-t" but can include additional flags.' |
|
147 | + )); |
|
148 | + } |
|
149 | + |
|
150 | + return $count; |
|
151 | + } |
|
152 | + |
|
153 | + /** Get the params to initialize the buffer */ |
|
154 | + protected function getBufferParams() |
|
155 | + { |
|
156 | + return $this->params; |
|
157 | + } |
|
158 | 158 | } |
@@ -15,72 +15,72 @@ |
||
15 | 15 | */ |
16 | 16 | interface Swift_Transport_EsmtpHandler |
17 | 17 | { |
18 | - /** |
|
19 | - * Get the name of the ESMTP extension this handles. |
|
20 | - * |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - public function getHandledKeyword(); |
|
18 | + /** |
|
19 | + * Get the name of the ESMTP extension this handles. |
|
20 | + * |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + public function getHandledKeyword(); |
|
24 | 24 | |
25 | - /** |
|
26 | - * Set the parameters which the EHLO greeting indicated. |
|
27 | - * |
|
28 | - * @param string[] $parameters |
|
29 | - */ |
|
30 | - public function setKeywordParams(array $parameters); |
|
25 | + /** |
|
26 | + * Set the parameters which the EHLO greeting indicated. |
|
27 | + * |
|
28 | + * @param string[] $parameters |
|
29 | + */ |
|
30 | + public function setKeywordParams(array $parameters); |
|
31 | 31 | |
32 | - /** |
|
33 | - * Runs immediately after a EHLO has been issued. |
|
34 | - * |
|
35 | - * @param Swift_Transport_SmtpAgent $agent to read/write |
|
36 | - */ |
|
37 | - public function afterEhlo(Swift_Transport_SmtpAgent $agent); |
|
32 | + /** |
|
33 | + * Runs immediately after a EHLO has been issued. |
|
34 | + * |
|
35 | + * @param Swift_Transport_SmtpAgent $agent to read/write |
|
36 | + */ |
|
37 | + public function afterEhlo(Swift_Transport_SmtpAgent $agent); |
|
38 | 38 | |
39 | - /** |
|
40 | - * Get params which are appended to MAIL FROM:<>. |
|
41 | - * |
|
42 | - * @return string[] |
|
43 | - */ |
|
44 | - public function getMailParams(); |
|
39 | + /** |
|
40 | + * Get params which are appended to MAIL FROM:<>. |
|
41 | + * |
|
42 | + * @return string[] |
|
43 | + */ |
|
44 | + public function getMailParams(); |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get params which are appended to RCPT TO:<>. |
|
48 | - * |
|
49 | - * @return string[] |
|
50 | - */ |
|
51 | - public function getRcptParams(); |
|
46 | + /** |
|
47 | + * Get params which are appended to RCPT TO:<>. |
|
48 | + * |
|
49 | + * @return string[] |
|
50 | + */ |
|
51 | + public function getRcptParams(); |
|
52 | 52 | |
53 | - /** |
|
54 | - * Runs when a command is due to be sent. |
|
55 | - * |
|
56 | - * @param Swift_Transport_SmtpAgent $agent to read/write |
|
57 | - * @param string $command to send |
|
58 | - * @param int[] $codes expected in response |
|
59 | - * @param string[] $failedRecipients to collect failures |
|
60 | - * @param bool $stop to be set true by-reference if the command is now sent |
|
61 | - */ |
|
62 | - public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false); |
|
53 | + /** |
|
54 | + * Runs when a command is due to be sent. |
|
55 | + * |
|
56 | + * @param Swift_Transport_SmtpAgent $agent to read/write |
|
57 | + * @param string $command to send |
|
58 | + * @param int[] $codes expected in response |
|
59 | + * @param string[] $failedRecipients to collect failures |
|
60 | + * @param bool $stop to be set true by-reference if the command is now sent |
|
61 | + */ |
|
62 | + public function onCommand(Swift_Transport_SmtpAgent $agent, $command, $codes = [], &$failedRecipients = null, &$stop = false); |
|
63 | 63 | |
64 | - /** |
|
65 | - * Returns +1, -1 or 0 according to the rules for usort(). |
|
66 | - * |
|
67 | - * This method is called to ensure extensions can be execute in an appropriate order. |
|
68 | - * |
|
69 | - * @param string $esmtpKeyword to compare with |
|
70 | - * |
|
71 | - * @return int |
|
72 | - */ |
|
73 | - public function getPriorityOver($esmtpKeyword); |
|
64 | + /** |
|
65 | + * Returns +1, -1 or 0 according to the rules for usort(). |
|
66 | + * |
|
67 | + * This method is called to ensure extensions can be execute in an appropriate order. |
|
68 | + * |
|
69 | + * @param string $esmtpKeyword to compare with |
|
70 | + * |
|
71 | + * @return int |
|
72 | + */ |
|
73 | + public function getPriorityOver($esmtpKeyword); |
|
74 | 74 | |
75 | - /** |
|
76 | - * Returns an array of method names which are exposed to the Esmtp class. |
|
77 | - * |
|
78 | - * @return string[] |
|
79 | - */ |
|
80 | - public function exposeMixinMethods(); |
|
75 | + /** |
|
76 | + * Returns an array of method names which are exposed to the Esmtp class. |
|
77 | + * |
|
78 | + * @return string[] |
|
79 | + */ |
|
80 | + public function exposeMixinMethods(); |
|
81 | 81 | |
82 | - /** |
|
83 | - * Tells this handler to clear any buffers and reset its state. |
|
84 | - */ |
|
85 | - public function resetState(); |
|
82 | + /** |
|
83 | + * Tells this handler to clear any buffers and reset its state. |
|
84 | + */ |
|
85 | + public function resetState(); |
|
86 | 86 | } |
@@ -15,432 +15,432 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_Transport_EsmtpTransport extends Swift_Transport_AbstractSmtpTransport implements Swift_Transport_SmtpAgent |
17 | 17 | { |
18 | - /** |
|
19 | - * ESMTP extension handlers. |
|
20 | - * |
|
21 | - * @var Swift_Transport_EsmtpHandler[] |
|
22 | - */ |
|
23 | - private $handlers = []; |
|
24 | - |
|
25 | - /** |
|
26 | - * ESMTP capabilities. |
|
27 | - * |
|
28 | - * @var string[] |
|
29 | - */ |
|
30 | - private $capabilities = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * Connection buffer parameters. |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - private $params = [ |
|
38 | - 'protocol' => 'tcp', |
|
39 | - 'host' => 'localhost', |
|
40 | - 'port' => 25, |
|
41 | - 'timeout' => 30, |
|
42 | - 'blocking' => 1, |
|
43 | - 'tls' => false, |
|
44 | - 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET, |
|
45 | - 'stream_context_options' => [], |
|
46 | - ]; |
|
47 | - |
|
48 | - /** |
|
49 | - * Creates a new EsmtpTransport using the given I/O buffer. |
|
50 | - * |
|
51 | - * @param Swift_Transport_EsmtpHandler[] $extensionHandlers |
|
52 | - * @param string $localDomain |
|
53 | - */ |
|
54 | - public function __construct(Swift_Transport_IoBuffer $buf, array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
55 | - { |
|
56 | - parent::__construct($buf, $dispatcher, $localDomain, $addressEncoder); |
|
57 | - $this->setExtensionHandlers($extensionHandlers); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Set the host to connect to. |
|
62 | - * |
|
63 | - * Literal IPv6 addresses should be wrapped in square brackets. |
|
64 | - * |
|
65 | - * @param string $host |
|
66 | - * |
|
67 | - * @return $this |
|
68 | - */ |
|
69 | - public function setHost($host) |
|
70 | - { |
|
71 | - $this->params['host'] = $host; |
|
72 | - |
|
73 | - return $this; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get the host to connect to. |
|
78 | - * |
|
79 | - * @return string |
|
80 | - */ |
|
81 | - public function getHost() |
|
82 | - { |
|
83 | - return $this->params['host']; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Set the port to connect to. |
|
88 | - * |
|
89 | - * @param int $port |
|
90 | - * |
|
91 | - * @return $this |
|
92 | - */ |
|
93 | - public function setPort($port) |
|
94 | - { |
|
95 | - $this->params['port'] = (int) $port; |
|
96 | - |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Get the port to connect to. |
|
102 | - * |
|
103 | - * @return int |
|
104 | - */ |
|
105 | - public function getPort() |
|
106 | - { |
|
107 | - return $this->params['port']; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Set the connection timeout. |
|
112 | - * |
|
113 | - * @param int $timeout seconds |
|
114 | - * |
|
115 | - * @return $this |
|
116 | - */ |
|
117 | - public function setTimeout($timeout) |
|
118 | - { |
|
119 | - $this->params['timeout'] = (int) $timeout; |
|
120 | - $this->buffer->setParam('timeout', (int) $timeout); |
|
121 | - |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Get the connection timeout. |
|
127 | - * |
|
128 | - * @return int |
|
129 | - */ |
|
130 | - public function getTimeout() |
|
131 | - { |
|
132 | - return $this->params['timeout']; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Set the encryption type (tls or ssl). |
|
137 | - * |
|
138 | - * @param string $encryption |
|
139 | - * |
|
140 | - * @return $this |
|
141 | - */ |
|
142 | - public function setEncryption($encryption) |
|
143 | - { |
|
144 | - $encryption = strtolower($encryption ?? ''); |
|
145 | - if ('tls' == $encryption) { |
|
146 | - $this->params['protocol'] = 'tcp'; |
|
147 | - $this->params['tls'] = true; |
|
148 | - } else { |
|
149 | - $this->params['protocol'] = $encryption; |
|
150 | - $this->params['tls'] = false; |
|
151 | - } |
|
152 | - |
|
153 | - return $this; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get the encryption type. |
|
158 | - * |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function getEncryption() |
|
162 | - { |
|
163 | - return $this->params['tls'] ? 'tls' : $this->params['protocol']; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Sets the stream context options. |
|
168 | - * |
|
169 | - * @param array $options |
|
170 | - * |
|
171 | - * @return $this |
|
172 | - */ |
|
173 | - public function setStreamOptions($options) |
|
174 | - { |
|
175 | - $this->params['stream_context_options'] = $options; |
|
176 | - |
|
177 | - return $this; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Returns the stream context options. |
|
182 | - * |
|
183 | - * @return array |
|
184 | - */ |
|
185 | - public function getStreamOptions() |
|
186 | - { |
|
187 | - return $this->params['stream_context_options']; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Sets the source IP. |
|
192 | - * |
|
193 | - * IPv6 addresses should be wrapped in square brackets. |
|
194 | - * |
|
195 | - * @param string $source |
|
196 | - * |
|
197 | - * @return $this |
|
198 | - */ |
|
199 | - public function setSourceIp($source) |
|
200 | - { |
|
201 | - $this->params['sourceIp'] = $source; |
|
202 | - |
|
203 | - return $this; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Returns the IP used to connect to the destination. |
|
208 | - * |
|
209 | - * @return string |
|
210 | - */ |
|
211 | - public function getSourceIp() |
|
212 | - { |
|
213 | - return $this->params['sourceIp'] ?? null; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Sets whether SMTP pipelining is enabled. |
|
218 | - * |
|
219 | - * By default, support is auto-detected using the PIPELINING SMTP extension. |
|
220 | - * Use this function to override that in the unlikely event of compatibility |
|
221 | - * issues. |
|
222 | - * |
|
223 | - * @param bool $enabled |
|
224 | - * |
|
225 | - * @return $this |
|
226 | - */ |
|
227 | - public function setPipelining($enabled) |
|
228 | - { |
|
229 | - $this->pipelining = $enabled; |
|
230 | - |
|
231 | - return $this; |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Returns whether SMTP pipelining is enabled. |
|
236 | - * |
|
237 | - * @return bool|null a boolean if pipelining is explicitly enabled or disabled, |
|
238 | - * or null if support is auto-detected |
|
239 | - */ |
|
240 | - public function getPipelining() |
|
241 | - { |
|
242 | - return $this->pipelining; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Set ESMTP extension handlers. |
|
247 | - * |
|
248 | - * @param Swift_Transport_EsmtpHandler[] $handlers |
|
249 | - * |
|
250 | - * @return $this |
|
251 | - */ |
|
252 | - public function setExtensionHandlers(array $handlers) |
|
253 | - { |
|
254 | - $assoc = []; |
|
255 | - foreach ($handlers as $handler) { |
|
256 | - $assoc[$handler->getHandledKeyword()] = $handler; |
|
257 | - } |
|
258 | - uasort($assoc, function ($a, $b) { |
|
259 | - return $a->getPriorityOver($b->getHandledKeyword()); |
|
260 | - }); |
|
261 | - $this->handlers = $assoc; |
|
262 | - $this->setHandlerParams(); |
|
263 | - |
|
264 | - return $this; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Get ESMTP extension handlers. |
|
269 | - * |
|
270 | - * @return Swift_Transport_EsmtpHandler[] |
|
271 | - */ |
|
272 | - public function getExtensionHandlers() |
|
273 | - { |
|
274 | - return array_values($this->handlers); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Run a command against the buffer, expecting the given response codes. |
|
279 | - * |
|
280 | - * If no response codes are given, the response will not be validated. |
|
281 | - * If codes are given, an exception will be thrown on an invalid response. |
|
282 | - * |
|
283 | - * @param string $command |
|
284 | - * @param int[] $codes |
|
285 | - * @param string[] $failures An array of failures by-reference |
|
286 | - * @param bool $pipeline Do not wait for response |
|
287 | - * @param string $address the address, if command is RCPT TO |
|
288 | - * |
|
289 | - * @return string|null The server response, or null if pipelining is enabled |
|
290 | - */ |
|
291 | - public function executeCommand($command, $codes = [], &$failures = null, $pipeline = false, $address = null) |
|
292 | - { |
|
293 | - $failures = (array) $failures; |
|
294 | - $stopSignal = false; |
|
295 | - $response = null; |
|
296 | - foreach ($this->getActiveHandlers() as $handler) { |
|
297 | - $response = $handler->onCommand( |
|
298 | - $this, $command, $codes, $failures, $stopSignal |
|
299 | - ); |
|
300 | - if ($stopSignal) { |
|
301 | - return $response; |
|
302 | - } |
|
303 | - } |
|
304 | - |
|
305 | - return parent::executeCommand($command, $codes, $failures, $pipeline, $address); |
|
306 | - } |
|
307 | - |
|
308 | - /** Mixin handling method for ESMTP handlers */ |
|
309 | - public function __call($method, $args) |
|
310 | - { |
|
311 | - foreach ($this->handlers as $handler) { |
|
312 | - if (\in_array(strtolower($method), |
|
313 | - array_map('strtolower', (array) $handler->exposeMixinMethods()) |
|
314 | - )) { |
|
315 | - $return = \call_user_func_array([$handler, $method], $args); |
|
316 | - // Allow fluid method calls |
|
317 | - if (null === $return && 'set' == substr($method, 0, 3)) { |
|
318 | - return $this; |
|
319 | - } else { |
|
320 | - return $return; |
|
321 | - } |
|
322 | - } |
|
323 | - } |
|
324 | - trigger_error('Call to undefined method '.$method, E_USER_ERROR); |
|
325 | - } |
|
326 | - |
|
327 | - /** Get the params to initialize the buffer */ |
|
328 | - protected function getBufferParams() |
|
329 | - { |
|
330 | - return $this->params; |
|
331 | - } |
|
332 | - |
|
333 | - /** Overridden to perform EHLO instead */ |
|
334 | - protected function doHeloCommand() |
|
335 | - { |
|
336 | - try { |
|
337 | - $response = $this->executeCommand( |
|
338 | - sprintf("EHLO %s\r\n", $this->domain), [250] |
|
339 | - ); |
|
340 | - } catch (Swift_TransportException $e) { |
|
341 | - return parent::doHeloCommand(); |
|
342 | - } |
|
343 | - |
|
344 | - if ($this->params['tls']) { |
|
345 | - try { |
|
346 | - $this->executeCommand("STARTTLS\r\n", [220]); |
|
347 | - |
|
348 | - if (!$this->buffer->startTLS()) { |
|
349 | - throw new Swift_TransportException('Unable to connect with TLS encryption'); |
|
350 | - } |
|
351 | - |
|
352 | - try { |
|
353 | - $response = $this->executeCommand( |
|
354 | - sprintf("EHLO %s\r\n", $this->domain), [250] |
|
355 | - ); |
|
356 | - } catch (Swift_TransportException $e) { |
|
357 | - return parent::doHeloCommand(); |
|
358 | - } |
|
359 | - } catch (Swift_TransportException $e) { |
|
360 | - $this->throwException($e); |
|
361 | - } |
|
362 | - } |
|
363 | - |
|
364 | - $this->capabilities = $this->getCapabilities($response); |
|
365 | - if (!isset($this->pipelining)) { |
|
366 | - $this->pipelining = isset($this->capabilities['PIPELINING']); |
|
367 | - } |
|
368 | - |
|
369 | - $this->setHandlerParams(); |
|
370 | - foreach ($this->getActiveHandlers() as $handler) { |
|
371 | - $handler->afterEhlo($this); |
|
372 | - } |
|
373 | - } |
|
374 | - |
|
375 | - /** Overridden to add Extension support */ |
|
376 | - protected function doMailFromCommand($address) |
|
377 | - { |
|
378 | - $address = $this->addressEncoder->encodeString($address); |
|
379 | - $handlers = $this->getActiveHandlers(); |
|
380 | - $params = []; |
|
381 | - foreach ($handlers as $handler) { |
|
382 | - $params = array_merge($params, (array) $handler->getMailParams()); |
|
383 | - } |
|
384 | - $paramStr = !empty($params) ? ' '.implode(' ', $params) : ''; |
|
385 | - $this->executeCommand( |
|
386 | - sprintf("MAIL FROM:<%s>%s\r\n", $address, $paramStr), [250], $failures, true |
|
387 | - ); |
|
388 | - } |
|
389 | - |
|
390 | - /** Overridden to add Extension support */ |
|
391 | - protected function doRcptToCommand($address) |
|
392 | - { |
|
393 | - $address = $this->addressEncoder->encodeString($address); |
|
394 | - $handlers = $this->getActiveHandlers(); |
|
395 | - $params = []; |
|
396 | - foreach ($handlers as $handler) { |
|
397 | - $params = array_merge($params, (array) $handler->getRcptParams()); |
|
398 | - } |
|
399 | - $paramStr = !empty($params) ? ' '.implode(' ', $params) : ''; |
|
400 | - $this->executeCommand( |
|
401 | - sprintf("RCPT TO:<%s>%s\r\n", $address, $paramStr), [250, 251, 252], $failures, true, $address |
|
402 | - ); |
|
403 | - } |
|
404 | - |
|
405 | - /** Determine ESMTP capabilities by function group */ |
|
406 | - private function getCapabilities($ehloResponse) |
|
407 | - { |
|
408 | - $capabilities = []; |
|
409 | - $ehloResponse = trim($ehloResponse ?? ''); |
|
410 | - $lines = explode("\r\n", $ehloResponse); |
|
411 | - array_shift($lines); |
|
412 | - foreach ($lines as $line) { |
|
413 | - if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di', $line, $matches)) { |
|
414 | - $keyword = strtoupper($matches[1]); |
|
415 | - $paramStr = strtoupper(ltrim($matches[2], ' =')); |
|
416 | - $params = !empty($paramStr) ? explode(' ', $paramStr) : []; |
|
417 | - $capabilities[$keyword] = $params; |
|
418 | - } |
|
419 | - } |
|
420 | - |
|
421 | - return $capabilities; |
|
422 | - } |
|
423 | - |
|
424 | - /** Set parameters which are used by each extension handler */ |
|
425 | - private function setHandlerParams() |
|
426 | - { |
|
427 | - foreach ($this->handlers as $keyword => $handler) { |
|
428 | - if (\array_key_exists($keyword, $this->capabilities)) { |
|
429 | - $handler->setKeywordParams($this->capabilities[$keyword]); |
|
430 | - } |
|
431 | - } |
|
432 | - } |
|
433 | - |
|
434 | - /** Get ESMTP handlers which are currently ok to use */ |
|
435 | - private function getActiveHandlers() |
|
436 | - { |
|
437 | - $handlers = []; |
|
438 | - foreach ($this->handlers as $keyword => $handler) { |
|
439 | - if (\array_key_exists($keyword, $this->capabilities)) { |
|
440 | - $handlers[] = $handler; |
|
441 | - } |
|
442 | - } |
|
443 | - |
|
444 | - return $handlers; |
|
445 | - } |
|
18 | + /** |
|
19 | + * ESMTP extension handlers. |
|
20 | + * |
|
21 | + * @var Swift_Transport_EsmtpHandler[] |
|
22 | + */ |
|
23 | + private $handlers = []; |
|
24 | + |
|
25 | + /** |
|
26 | + * ESMTP capabilities. |
|
27 | + * |
|
28 | + * @var string[] |
|
29 | + */ |
|
30 | + private $capabilities = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * Connection buffer parameters. |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + private $params = [ |
|
38 | + 'protocol' => 'tcp', |
|
39 | + 'host' => 'localhost', |
|
40 | + 'port' => 25, |
|
41 | + 'timeout' => 30, |
|
42 | + 'blocking' => 1, |
|
43 | + 'tls' => false, |
|
44 | + 'type' => Swift_Transport_IoBuffer::TYPE_SOCKET, |
|
45 | + 'stream_context_options' => [], |
|
46 | + ]; |
|
47 | + |
|
48 | + /** |
|
49 | + * Creates a new EsmtpTransport using the given I/O buffer. |
|
50 | + * |
|
51 | + * @param Swift_Transport_EsmtpHandler[] $extensionHandlers |
|
52 | + * @param string $localDomain |
|
53 | + */ |
|
54 | + public function __construct(Swift_Transport_IoBuffer $buf, array $extensionHandlers, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
55 | + { |
|
56 | + parent::__construct($buf, $dispatcher, $localDomain, $addressEncoder); |
|
57 | + $this->setExtensionHandlers($extensionHandlers); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Set the host to connect to. |
|
62 | + * |
|
63 | + * Literal IPv6 addresses should be wrapped in square brackets. |
|
64 | + * |
|
65 | + * @param string $host |
|
66 | + * |
|
67 | + * @return $this |
|
68 | + */ |
|
69 | + public function setHost($host) |
|
70 | + { |
|
71 | + $this->params['host'] = $host; |
|
72 | + |
|
73 | + return $this; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get the host to connect to. |
|
78 | + * |
|
79 | + * @return string |
|
80 | + */ |
|
81 | + public function getHost() |
|
82 | + { |
|
83 | + return $this->params['host']; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Set the port to connect to. |
|
88 | + * |
|
89 | + * @param int $port |
|
90 | + * |
|
91 | + * @return $this |
|
92 | + */ |
|
93 | + public function setPort($port) |
|
94 | + { |
|
95 | + $this->params['port'] = (int) $port; |
|
96 | + |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Get the port to connect to. |
|
102 | + * |
|
103 | + * @return int |
|
104 | + */ |
|
105 | + public function getPort() |
|
106 | + { |
|
107 | + return $this->params['port']; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Set the connection timeout. |
|
112 | + * |
|
113 | + * @param int $timeout seconds |
|
114 | + * |
|
115 | + * @return $this |
|
116 | + */ |
|
117 | + public function setTimeout($timeout) |
|
118 | + { |
|
119 | + $this->params['timeout'] = (int) $timeout; |
|
120 | + $this->buffer->setParam('timeout', (int) $timeout); |
|
121 | + |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Get the connection timeout. |
|
127 | + * |
|
128 | + * @return int |
|
129 | + */ |
|
130 | + public function getTimeout() |
|
131 | + { |
|
132 | + return $this->params['timeout']; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Set the encryption type (tls or ssl). |
|
137 | + * |
|
138 | + * @param string $encryption |
|
139 | + * |
|
140 | + * @return $this |
|
141 | + */ |
|
142 | + public function setEncryption($encryption) |
|
143 | + { |
|
144 | + $encryption = strtolower($encryption ?? ''); |
|
145 | + if ('tls' == $encryption) { |
|
146 | + $this->params['protocol'] = 'tcp'; |
|
147 | + $this->params['tls'] = true; |
|
148 | + } else { |
|
149 | + $this->params['protocol'] = $encryption; |
|
150 | + $this->params['tls'] = false; |
|
151 | + } |
|
152 | + |
|
153 | + return $this; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get the encryption type. |
|
158 | + * |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function getEncryption() |
|
162 | + { |
|
163 | + return $this->params['tls'] ? 'tls' : $this->params['protocol']; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Sets the stream context options. |
|
168 | + * |
|
169 | + * @param array $options |
|
170 | + * |
|
171 | + * @return $this |
|
172 | + */ |
|
173 | + public function setStreamOptions($options) |
|
174 | + { |
|
175 | + $this->params['stream_context_options'] = $options; |
|
176 | + |
|
177 | + return $this; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Returns the stream context options. |
|
182 | + * |
|
183 | + * @return array |
|
184 | + */ |
|
185 | + public function getStreamOptions() |
|
186 | + { |
|
187 | + return $this->params['stream_context_options']; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Sets the source IP. |
|
192 | + * |
|
193 | + * IPv6 addresses should be wrapped in square brackets. |
|
194 | + * |
|
195 | + * @param string $source |
|
196 | + * |
|
197 | + * @return $this |
|
198 | + */ |
|
199 | + public function setSourceIp($source) |
|
200 | + { |
|
201 | + $this->params['sourceIp'] = $source; |
|
202 | + |
|
203 | + return $this; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Returns the IP used to connect to the destination. |
|
208 | + * |
|
209 | + * @return string |
|
210 | + */ |
|
211 | + public function getSourceIp() |
|
212 | + { |
|
213 | + return $this->params['sourceIp'] ?? null; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Sets whether SMTP pipelining is enabled. |
|
218 | + * |
|
219 | + * By default, support is auto-detected using the PIPELINING SMTP extension. |
|
220 | + * Use this function to override that in the unlikely event of compatibility |
|
221 | + * issues. |
|
222 | + * |
|
223 | + * @param bool $enabled |
|
224 | + * |
|
225 | + * @return $this |
|
226 | + */ |
|
227 | + public function setPipelining($enabled) |
|
228 | + { |
|
229 | + $this->pipelining = $enabled; |
|
230 | + |
|
231 | + return $this; |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Returns whether SMTP pipelining is enabled. |
|
236 | + * |
|
237 | + * @return bool|null a boolean if pipelining is explicitly enabled or disabled, |
|
238 | + * or null if support is auto-detected |
|
239 | + */ |
|
240 | + public function getPipelining() |
|
241 | + { |
|
242 | + return $this->pipelining; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Set ESMTP extension handlers. |
|
247 | + * |
|
248 | + * @param Swift_Transport_EsmtpHandler[] $handlers |
|
249 | + * |
|
250 | + * @return $this |
|
251 | + */ |
|
252 | + public function setExtensionHandlers(array $handlers) |
|
253 | + { |
|
254 | + $assoc = []; |
|
255 | + foreach ($handlers as $handler) { |
|
256 | + $assoc[$handler->getHandledKeyword()] = $handler; |
|
257 | + } |
|
258 | + uasort($assoc, function ($a, $b) { |
|
259 | + return $a->getPriorityOver($b->getHandledKeyword()); |
|
260 | + }); |
|
261 | + $this->handlers = $assoc; |
|
262 | + $this->setHandlerParams(); |
|
263 | + |
|
264 | + return $this; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Get ESMTP extension handlers. |
|
269 | + * |
|
270 | + * @return Swift_Transport_EsmtpHandler[] |
|
271 | + */ |
|
272 | + public function getExtensionHandlers() |
|
273 | + { |
|
274 | + return array_values($this->handlers); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Run a command against the buffer, expecting the given response codes. |
|
279 | + * |
|
280 | + * If no response codes are given, the response will not be validated. |
|
281 | + * If codes are given, an exception will be thrown on an invalid response. |
|
282 | + * |
|
283 | + * @param string $command |
|
284 | + * @param int[] $codes |
|
285 | + * @param string[] $failures An array of failures by-reference |
|
286 | + * @param bool $pipeline Do not wait for response |
|
287 | + * @param string $address the address, if command is RCPT TO |
|
288 | + * |
|
289 | + * @return string|null The server response, or null if pipelining is enabled |
|
290 | + */ |
|
291 | + public function executeCommand($command, $codes = [], &$failures = null, $pipeline = false, $address = null) |
|
292 | + { |
|
293 | + $failures = (array) $failures; |
|
294 | + $stopSignal = false; |
|
295 | + $response = null; |
|
296 | + foreach ($this->getActiveHandlers() as $handler) { |
|
297 | + $response = $handler->onCommand( |
|
298 | + $this, $command, $codes, $failures, $stopSignal |
|
299 | + ); |
|
300 | + if ($stopSignal) { |
|
301 | + return $response; |
|
302 | + } |
|
303 | + } |
|
304 | + |
|
305 | + return parent::executeCommand($command, $codes, $failures, $pipeline, $address); |
|
306 | + } |
|
307 | + |
|
308 | + /** Mixin handling method for ESMTP handlers */ |
|
309 | + public function __call($method, $args) |
|
310 | + { |
|
311 | + foreach ($this->handlers as $handler) { |
|
312 | + if (\in_array(strtolower($method), |
|
313 | + array_map('strtolower', (array) $handler->exposeMixinMethods()) |
|
314 | + )) { |
|
315 | + $return = \call_user_func_array([$handler, $method], $args); |
|
316 | + // Allow fluid method calls |
|
317 | + if (null === $return && 'set' == substr($method, 0, 3)) { |
|
318 | + return $this; |
|
319 | + } else { |
|
320 | + return $return; |
|
321 | + } |
|
322 | + } |
|
323 | + } |
|
324 | + trigger_error('Call to undefined method '.$method, E_USER_ERROR); |
|
325 | + } |
|
326 | + |
|
327 | + /** Get the params to initialize the buffer */ |
|
328 | + protected function getBufferParams() |
|
329 | + { |
|
330 | + return $this->params; |
|
331 | + } |
|
332 | + |
|
333 | + /** Overridden to perform EHLO instead */ |
|
334 | + protected function doHeloCommand() |
|
335 | + { |
|
336 | + try { |
|
337 | + $response = $this->executeCommand( |
|
338 | + sprintf("EHLO %s\r\n", $this->domain), [250] |
|
339 | + ); |
|
340 | + } catch (Swift_TransportException $e) { |
|
341 | + return parent::doHeloCommand(); |
|
342 | + } |
|
343 | + |
|
344 | + if ($this->params['tls']) { |
|
345 | + try { |
|
346 | + $this->executeCommand("STARTTLS\r\n", [220]); |
|
347 | + |
|
348 | + if (!$this->buffer->startTLS()) { |
|
349 | + throw new Swift_TransportException('Unable to connect with TLS encryption'); |
|
350 | + } |
|
351 | + |
|
352 | + try { |
|
353 | + $response = $this->executeCommand( |
|
354 | + sprintf("EHLO %s\r\n", $this->domain), [250] |
|
355 | + ); |
|
356 | + } catch (Swift_TransportException $e) { |
|
357 | + return parent::doHeloCommand(); |
|
358 | + } |
|
359 | + } catch (Swift_TransportException $e) { |
|
360 | + $this->throwException($e); |
|
361 | + } |
|
362 | + } |
|
363 | + |
|
364 | + $this->capabilities = $this->getCapabilities($response); |
|
365 | + if (!isset($this->pipelining)) { |
|
366 | + $this->pipelining = isset($this->capabilities['PIPELINING']); |
|
367 | + } |
|
368 | + |
|
369 | + $this->setHandlerParams(); |
|
370 | + foreach ($this->getActiveHandlers() as $handler) { |
|
371 | + $handler->afterEhlo($this); |
|
372 | + } |
|
373 | + } |
|
374 | + |
|
375 | + /** Overridden to add Extension support */ |
|
376 | + protected function doMailFromCommand($address) |
|
377 | + { |
|
378 | + $address = $this->addressEncoder->encodeString($address); |
|
379 | + $handlers = $this->getActiveHandlers(); |
|
380 | + $params = []; |
|
381 | + foreach ($handlers as $handler) { |
|
382 | + $params = array_merge($params, (array) $handler->getMailParams()); |
|
383 | + } |
|
384 | + $paramStr = !empty($params) ? ' '.implode(' ', $params) : ''; |
|
385 | + $this->executeCommand( |
|
386 | + sprintf("MAIL FROM:<%s>%s\r\n", $address, $paramStr), [250], $failures, true |
|
387 | + ); |
|
388 | + } |
|
389 | + |
|
390 | + /** Overridden to add Extension support */ |
|
391 | + protected function doRcptToCommand($address) |
|
392 | + { |
|
393 | + $address = $this->addressEncoder->encodeString($address); |
|
394 | + $handlers = $this->getActiveHandlers(); |
|
395 | + $params = []; |
|
396 | + foreach ($handlers as $handler) { |
|
397 | + $params = array_merge($params, (array) $handler->getRcptParams()); |
|
398 | + } |
|
399 | + $paramStr = !empty($params) ? ' '.implode(' ', $params) : ''; |
|
400 | + $this->executeCommand( |
|
401 | + sprintf("RCPT TO:<%s>%s\r\n", $address, $paramStr), [250, 251, 252], $failures, true, $address |
|
402 | + ); |
|
403 | + } |
|
404 | + |
|
405 | + /** Determine ESMTP capabilities by function group */ |
|
406 | + private function getCapabilities($ehloResponse) |
|
407 | + { |
|
408 | + $capabilities = []; |
|
409 | + $ehloResponse = trim($ehloResponse ?? ''); |
|
410 | + $lines = explode("\r\n", $ehloResponse); |
|
411 | + array_shift($lines); |
|
412 | + foreach ($lines as $line) { |
|
413 | + if (preg_match('/^[0-9]{3}[ -]([A-Z0-9-]+)((?:[ =].*)?)$/Di', $line, $matches)) { |
|
414 | + $keyword = strtoupper($matches[1]); |
|
415 | + $paramStr = strtoupper(ltrim($matches[2], ' =')); |
|
416 | + $params = !empty($paramStr) ? explode(' ', $paramStr) : []; |
|
417 | + $capabilities[$keyword] = $params; |
|
418 | + } |
|
419 | + } |
|
420 | + |
|
421 | + return $capabilities; |
|
422 | + } |
|
423 | + |
|
424 | + /** Set parameters which are used by each extension handler */ |
|
425 | + private function setHandlerParams() |
|
426 | + { |
|
427 | + foreach ($this->handlers as $keyword => $handler) { |
|
428 | + if (\array_key_exists($keyword, $this->capabilities)) { |
|
429 | + $handler->setKeywordParams($this->capabilities[$keyword]); |
|
430 | + } |
|
431 | + } |
|
432 | + } |
|
433 | + |
|
434 | + /** Get ESMTP handlers which are currently ok to use */ |
|
435 | + private function getActiveHandlers() |
|
436 | + { |
|
437 | + $handlers = []; |
|
438 | + foreach ($this->handlers as $keyword => $handler) { |
|
439 | + if (\array_key_exists($keyword, $this->capabilities)) { |
|
440 | + $handlers[] = $handler; |
|
441 | + } |
|
442 | + } |
|
443 | + |
|
444 | + return $handlers; |
|
445 | + } |
|
446 | 446 | } |
@@ -255,7 +255,7 @@ |
||
255 | 255 | foreach ($handlers as $handler) { |
256 | 256 | $assoc[$handler->getHandledKeyword()] = $handler; |
257 | 257 | } |
258 | - uasort($assoc, function ($a, $b) { |
|
258 | + uasort($assoc, function($a, $b) { |
|
259 | 259 | return $a->getPriorityOver($b->getHandledKeyword()); |
260 | 260 | }); |
261 | 261 | $this->handlers = $assoc; |
@@ -15,527 +15,527 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class Swift_Transport_AbstractSmtpTransport implements Swift_Transport |
17 | 17 | { |
18 | - /** Input-Output buffer for sending/receiving SMTP commands and responses */ |
|
19 | - protected $buffer; |
|
20 | - |
|
21 | - /** Connection status */ |
|
22 | - protected $started = false; |
|
23 | - |
|
24 | - /** The domain name to use in HELO command */ |
|
25 | - protected $domain = '[127.0.0.1]'; |
|
26 | - |
|
27 | - /** The event dispatching layer */ |
|
28 | - protected $eventDispatcher; |
|
29 | - |
|
30 | - protected $addressEncoder; |
|
31 | - |
|
32 | - /** Whether the PIPELINING SMTP extension is enabled (RFC 2920) */ |
|
33 | - protected $pipelining = null; |
|
34 | - |
|
35 | - /** The pipelined commands waiting for response */ |
|
36 | - protected $pipeline = []; |
|
37 | - |
|
38 | - /** Source Ip */ |
|
39 | - protected $sourceIp; |
|
40 | - |
|
41 | - /** Return an array of params for the Buffer */ |
|
42 | - abstract protected function getBufferParams(); |
|
43 | - |
|
44 | - /** |
|
45 | - * Creates a new EsmtpTransport using the given I/O buffer. |
|
46 | - * |
|
47 | - * @param string $localDomain |
|
48 | - */ |
|
49 | - public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
50 | - { |
|
51 | - $this->buffer = $buf; |
|
52 | - $this->eventDispatcher = $dispatcher; |
|
53 | - $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder(); |
|
54 | - $this->setLocalDomain($localDomain); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Set the name of the local domain which Swift will identify itself as. |
|
59 | - * |
|
60 | - * This should be a fully-qualified domain name and should be truly the domain |
|
61 | - * you're using. |
|
62 | - * |
|
63 | - * If your server does not have a domain name, use the IP address. This will |
|
64 | - * automatically be wrapped in square brackets as described in RFC 5321, |
|
65 | - * section 4.1.3. |
|
66 | - * |
|
67 | - * @param string $domain |
|
68 | - * |
|
69 | - * @return $this |
|
70 | - */ |
|
71 | - public function setLocalDomain($domain) |
|
72 | - { |
|
73 | - if ('[' !== substr($domain, 0, 1)) { |
|
74 | - if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
75 | - $domain = '['.$domain.']'; |
|
76 | - } elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
77 | - $domain = '[IPv6:'.$domain.']'; |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - $this->domain = $domain; |
|
82 | - |
|
83 | - return $this; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Get the name of the domain Swift will identify as. |
|
88 | - * |
|
89 | - * If an IP address was specified, this will be returned wrapped in square |
|
90 | - * brackets as described in RFC 5321, section 4.1.3. |
|
91 | - * |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function getLocalDomain() |
|
95 | - { |
|
96 | - return $this->domain; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Sets the source IP. |
|
101 | - * |
|
102 | - * @param string $source |
|
103 | - */ |
|
104 | - public function setSourceIp($source) |
|
105 | - { |
|
106 | - $this->sourceIp = $source; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Returns the IP used to connect to the destination. |
|
111 | - * |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function getSourceIp() |
|
115 | - { |
|
116 | - return $this->sourceIp; |
|
117 | - } |
|
118 | - |
|
119 | - public function setAddressEncoder(Swift_AddressEncoder $addressEncoder) |
|
120 | - { |
|
121 | - $this->addressEncoder = $addressEncoder; |
|
122 | - } |
|
123 | - |
|
124 | - public function getAddressEncoder() |
|
125 | - { |
|
126 | - return $this->addressEncoder; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Start the SMTP connection. |
|
131 | - */ |
|
132 | - public function start() |
|
133 | - { |
|
134 | - if (!$this->started) { |
|
135 | - if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
136 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted'); |
|
137 | - if ($evt->bubbleCancelled()) { |
|
138 | - return; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - try { |
|
143 | - $this->buffer->initialize($this->getBufferParams()); |
|
144 | - } catch (Swift_TransportException $e) { |
|
145 | - $this->throwException($e); |
|
146 | - } |
|
147 | - $this->readGreeting(); |
|
148 | - $this->doHeloCommand(); |
|
149 | - |
|
150 | - if ($evt) { |
|
151 | - $this->eventDispatcher->dispatchEvent($evt, 'transportStarted'); |
|
152 | - } |
|
153 | - |
|
154 | - $this->started = true; |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Test if an SMTP connection has been established. |
|
160 | - * |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - public function isStarted() |
|
164 | - { |
|
165 | - return $this->started; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Send the given Message. |
|
170 | - * |
|
171 | - * Recipient/sender data will be retrieved from the Message API. |
|
172 | - * The return value is the number of recipients who were accepted for delivery. |
|
173 | - * |
|
174 | - * @param string[] $failedRecipients An array of failures by-reference |
|
175 | - * |
|
176 | - * @return int |
|
177 | - */ |
|
178 | - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
179 | - { |
|
180 | - if (!$this->isStarted()) { |
|
181 | - $this->start(); |
|
182 | - } |
|
183 | - |
|
184 | - $sent = 0; |
|
185 | - $failedRecipients = (array) $failedRecipients; |
|
186 | - |
|
187 | - if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
188 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
189 | - if ($evt->bubbleCancelled()) { |
|
190 | - return 0; |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - if (!$reversePath = $this->getReversePath($message)) { |
|
195 | - $this->throwException(new Swift_TransportException('Cannot send message without a sender address')); |
|
196 | - } |
|
197 | - |
|
198 | - $to = (array) $message->getTo(); |
|
199 | - $cc = (array) $message->getCc(); |
|
200 | - $bcc = (array) $message->getBcc(); |
|
201 | - $tos = array_merge($to, $cc, $bcc); |
|
202 | - |
|
203 | - $message->setBcc([]); |
|
204 | - |
|
205 | - try { |
|
206 | - $sent += $this->sendTo($message, $reversePath, $tos, $failedRecipients); |
|
207 | - } finally { |
|
208 | - $message->setBcc($bcc); |
|
209 | - } |
|
210 | - |
|
211 | - if ($evt) { |
|
212 | - if ($sent == \count($to) + \count($cc) + \count($bcc)) { |
|
213 | - $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
214 | - } elseif ($sent > 0) { |
|
215 | - $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); |
|
216 | - } else { |
|
217 | - $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
|
218 | - } |
|
219 | - $evt->setFailedRecipients($failedRecipients); |
|
220 | - $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
221 | - } |
|
222 | - |
|
223 | - $message->generateId(); //Make sure a new Message ID is used |
|
224 | - |
|
225 | - return $sent; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Stop the SMTP connection. |
|
230 | - */ |
|
231 | - public function stop() |
|
232 | - { |
|
233 | - if ($this->started) { |
|
234 | - if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
235 | - $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped'); |
|
236 | - if ($evt->bubbleCancelled()) { |
|
237 | - return; |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - try { |
|
242 | - $this->executeCommand("QUIT\r\n", [221]); |
|
243 | - } catch (Swift_TransportException $e) { |
|
244 | - } |
|
245 | - |
|
246 | - try { |
|
247 | - $this->buffer->terminate(); |
|
248 | - |
|
249 | - if ($evt) { |
|
250 | - $this->eventDispatcher->dispatchEvent($evt, 'transportStopped'); |
|
251 | - } |
|
252 | - } catch (Swift_TransportException $e) { |
|
253 | - $this->throwException($e); |
|
254 | - } |
|
255 | - } |
|
256 | - $this->started = false; |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * {@inheritdoc} |
|
261 | - */ |
|
262 | - public function ping() |
|
263 | - { |
|
264 | - try { |
|
265 | - if (!$this->isStarted()) { |
|
266 | - $this->start(); |
|
267 | - } |
|
268 | - |
|
269 | - $this->executeCommand("NOOP\r\n", [250]); |
|
270 | - } catch (Swift_TransportException $e) { |
|
271 | - try { |
|
272 | - $this->stop(); |
|
273 | - } catch (Swift_TransportException $e) { |
|
274 | - } |
|
275 | - |
|
276 | - return false; |
|
277 | - } |
|
278 | - |
|
279 | - return true; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Register a plugin. |
|
284 | - */ |
|
285 | - public function registerPlugin(Swift_Events_EventListener $plugin) |
|
286 | - { |
|
287 | - $this->eventDispatcher->bindEventListener($plugin); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Reset the current mail transaction. |
|
292 | - */ |
|
293 | - public function reset() |
|
294 | - { |
|
295 | - $this->executeCommand("RSET\r\n", [250], $failures, true); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Get the IoBuffer where read/writes are occurring. |
|
300 | - * |
|
301 | - * @return Swift_Transport_IoBuffer |
|
302 | - */ |
|
303 | - public function getBuffer() |
|
304 | - { |
|
305 | - return $this->buffer; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Run a command against the buffer, expecting the given response codes. |
|
310 | - * |
|
311 | - * If no response codes are given, the response will not be validated. |
|
312 | - * If codes are given, an exception will be thrown on an invalid response. |
|
313 | - * If the command is RCPT TO, and the pipeline is non-empty, no exception |
|
314 | - * will be thrown; instead the failing address is added to $failures. |
|
315 | - * |
|
316 | - * @param string $command |
|
317 | - * @param int[] $codes |
|
318 | - * @param string[] $failures An array of failures by-reference |
|
319 | - * @param bool $pipeline Do not wait for response |
|
320 | - * @param string $address the address, if command is RCPT TO |
|
321 | - * |
|
322 | - * @return string|null The server response, or null if pipelining is enabled |
|
323 | - */ |
|
324 | - public function executeCommand($command, $codes = [], &$failures = null, $pipeline = false, $address = null) |
|
325 | - { |
|
326 | - $failures = (array) $failures; |
|
327 | - $seq = $this->buffer->write($command); |
|
328 | - if ($evt = $this->eventDispatcher->createCommandEvent($this, $command, $codes)) { |
|
329 | - $this->eventDispatcher->dispatchEvent($evt, 'commandSent'); |
|
330 | - } |
|
331 | - |
|
332 | - $this->pipeline[] = [$command, $seq, $codes, $address]; |
|
333 | - |
|
334 | - if ($pipeline && $this->pipelining) { |
|
335 | - return null; |
|
336 | - } |
|
337 | - |
|
338 | - $response = null; |
|
339 | - |
|
340 | - while ($this->pipeline) { |
|
341 | - list($command, $seq, $codes, $address) = array_shift($this->pipeline); |
|
342 | - $response = $this->getFullResponse($seq); |
|
343 | - try { |
|
344 | - $this->assertResponseCode($response, $codes); |
|
345 | - } catch (Swift_TransportException $e) { |
|
346 | - if ($this->pipeline && $address) { |
|
347 | - $failures[] = $address; |
|
348 | - } else { |
|
349 | - $this->throwException($e); |
|
350 | - } |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - return $response; |
|
355 | - } |
|
356 | - |
|
357 | - /** Read the opening SMTP greeting */ |
|
358 | - protected function readGreeting() |
|
359 | - { |
|
360 | - $this->assertResponseCode($this->getFullResponse(0), [220]); |
|
361 | - } |
|
362 | - |
|
363 | - /** Send the HELO welcome */ |
|
364 | - protected function doHeloCommand() |
|
365 | - { |
|
366 | - $this->executeCommand( |
|
367 | - sprintf("HELO %s\r\n", $this->domain), [250] |
|
368 | - ); |
|
369 | - } |
|
370 | - |
|
371 | - /** Send the MAIL FROM command */ |
|
372 | - protected function doMailFromCommand($address) |
|
373 | - { |
|
374 | - $address = $this->addressEncoder->encodeString($address); |
|
375 | - $this->executeCommand( |
|
376 | - sprintf("MAIL FROM:<%s>\r\n", $address), [250], $failures, true |
|
377 | - ); |
|
378 | - } |
|
379 | - |
|
380 | - /** Send the RCPT TO command */ |
|
381 | - protected function doRcptToCommand($address) |
|
382 | - { |
|
383 | - $address = $this->addressEncoder->encodeString($address); |
|
384 | - $this->executeCommand( |
|
385 | - sprintf("RCPT TO:<%s>\r\n", $address), [250, 251, 252], $failures, true, $address |
|
386 | - ); |
|
387 | - } |
|
388 | - |
|
389 | - /** Send the DATA command */ |
|
390 | - protected function doDataCommand(&$failedRecipients) |
|
391 | - { |
|
392 | - $this->executeCommand("DATA\r\n", [354], $failedRecipients); |
|
393 | - } |
|
394 | - |
|
395 | - /** Stream the contents of the message over the buffer */ |
|
396 | - protected function streamMessage(Swift_Mime_SimpleMessage $message) |
|
397 | - { |
|
398 | - $this->buffer->setWriteTranslations(["\r\n." => "\r\n.."]); |
|
399 | - try { |
|
400 | - $message->toByteStream($this->buffer); |
|
401 | - $this->buffer->flushBuffers(); |
|
402 | - } catch (Swift_TransportException $e) { |
|
403 | - $this->throwException($e); |
|
404 | - } |
|
405 | - $this->buffer->setWriteTranslations([]); |
|
406 | - $this->executeCommand("\r\n.\r\n", [250]); |
|
407 | - } |
|
408 | - |
|
409 | - /** Determine the best-use reverse path for this message */ |
|
410 | - protected function getReversePath(Swift_Mime_SimpleMessage $message) |
|
411 | - { |
|
412 | - $return = $message->getReturnPath(); |
|
413 | - $sender = $message->getSender(); |
|
414 | - $from = $message->getFrom(); |
|
415 | - $path = null; |
|
416 | - if (!empty($return)) { |
|
417 | - $path = $return; |
|
418 | - } elseif (!empty($sender)) { |
|
419 | - // Don't use array_keys |
|
420 | - reset($sender); // Reset Pointer to first pos |
|
421 | - $path = key($sender); // Get key |
|
422 | - } elseif (!empty($from)) { |
|
423 | - reset($from); // Reset Pointer to first pos |
|
424 | - $path = key($from); // Get key |
|
425 | - } |
|
426 | - |
|
427 | - return $path; |
|
428 | - } |
|
429 | - |
|
430 | - /** Throw a TransportException, first sending it to any listeners */ |
|
431 | - protected function throwException(Swift_TransportException $e) |
|
432 | - { |
|
433 | - if ($evt = $this->eventDispatcher->createTransportExceptionEvent($this, $e)) { |
|
434 | - $this->eventDispatcher->dispatchEvent($evt, 'exceptionThrown'); |
|
435 | - if (!$evt->bubbleCancelled()) { |
|
436 | - throw $e; |
|
437 | - } |
|
438 | - } else { |
|
439 | - throw $e; |
|
440 | - } |
|
441 | - } |
|
442 | - |
|
443 | - /** Throws an Exception if a response code is incorrect */ |
|
444 | - protected function assertResponseCode($response, $wanted) |
|
445 | - { |
|
446 | - if (!$response) { |
|
447 | - $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got an empty response')); |
|
448 | - } |
|
449 | - |
|
450 | - list($code) = sscanf($response, '%3d'); |
|
451 | - $valid = (empty($wanted) || \in_array($code, $wanted)); |
|
452 | - |
|
453 | - if ($evt = $this->eventDispatcher->createResponseEvent($this, $response, |
|
454 | - $valid)) { |
|
455 | - $this->eventDispatcher->dispatchEvent($evt, 'responseReceived'); |
|
456 | - } |
|
457 | - |
|
458 | - if (!$valid) { |
|
459 | - $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got code "'.$code.'", with message "'.$response.'"', $code)); |
|
460 | - } |
|
461 | - } |
|
462 | - |
|
463 | - /** Get an entire multi-line response using its sequence number */ |
|
464 | - protected function getFullResponse($seq) |
|
465 | - { |
|
466 | - $response = ''; |
|
467 | - try { |
|
468 | - do { |
|
469 | - $line = $this->buffer->readLine($seq); |
|
470 | - $response .= $line; |
|
471 | - } while (null !== $line && false !== $line && ' ' != $line[3]); |
|
472 | - } catch (Swift_TransportException $e) { |
|
473 | - $this->throwException($e); |
|
474 | - } catch (Swift_IoException $e) { |
|
475 | - $this->throwException(new Swift_TransportException($e->getMessage(), 0, $e)); |
|
476 | - } |
|
477 | - |
|
478 | - return $response; |
|
479 | - } |
|
480 | - |
|
481 | - /** Send an email to the given recipients from the given reverse path */ |
|
482 | - private function doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients) |
|
483 | - { |
|
484 | - $sent = 0; |
|
485 | - $this->doMailFromCommand($reversePath); |
|
486 | - foreach ($recipients as $forwardPath) { |
|
487 | - try { |
|
488 | - $this->doRcptToCommand($forwardPath); |
|
489 | - ++$sent; |
|
490 | - } catch (Swift_TransportException $e) { |
|
491 | - $failedRecipients[] = $forwardPath; |
|
492 | - } catch (Swift_AddressEncoderException $e) { |
|
493 | - $failedRecipients[] = $forwardPath; |
|
494 | - } |
|
495 | - } |
|
496 | - |
|
497 | - if (0 != $sent) { |
|
498 | - $sent += \count($failedRecipients); |
|
499 | - $this->doDataCommand($failedRecipients); |
|
500 | - $sent -= \count($failedRecipients); |
|
501 | - |
|
502 | - $this->streamMessage($message); |
|
503 | - } else { |
|
504 | - $this->reset(); |
|
505 | - } |
|
506 | - |
|
507 | - return $sent; |
|
508 | - } |
|
509 | - |
|
510 | - /** Send a message to the given To: recipients */ |
|
511 | - private function sendTo(Swift_Mime_SimpleMessage $message, $reversePath, array $to, array &$failedRecipients) |
|
512 | - { |
|
513 | - if (empty($to)) { |
|
514 | - return 0; |
|
515 | - } |
|
516 | - |
|
517 | - return $this->doMailTransaction($message, $reversePath, array_keys($to), |
|
518 | - $failedRecipients); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Destructor. |
|
523 | - */ |
|
524 | - public function __destruct() |
|
525 | - { |
|
526 | - try { |
|
527 | - $this->stop(); |
|
528 | - } catch (Exception $e) { |
|
529 | - } |
|
530 | - } |
|
531 | - |
|
532 | - public function __sleep() |
|
533 | - { |
|
534 | - throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
535 | - } |
|
536 | - |
|
537 | - public function __wakeup() |
|
538 | - { |
|
539 | - throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
540 | - } |
|
18 | + /** Input-Output buffer for sending/receiving SMTP commands and responses */ |
|
19 | + protected $buffer; |
|
20 | + |
|
21 | + /** Connection status */ |
|
22 | + protected $started = false; |
|
23 | + |
|
24 | + /** The domain name to use in HELO command */ |
|
25 | + protected $domain = '[127.0.0.1]'; |
|
26 | + |
|
27 | + /** The event dispatching layer */ |
|
28 | + protected $eventDispatcher; |
|
29 | + |
|
30 | + protected $addressEncoder; |
|
31 | + |
|
32 | + /** Whether the PIPELINING SMTP extension is enabled (RFC 2920) */ |
|
33 | + protected $pipelining = null; |
|
34 | + |
|
35 | + /** The pipelined commands waiting for response */ |
|
36 | + protected $pipeline = []; |
|
37 | + |
|
38 | + /** Source Ip */ |
|
39 | + protected $sourceIp; |
|
40 | + |
|
41 | + /** Return an array of params for the Buffer */ |
|
42 | + abstract protected function getBufferParams(); |
|
43 | + |
|
44 | + /** |
|
45 | + * Creates a new EsmtpTransport using the given I/O buffer. |
|
46 | + * |
|
47 | + * @param string $localDomain |
|
48 | + */ |
|
49 | + public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher, $localDomain = '127.0.0.1', Swift_AddressEncoder $addressEncoder = null) |
|
50 | + { |
|
51 | + $this->buffer = $buf; |
|
52 | + $this->eventDispatcher = $dispatcher; |
|
53 | + $this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder(); |
|
54 | + $this->setLocalDomain($localDomain); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Set the name of the local domain which Swift will identify itself as. |
|
59 | + * |
|
60 | + * This should be a fully-qualified domain name and should be truly the domain |
|
61 | + * you're using. |
|
62 | + * |
|
63 | + * If your server does not have a domain name, use the IP address. This will |
|
64 | + * automatically be wrapped in square brackets as described in RFC 5321, |
|
65 | + * section 4.1.3. |
|
66 | + * |
|
67 | + * @param string $domain |
|
68 | + * |
|
69 | + * @return $this |
|
70 | + */ |
|
71 | + public function setLocalDomain($domain) |
|
72 | + { |
|
73 | + if ('[' !== substr($domain, 0, 1)) { |
|
74 | + if (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
75 | + $domain = '['.$domain.']'; |
|
76 | + } elseif (filter_var($domain, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
77 | + $domain = '[IPv6:'.$domain.']'; |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + $this->domain = $domain; |
|
82 | + |
|
83 | + return $this; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Get the name of the domain Swift will identify as. |
|
88 | + * |
|
89 | + * If an IP address was specified, this will be returned wrapped in square |
|
90 | + * brackets as described in RFC 5321, section 4.1.3. |
|
91 | + * |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function getLocalDomain() |
|
95 | + { |
|
96 | + return $this->domain; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Sets the source IP. |
|
101 | + * |
|
102 | + * @param string $source |
|
103 | + */ |
|
104 | + public function setSourceIp($source) |
|
105 | + { |
|
106 | + $this->sourceIp = $source; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Returns the IP used to connect to the destination. |
|
111 | + * |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function getSourceIp() |
|
115 | + { |
|
116 | + return $this->sourceIp; |
|
117 | + } |
|
118 | + |
|
119 | + public function setAddressEncoder(Swift_AddressEncoder $addressEncoder) |
|
120 | + { |
|
121 | + $this->addressEncoder = $addressEncoder; |
|
122 | + } |
|
123 | + |
|
124 | + public function getAddressEncoder() |
|
125 | + { |
|
126 | + return $this->addressEncoder; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Start the SMTP connection. |
|
131 | + */ |
|
132 | + public function start() |
|
133 | + { |
|
134 | + if (!$this->started) { |
|
135 | + if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
136 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStarted'); |
|
137 | + if ($evt->bubbleCancelled()) { |
|
138 | + return; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + try { |
|
143 | + $this->buffer->initialize($this->getBufferParams()); |
|
144 | + } catch (Swift_TransportException $e) { |
|
145 | + $this->throwException($e); |
|
146 | + } |
|
147 | + $this->readGreeting(); |
|
148 | + $this->doHeloCommand(); |
|
149 | + |
|
150 | + if ($evt) { |
|
151 | + $this->eventDispatcher->dispatchEvent($evt, 'transportStarted'); |
|
152 | + } |
|
153 | + |
|
154 | + $this->started = true; |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Test if an SMTP connection has been established. |
|
160 | + * |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + public function isStarted() |
|
164 | + { |
|
165 | + return $this->started; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Send the given Message. |
|
170 | + * |
|
171 | + * Recipient/sender data will be retrieved from the Message API. |
|
172 | + * The return value is the number of recipients who were accepted for delivery. |
|
173 | + * |
|
174 | + * @param string[] $failedRecipients An array of failures by-reference |
|
175 | + * |
|
176 | + * @return int |
|
177 | + */ |
|
178 | + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
179 | + { |
|
180 | + if (!$this->isStarted()) { |
|
181 | + $this->start(); |
|
182 | + } |
|
183 | + |
|
184 | + $sent = 0; |
|
185 | + $failedRecipients = (array) $failedRecipients; |
|
186 | + |
|
187 | + if ($evt = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
188 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
|
189 | + if ($evt->bubbleCancelled()) { |
|
190 | + return 0; |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + if (!$reversePath = $this->getReversePath($message)) { |
|
195 | + $this->throwException(new Swift_TransportException('Cannot send message without a sender address')); |
|
196 | + } |
|
197 | + |
|
198 | + $to = (array) $message->getTo(); |
|
199 | + $cc = (array) $message->getCc(); |
|
200 | + $bcc = (array) $message->getBcc(); |
|
201 | + $tos = array_merge($to, $cc, $bcc); |
|
202 | + |
|
203 | + $message->setBcc([]); |
|
204 | + |
|
205 | + try { |
|
206 | + $sent += $this->sendTo($message, $reversePath, $tos, $failedRecipients); |
|
207 | + } finally { |
|
208 | + $message->setBcc($bcc); |
|
209 | + } |
|
210 | + |
|
211 | + if ($evt) { |
|
212 | + if ($sent == \count($to) + \count($cc) + \count($bcc)) { |
|
213 | + $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
214 | + } elseif ($sent > 0) { |
|
215 | + $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); |
|
216 | + } else { |
|
217 | + $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
|
218 | + } |
|
219 | + $evt->setFailedRecipients($failedRecipients); |
|
220 | + $this->eventDispatcher->dispatchEvent($evt, 'sendPerformed'); |
|
221 | + } |
|
222 | + |
|
223 | + $message->generateId(); //Make sure a new Message ID is used |
|
224 | + |
|
225 | + return $sent; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Stop the SMTP connection. |
|
230 | + */ |
|
231 | + public function stop() |
|
232 | + { |
|
233 | + if ($this->started) { |
|
234 | + if ($evt = $this->eventDispatcher->createTransportChangeEvent($this)) { |
|
235 | + $this->eventDispatcher->dispatchEvent($evt, 'beforeTransportStopped'); |
|
236 | + if ($evt->bubbleCancelled()) { |
|
237 | + return; |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + try { |
|
242 | + $this->executeCommand("QUIT\r\n", [221]); |
|
243 | + } catch (Swift_TransportException $e) { |
|
244 | + } |
|
245 | + |
|
246 | + try { |
|
247 | + $this->buffer->terminate(); |
|
248 | + |
|
249 | + if ($evt) { |
|
250 | + $this->eventDispatcher->dispatchEvent($evt, 'transportStopped'); |
|
251 | + } |
|
252 | + } catch (Swift_TransportException $e) { |
|
253 | + $this->throwException($e); |
|
254 | + } |
|
255 | + } |
|
256 | + $this->started = false; |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * {@inheritdoc} |
|
261 | + */ |
|
262 | + public function ping() |
|
263 | + { |
|
264 | + try { |
|
265 | + if (!$this->isStarted()) { |
|
266 | + $this->start(); |
|
267 | + } |
|
268 | + |
|
269 | + $this->executeCommand("NOOP\r\n", [250]); |
|
270 | + } catch (Swift_TransportException $e) { |
|
271 | + try { |
|
272 | + $this->stop(); |
|
273 | + } catch (Swift_TransportException $e) { |
|
274 | + } |
|
275 | + |
|
276 | + return false; |
|
277 | + } |
|
278 | + |
|
279 | + return true; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Register a plugin. |
|
284 | + */ |
|
285 | + public function registerPlugin(Swift_Events_EventListener $plugin) |
|
286 | + { |
|
287 | + $this->eventDispatcher->bindEventListener($plugin); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Reset the current mail transaction. |
|
292 | + */ |
|
293 | + public function reset() |
|
294 | + { |
|
295 | + $this->executeCommand("RSET\r\n", [250], $failures, true); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Get the IoBuffer where read/writes are occurring. |
|
300 | + * |
|
301 | + * @return Swift_Transport_IoBuffer |
|
302 | + */ |
|
303 | + public function getBuffer() |
|
304 | + { |
|
305 | + return $this->buffer; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Run a command against the buffer, expecting the given response codes. |
|
310 | + * |
|
311 | + * If no response codes are given, the response will not be validated. |
|
312 | + * If codes are given, an exception will be thrown on an invalid response. |
|
313 | + * If the command is RCPT TO, and the pipeline is non-empty, no exception |
|
314 | + * will be thrown; instead the failing address is added to $failures. |
|
315 | + * |
|
316 | + * @param string $command |
|
317 | + * @param int[] $codes |
|
318 | + * @param string[] $failures An array of failures by-reference |
|
319 | + * @param bool $pipeline Do not wait for response |
|
320 | + * @param string $address the address, if command is RCPT TO |
|
321 | + * |
|
322 | + * @return string|null The server response, or null if pipelining is enabled |
|
323 | + */ |
|
324 | + public function executeCommand($command, $codes = [], &$failures = null, $pipeline = false, $address = null) |
|
325 | + { |
|
326 | + $failures = (array) $failures; |
|
327 | + $seq = $this->buffer->write($command); |
|
328 | + if ($evt = $this->eventDispatcher->createCommandEvent($this, $command, $codes)) { |
|
329 | + $this->eventDispatcher->dispatchEvent($evt, 'commandSent'); |
|
330 | + } |
|
331 | + |
|
332 | + $this->pipeline[] = [$command, $seq, $codes, $address]; |
|
333 | + |
|
334 | + if ($pipeline && $this->pipelining) { |
|
335 | + return null; |
|
336 | + } |
|
337 | + |
|
338 | + $response = null; |
|
339 | + |
|
340 | + while ($this->pipeline) { |
|
341 | + list($command, $seq, $codes, $address) = array_shift($this->pipeline); |
|
342 | + $response = $this->getFullResponse($seq); |
|
343 | + try { |
|
344 | + $this->assertResponseCode($response, $codes); |
|
345 | + } catch (Swift_TransportException $e) { |
|
346 | + if ($this->pipeline && $address) { |
|
347 | + $failures[] = $address; |
|
348 | + } else { |
|
349 | + $this->throwException($e); |
|
350 | + } |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + return $response; |
|
355 | + } |
|
356 | + |
|
357 | + /** Read the opening SMTP greeting */ |
|
358 | + protected function readGreeting() |
|
359 | + { |
|
360 | + $this->assertResponseCode($this->getFullResponse(0), [220]); |
|
361 | + } |
|
362 | + |
|
363 | + /** Send the HELO welcome */ |
|
364 | + protected function doHeloCommand() |
|
365 | + { |
|
366 | + $this->executeCommand( |
|
367 | + sprintf("HELO %s\r\n", $this->domain), [250] |
|
368 | + ); |
|
369 | + } |
|
370 | + |
|
371 | + /** Send the MAIL FROM command */ |
|
372 | + protected function doMailFromCommand($address) |
|
373 | + { |
|
374 | + $address = $this->addressEncoder->encodeString($address); |
|
375 | + $this->executeCommand( |
|
376 | + sprintf("MAIL FROM:<%s>\r\n", $address), [250], $failures, true |
|
377 | + ); |
|
378 | + } |
|
379 | + |
|
380 | + /** Send the RCPT TO command */ |
|
381 | + protected function doRcptToCommand($address) |
|
382 | + { |
|
383 | + $address = $this->addressEncoder->encodeString($address); |
|
384 | + $this->executeCommand( |
|
385 | + sprintf("RCPT TO:<%s>\r\n", $address), [250, 251, 252], $failures, true, $address |
|
386 | + ); |
|
387 | + } |
|
388 | + |
|
389 | + /** Send the DATA command */ |
|
390 | + protected function doDataCommand(&$failedRecipients) |
|
391 | + { |
|
392 | + $this->executeCommand("DATA\r\n", [354], $failedRecipients); |
|
393 | + } |
|
394 | + |
|
395 | + /** Stream the contents of the message over the buffer */ |
|
396 | + protected function streamMessage(Swift_Mime_SimpleMessage $message) |
|
397 | + { |
|
398 | + $this->buffer->setWriteTranslations(["\r\n." => "\r\n.."]); |
|
399 | + try { |
|
400 | + $message->toByteStream($this->buffer); |
|
401 | + $this->buffer->flushBuffers(); |
|
402 | + } catch (Swift_TransportException $e) { |
|
403 | + $this->throwException($e); |
|
404 | + } |
|
405 | + $this->buffer->setWriteTranslations([]); |
|
406 | + $this->executeCommand("\r\n.\r\n", [250]); |
|
407 | + } |
|
408 | + |
|
409 | + /** Determine the best-use reverse path for this message */ |
|
410 | + protected function getReversePath(Swift_Mime_SimpleMessage $message) |
|
411 | + { |
|
412 | + $return = $message->getReturnPath(); |
|
413 | + $sender = $message->getSender(); |
|
414 | + $from = $message->getFrom(); |
|
415 | + $path = null; |
|
416 | + if (!empty($return)) { |
|
417 | + $path = $return; |
|
418 | + } elseif (!empty($sender)) { |
|
419 | + // Don't use array_keys |
|
420 | + reset($sender); // Reset Pointer to first pos |
|
421 | + $path = key($sender); // Get key |
|
422 | + } elseif (!empty($from)) { |
|
423 | + reset($from); // Reset Pointer to first pos |
|
424 | + $path = key($from); // Get key |
|
425 | + } |
|
426 | + |
|
427 | + return $path; |
|
428 | + } |
|
429 | + |
|
430 | + /** Throw a TransportException, first sending it to any listeners */ |
|
431 | + protected function throwException(Swift_TransportException $e) |
|
432 | + { |
|
433 | + if ($evt = $this->eventDispatcher->createTransportExceptionEvent($this, $e)) { |
|
434 | + $this->eventDispatcher->dispatchEvent($evt, 'exceptionThrown'); |
|
435 | + if (!$evt->bubbleCancelled()) { |
|
436 | + throw $e; |
|
437 | + } |
|
438 | + } else { |
|
439 | + throw $e; |
|
440 | + } |
|
441 | + } |
|
442 | + |
|
443 | + /** Throws an Exception if a response code is incorrect */ |
|
444 | + protected function assertResponseCode($response, $wanted) |
|
445 | + { |
|
446 | + if (!$response) { |
|
447 | + $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got an empty response')); |
|
448 | + } |
|
449 | + |
|
450 | + list($code) = sscanf($response, '%3d'); |
|
451 | + $valid = (empty($wanted) || \in_array($code, $wanted)); |
|
452 | + |
|
453 | + if ($evt = $this->eventDispatcher->createResponseEvent($this, $response, |
|
454 | + $valid)) { |
|
455 | + $this->eventDispatcher->dispatchEvent($evt, 'responseReceived'); |
|
456 | + } |
|
457 | + |
|
458 | + if (!$valid) { |
|
459 | + $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got code "'.$code.'", with message "'.$response.'"', $code)); |
|
460 | + } |
|
461 | + } |
|
462 | + |
|
463 | + /** Get an entire multi-line response using its sequence number */ |
|
464 | + protected function getFullResponse($seq) |
|
465 | + { |
|
466 | + $response = ''; |
|
467 | + try { |
|
468 | + do { |
|
469 | + $line = $this->buffer->readLine($seq); |
|
470 | + $response .= $line; |
|
471 | + } while (null !== $line && false !== $line && ' ' != $line[3]); |
|
472 | + } catch (Swift_TransportException $e) { |
|
473 | + $this->throwException($e); |
|
474 | + } catch (Swift_IoException $e) { |
|
475 | + $this->throwException(new Swift_TransportException($e->getMessage(), 0, $e)); |
|
476 | + } |
|
477 | + |
|
478 | + return $response; |
|
479 | + } |
|
480 | + |
|
481 | + /** Send an email to the given recipients from the given reverse path */ |
|
482 | + private function doMailTransaction($message, $reversePath, array $recipients, array &$failedRecipients) |
|
483 | + { |
|
484 | + $sent = 0; |
|
485 | + $this->doMailFromCommand($reversePath); |
|
486 | + foreach ($recipients as $forwardPath) { |
|
487 | + try { |
|
488 | + $this->doRcptToCommand($forwardPath); |
|
489 | + ++$sent; |
|
490 | + } catch (Swift_TransportException $e) { |
|
491 | + $failedRecipients[] = $forwardPath; |
|
492 | + } catch (Swift_AddressEncoderException $e) { |
|
493 | + $failedRecipients[] = $forwardPath; |
|
494 | + } |
|
495 | + } |
|
496 | + |
|
497 | + if (0 != $sent) { |
|
498 | + $sent += \count($failedRecipients); |
|
499 | + $this->doDataCommand($failedRecipients); |
|
500 | + $sent -= \count($failedRecipients); |
|
501 | + |
|
502 | + $this->streamMessage($message); |
|
503 | + } else { |
|
504 | + $this->reset(); |
|
505 | + } |
|
506 | + |
|
507 | + return $sent; |
|
508 | + } |
|
509 | + |
|
510 | + /** Send a message to the given To: recipients */ |
|
511 | + private function sendTo(Swift_Mime_SimpleMessage $message, $reversePath, array $to, array &$failedRecipients) |
|
512 | + { |
|
513 | + if (empty($to)) { |
|
514 | + return 0; |
|
515 | + } |
|
516 | + |
|
517 | + return $this->doMailTransaction($message, $reversePath, array_keys($to), |
|
518 | + $failedRecipients); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Destructor. |
|
523 | + */ |
|
524 | + public function __destruct() |
|
525 | + { |
|
526 | + try { |
|
527 | + $this->stop(); |
|
528 | + } catch (Exception $e) { |
|
529 | + } |
|
530 | + } |
|
531 | + |
|
532 | + public function __sleep() |
|
533 | + { |
|
534 | + throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
535 | + } |
|
536 | + |
|
537 | + public function __wakeup() |
|
538 | + { |
|
539 | + throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
540 | + } |
|
541 | 541 | } |
@@ -15,178 +15,178 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_Transport_LoadBalancedTransport implements Swift_Transport |
17 | 17 | { |
18 | - /** |
|
19 | - * Transports which are deemed useless. |
|
20 | - * |
|
21 | - * @var Swift_Transport[] |
|
22 | - */ |
|
23 | - private $deadTransports = []; |
|
24 | - |
|
25 | - /** |
|
26 | - * The Transports which are used in rotation. |
|
27 | - * |
|
28 | - * @var Swift_Transport[] |
|
29 | - */ |
|
30 | - protected $transports = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * The Transport used in the last successful send operation. |
|
34 | - * |
|
35 | - * @var Swift_Transport |
|
36 | - */ |
|
37 | - protected $lastUsedTransport = null; |
|
38 | - |
|
39 | - // needed as __construct is called from elsewhere explicitly |
|
40 | - public function __construct() |
|
41 | - { |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Set $transports to delegate to. |
|
46 | - * |
|
47 | - * @param Swift_Transport[] $transports |
|
48 | - */ |
|
49 | - public function setTransports(array $transports) |
|
50 | - { |
|
51 | - $this->transports = $transports; |
|
52 | - $this->deadTransports = []; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Get $transports to delegate to. |
|
57 | - * |
|
58 | - * @return Swift_Transport[] |
|
59 | - */ |
|
60 | - public function getTransports() |
|
61 | - { |
|
62 | - return array_merge($this->transports, $this->deadTransports); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Get the Transport used in the last successful send operation. |
|
67 | - * |
|
68 | - * @return Swift_Transport |
|
69 | - */ |
|
70 | - public function getLastUsedTransport() |
|
71 | - { |
|
72 | - return $this->lastUsedTransport; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Test if this Transport mechanism has started. |
|
77 | - * |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function isStarted() |
|
81 | - { |
|
82 | - return \count($this->transports) > 0; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Start this Transport mechanism. |
|
87 | - */ |
|
88 | - public function start() |
|
89 | - { |
|
90 | - $this->transports = array_merge($this->transports, $this->deadTransports); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Stop this Transport mechanism. |
|
95 | - */ |
|
96 | - public function stop() |
|
97 | - { |
|
98 | - foreach ($this->transports as $transport) { |
|
99 | - $transport->stop(); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * {@inheritdoc} |
|
105 | - */ |
|
106 | - public function ping() |
|
107 | - { |
|
108 | - foreach ($this->transports as $transport) { |
|
109 | - if (!$transport->ping()) { |
|
110 | - $this->killCurrentTransport(); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - return \count($this->transports) > 0; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Send the given Message. |
|
119 | - * |
|
120 | - * Recipient/sender data will be retrieved from the Message API. |
|
121 | - * The return value is the number of recipients who were accepted for delivery. |
|
122 | - * |
|
123 | - * @param string[] $failedRecipients An array of failures by-reference |
|
124 | - * |
|
125 | - * @return int |
|
126 | - */ |
|
127 | - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
128 | - { |
|
129 | - $maxTransports = \count($this->transports); |
|
130 | - $sent = 0; |
|
131 | - $this->lastUsedTransport = null; |
|
132 | - |
|
133 | - for ($i = 0; $i < $maxTransports |
|
134 | - && $transport = $this->getNextTransport(); ++$i) { |
|
135 | - try { |
|
136 | - if (!$transport->isStarted()) { |
|
137 | - $transport->start(); |
|
138 | - } |
|
139 | - if ($sent = $transport->send($message, $failedRecipients)) { |
|
140 | - $this->lastUsedTransport = $transport; |
|
141 | - break; |
|
142 | - } |
|
143 | - } catch (Swift_TransportException $e) { |
|
144 | - $this->killCurrentTransport(); |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - if (0 == \count($this->transports)) { |
|
149 | - throw new Swift_TransportException('All Transports in LoadBalancedTransport failed, or no Transports available'); |
|
150 | - } |
|
151 | - |
|
152 | - return $sent; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Register a plugin. |
|
157 | - */ |
|
158 | - public function registerPlugin(Swift_Events_EventListener $plugin) |
|
159 | - { |
|
160 | - foreach ($this->transports as $transport) { |
|
161 | - $transport->registerPlugin($plugin); |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Rotates the transport list around and returns the first instance. |
|
167 | - * |
|
168 | - * @return Swift_Transport |
|
169 | - */ |
|
170 | - protected function getNextTransport() |
|
171 | - { |
|
172 | - if ($next = array_shift($this->transports)) { |
|
173 | - $this->transports[] = $next; |
|
174 | - } |
|
175 | - |
|
176 | - return $next; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Tag the currently used (top of stack) transport as dead/useless. |
|
181 | - */ |
|
182 | - protected function killCurrentTransport() |
|
183 | - { |
|
184 | - if ($transport = array_pop($this->transports)) { |
|
185 | - try { |
|
186 | - $transport->stop(); |
|
187 | - } catch (Exception $e) { |
|
188 | - } |
|
189 | - $this->deadTransports[] = $transport; |
|
190 | - } |
|
191 | - } |
|
18 | + /** |
|
19 | + * Transports which are deemed useless. |
|
20 | + * |
|
21 | + * @var Swift_Transport[] |
|
22 | + */ |
|
23 | + private $deadTransports = []; |
|
24 | + |
|
25 | + /** |
|
26 | + * The Transports which are used in rotation. |
|
27 | + * |
|
28 | + * @var Swift_Transport[] |
|
29 | + */ |
|
30 | + protected $transports = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * The Transport used in the last successful send operation. |
|
34 | + * |
|
35 | + * @var Swift_Transport |
|
36 | + */ |
|
37 | + protected $lastUsedTransport = null; |
|
38 | + |
|
39 | + // needed as __construct is called from elsewhere explicitly |
|
40 | + public function __construct() |
|
41 | + { |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Set $transports to delegate to. |
|
46 | + * |
|
47 | + * @param Swift_Transport[] $transports |
|
48 | + */ |
|
49 | + public function setTransports(array $transports) |
|
50 | + { |
|
51 | + $this->transports = $transports; |
|
52 | + $this->deadTransports = []; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Get $transports to delegate to. |
|
57 | + * |
|
58 | + * @return Swift_Transport[] |
|
59 | + */ |
|
60 | + public function getTransports() |
|
61 | + { |
|
62 | + return array_merge($this->transports, $this->deadTransports); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Get the Transport used in the last successful send operation. |
|
67 | + * |
|
68 | + * @return Swift_Transport |
|
69 | + */ |
|
70 | + public function getLastUsedTransport() |
|
71 | + { |
|
72 | + return $this->lastUsedTransport; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Test if this Transport mechanism has started. |
|
77 | + * |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function isStarted() |
|
81 | + { |
|
82 | + return \count($this->transports) > 0; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Start this Transport mechanism. |
|
87 | + */ |
|
88 | + public function start() |
|
89 | + { |
|
90 | + $this->transports = array_merge($this->transports, $this->deadTransports); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Stop this Transport mechanism. |
|
95 | + */ |
|
96 | + public function stop() |
|
97 | + { |
|
98 | + foreach ($this->transports as $transport) { |
|
99 | + $transport->stop(); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * {@inheritdoc} |
|
105 | + */ |
|
106 | + public function ping() |
|
107 | + { |
|
108 | + foreach ($this->transports as $transport) { |
|
109 | + if (!$transport->ping()) { |
|
110 | + $this->killCurrentTransport(); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + return \count($this->transports) > 0; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Send the given Message. |
|
119 | + * |
|
120 | + * Recipient/sender data will be retrieved from the Message API. |
|
121 | + * The return value is the number of recipients who were accepted for delivery. |
|
122 | + * |
|
123 | + * @param string[] $failedRecipients An array of failures by-reference |
|
124 | + * |
|
125 | + * @return int |
|
126 | + */ |
|
127 | + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
|
128 | + { |
|
129 | + $maxTransports = \count($this->transports); |
|
130 | + $sent = 0; |
|
131 | + $this->lastUsedTransport = null; |
|
132 | + |
|
133 | + for ($i = 0; $i < $maxTransports |
|
134 | + && $transport = $this->getNextTransport(); ++$i) { |
|
135 | + try { |
|
136 | + if (!$transport->isStarted()) { |
|
137 | + $transport->start(); |
|
138 | + } |
|
139 | + if ($sent = $transport->send($message, $failedRecipients)) { |
|
140 | + $this->lastUsedTransport = $transport; |
|
141 | + break; |
|
142 | + } |
|
143 | + } catch (Swift_TransportException $e) { |
|
144 | + $this->killCurrentTransport(); |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + if (0 == \count($this->transports)) { |
|
149 | + throw new Swift_TransportException('All Transports in LoadBalancedTransport failed, or no Transports available'); |
|
150 | + } |
|
151 | + |
|
152 | + return $sent; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Register a plugin. |
|
157 | + */ |
|
158 | + public function registerPlugin(Swift_Events_EventListener $plugin) |
|
159 | + { |
|
160 | + foreach ($this->transports as $transport) { |
|
161 | + $transport->registerPlugin($plugin); |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Rotates the transport list around and returns the first instance. |
|
167 | + * |
|
168 | + * @return Swift_Transport |
|
169 | + */ |
|
170 | + protected function getNextTransport() |
|
171 | + { |
|
172 | + if ($next = array_shift($this->transports)) { |
|
173 | + $this->transports[] = $next; |
|
174 | + } |
|
175 | + |
|
176 | + return $next; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Tag the currently used (top of stack) transport as dead/useless. |
|
181 | + */ |
|
182 | + protected function killCurrentTransport() |
|
183 | + { |
|
184 | + if ($transport = array_pop($this->transports)) { |
|
185 | + try { |
|
186 | + $transport->stop(); |
|
187 | + } catch (Exception $e) { |
|
188 | + } |
|
189 | + $this->deadTransports[] = $transport; |
|
190 | + } |
|
191 | + } |
|
192 | 192 | } |