1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Smtp\Capability; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Protocol\ConnectionInterface; |
7
|
|
|
use Genkgo\Mail\Protocol\Smtp\CapabilityInterface; |
8
|
|
|
use Genkgo\Mail\Protocol\Smtp\Session; |
9
|
|
|
|
10
|
|
|
final class EhloCapability implements CapabilityInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $capabilities; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $serverName; |
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
private CONST PROTOCOL_COMMANDS = [ |
25
|
|
|
'MAIL FROM' => true, |
26
|
|
|
'EHLO' => true, |
27
|
|
|
'RCPT TO' => true, |
28
|
|
|
'RSET' => true, |
29
|
|
|
'QUIT' => true, |
30
|
|
|
'HELO' => true, |
31
|
|
|
'DATA' => true, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* EhloCapability constructor. |
36
|
|
|
* @param string $serverName |
37
|
|
|
* @param array $capabilities |
38
|
|
|
*/ |
39
|
|
|
public function __construct(string $serverName, array $capabilities) |
40
|
|
|
{ |
41
|
|
|
$this->serverName = $serverName; |
42
|
|
|
$this->capabilities = $capabilities; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ConnectionInterface $connection |
47
|
|
|
* @param Session $session |
48
|
|
|
* @return Session |
49
|
|
|
*/ |
50
|
|
|
public function manifest(ConnectionInterface $connection, Session $session): Session |
51
|
|
|
{ |
52
|
|
|
$command = $session->getCommand(); |
53
|
|
|
|
54
|
|
|
$advertisements = array_filter( |
55
|
|
|
array_map( |
56
|
|
|
function (CapabilityInterface $capability) { |
57
|
|
|
return $capability->advertise(); |
58
|
|
|
}, |
59
|
|
|
$this->capabilities |
60
|
|
|
), |
61
|
|
|
function (string $advertisement) { |
62
|
|
|
return !isset(self::PROTOCOL_COMMANDS[$advertisement]); |
63
|
|
|
} |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$messages = array_merge( |
67
|
|
|
[$this->serverName . ' Hello ' . substr($command, 5)], |
68
|
|
|
$advertisements |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
foreach ($messages as $message) { |
72
|
|
|
if (next($messages) === false) { |
73
|
|
|
$connection->send('250 ' . $message); |
74
|
|
|
} else { |
75
|
|
|
$connection->send('250-' . $message); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $session->withState(Session::STATE_EHLO); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function advertise(): string |
86
|
|
|
{ |
87
|
|
|
return 'EHLO'; |
88
|
|
|
} |
89
|
|
|
} |