Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
config.php
1<?php
2
4
5use \Bitrix\Main;
6use \Bitrix\Mail;
7
8class Config
9{
10
16 protected $from;
17
23 protected $host;
24
30 protected $port;
31
37 protected $protocol;
38
44 protected $login;
45
51 protected $password;
52
58 protected bool $isOauth = false;
59
60 public function __construct(array $params = null)
61 {
62 if (!empty($params))
63 {
64 foreach ($params as $name => $value)
65 {
66 $setter = sprintf('set%s', $name);
67 if (is_callable(array($this, $setter)))
68 $this->$setter($value);
69 }
70 }
71 }
72
73 public function setFrom($from)
74 {
75 $this->from = $from;
76 return $this;
77 }
78
79 public function setHost($host)
80 {
81 $this->host = $host;
82 return $this;
83 }
84
85 public function setPort($port)
86 {
87 $this->port = (int) $port;
88 return $this;
89 }
90
91 public function setProtocol($protocol)
92 {
93 $this->protocol = $protocol;
94 return $this;
95 }
96
97 public function setLogin($login)
98 {
99 $this->login = $login;
100 return $this;
101 }
102
103 public function setPassword($password)
104 {
105 $this->password = $password;
106 return $this;
107 }
108
116 public function setIsOauth($isOauth): self
117 {
118 $this->isOauth = (bool)$isOauth;
119 return $this;
120 }
121
122 public function getFrom()
123 {
124 return $this->from;
125 }
126
127 public function getHost()
128 {
129 return $this->host;
130 }
131
132 public function getPort()
133 {
134 return $this->port;
135 }
136
137 public function getProtocol()
138 {
139 return $this->protocol;
140 }
141
142 public function getLogin()
143 {
144 return $this->login;
145 }
146
147 public function getPassword()
148 {
149 return $this->password;
150 }
151
157 public function getIsOauth(): bool
158 {
159 return $this->isOauth;
160 }
161
162 public static function canCheck()
163 {
164 return Main\Loader::includeModule('mail') && class_exists('Bitrix\Mail\Smtp');
165 }
166
167 public function check(&$error = null, Main\ErrorCollection &$errors = null)
168 {
169 $error = null;
170 $errors = null;
171
172 if (!$this->canCheck())
173 {
174 return null;
175 }
176
177 if (Main\ModuleManager::isModuleInstalled('bitrix24'))
178 {
179 // Private addresses can't be used in the cloud
180 $ip = Main\Web\IpAddress::createByName($this->host);
181 if ($ip->isPrivate())
182 {
183 $error = 'SMTP server address is invalid';
184 $errors = new Main\ErrorCollection([new Main\Error($error)]);
185 return false;
186 }
187 }
188
189 $client = new Mail\Smtp(
190 $this->host,
191 $this->port,
192 ('smtps' === $this->protocol || ('smtp' !== $this->protocol && 465 === $this->port)),
193 true,
194 $this->login,
195 $this->password
196 );
197 if (method_exists($client, 'setIsOauth'))
198 {
199 $client->setIsOauth($this->getIsOauth());
200 }
201
202 if (!$client->authenticate($error))
203 {
204 $errors = $client->getErrors();
205 return false;
206 }
207
208 return true;
209 }
210
211}
__construct(array $params=null)
Definition config.php:60
check(&$error=null, Main\ErrorCollection &$errors=null)
Definition config.php:167
static isModuleInstalled($moduleName)