/**
  * @param $email
  * @param $host
  * @param $users
  * @param $password
  * @param $name
  * @return null|MailAccount
  */
 public function test($email, $host, $users, $password, $name)
 {
     if (!is_array($users)) {
         $users = [$users];
     }
     $ports = [143, 585, 993];
     $encryptionProtocols = ['ssl', 'tls', null];
     $hostPrefixes = ['', 'imap.'];
     foreach ($hostPrefixes as $hostPrefix) {
         $url = $hostPrefix . $host;
         if (gethostbyname($url) === $url) {
             continue;
         }
         foreach ($ports as $port) {
             if (!$this->canConnect($url, $port)) {
                 continue;
             }
             foreach ($encryptionProtocols as $encryptionProtocol) {
                 foreach ($users as $user) {
                     try {
                         return $this->imapConnector->connect($email, $password, $name, $host, $port, $encryptionProtocol, $user);
                     } catch (\Horde_Imap_Client_Exception $e) {
                         $error = $e->getMessage();
                         $this->logger->info("Test-Account-Failed: {$this->userId}, {$url}, {$port}, {$user}, {$encryptionProtocol} -> {$error}");
                     }
                 }
             }
         }
     }
     return null;
 }
Пример #2
0
 /**
  * @param $email
  * @param $password
  * @param $name
  * @return MailAccount|null
  */
 public function createAutoDetected($email, $password, $name)
 {
     // splitting the email address into user and host part
     // TODO: use horde libs for email address parsing
     list(, $host) = explode("@", $email);
     $ispdb = $this->mozillaIspDb->query($host);
     if (!empty($ispdb)) {
         $account = null;
         if (isset($ispdb['imap'])) {
             foreach ($ispdb['imap'] as $imap) {
                 $host = $imap['hostname'];
                 $port = $imap['port'];
                 $encryptionProtocol = null;
                 if ($imap['socketType'] === 'SSL') {
                     $encryptionProtocol = 'ssl';
                 }
                 if ($imap['socketType'] === 'STARTTLS') {
                     $encryptionProtocol = 'tls';
                 }
                 if ($imap['username'] === '%EMAILADDRESS%') {
                     $user = $email;
                 } elseif ($imap['username'] === '%EMAILLOCALPART%') {
                     list($user, ) = explode("@", $email);
                 } else {
                     $this->logger->info("Unknown username variable: " . $imap['username']);
                     return null;
                 }
                 try {
                     $account = $this->imapConnector->connect($email, $password, $name, $host, $port, $encryptionProtocol, $user);
                     break;
                 } catch (\Horde_Imap_Client_Exception $e) {
                     $error = $e->getMessage();
                     $this->logger->info("Test-Account-Failed: {$this->userId}, {$host}, {$port}, {$user}, {$encryptionProtocol} -> {$error}");
                 }
             }
         }
         if (!is_null($account)) {
             foreach ($ispdb['smtp'] as $smtp) {
                 try {
                     if ($smtp['username'] === '%EMAILADDRESS%') {
                         $user = $email;
                     } elseif ($smtp['username'] === '%EMAILLOCALPART%') {
                         list($user, ) = explode("@", $email);
                     } else {
                         $this->logger->info("Unknown username variable: " . $smtp['username']);
                         return null;
                     }
                     $account->setOutboundHost($smtp['hostname']);
                     $account->setOutboundPort($smtp['port']);
                     $password = $this->crypto->encrypt($password);
                     $account->setOutboundPassword($password);
                     $account->setOutboundUser($user);
                     $account->setOutboundSslMode(strtolower($smtp['socketType']));
                     $a = new Account($account);
                     $smtp = $a->createTransport();
                     if ($smtp instanceof Horde_Mail_Transport_Smtphorde) {
                         $smtp->getSMTPObject();
                     }
                     break;
                 } catch (\PEAR_Exception $ex) {
                     $error = $ex->getMessage();
                     $this->logger->info("Test-Account-Failed(smtp): {$error}");
                 }
             }
             return $account;
         }
     }
     $account = $this->detectImapAndSmtp($email, $password, $name);
     if (!is_null($account)) {
         return $account;
     }
     return null;
 }