|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Christoph Wurst <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Mail |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\Mail\Service\AutoConfig; |
|
23
|
|
|
|
|
24
|
|
|
use OCA\Mail\Db\MailAccount; |
|
25
|
|
|
|
|
26
|
|
|
class SmtpServerDetector { |
|
27
|
|
|
|
|
28
|
|
|
/** @var MxRecord */ |
|
29
|
|
|
private $mxRecord; |
|
30
|
|
|
|
|
31
|
|
|
/** @var SmtpConnectivityTester */ |
|
32
|
|
|
private $smtpConnectivityTester; |
|
33
|
|
|
|
|
34
|
|
|
/** @var bool */ |
|
35
|
|
|
private $testSmtp; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param MxRecord $mxRecord |
|
39
|
|
|
* @param SmtpConnectivityTester $smtpTester |
|
40
|
|
|
* @param bool $testSmtp |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(MxRecord $mxRecord, |
|
43
|
|
|
SmtpConnectivityTester $smtpTester, $testSmtp) { |
|
44
|
|
|
$this->mxRecord = $mxRecord; |
|
45
|
|
|
$this->smtpConnectivityTester = $smtpTester; |
|
46
|
|
|
$this->testSmtp = $testSmtp; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function detect(MailAccount $account, $email, $password) { |
|
50
|
|
|
if ($this->testSmtp === false) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// splitting the email address into user and host part |
|
55
|
|
|
// TODO: use horde libs for email address parsing |
|
56
|
|
|
list($user, $host) = explode("@", $email); |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
* Try to get the mx record for the email address |
|
60
|
|
|
*/ |
|
61
|
|
|
$mxHosts = $this->mxRecord->query($host); |
|
62
|
|
View Code Duplication |
if ($mxHosts) { |
|
|
|
|
|
|
63
|
|
|
foreach ($mxHosts as $mxHost) { |
|
64
|
|
|
$result = $this->smtpConnectivityTester->test($account, $mxHost, |
|
65
|
|
|
[$user, $email], $password); |
|
66
|
|
|
if ($result) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* |
|
73
|
|
|
* IMAP login with full email address as user |
|
74
|
|
|
* works for a lot of providers (e.g. Google Mail) |
|
75
|
|
|
*/ |
|
76
|
|
|
$this->smtpConnectivityTester->test($account, $host, [$user, $email], |
|
77
|
|
|
$password, true); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.