/**
  * @param MailAccount $account
  * @param $host
  * @param $users
  * @param $password
  * @param bool $withHostPrefix
  * @return bool
  */
 public function test(MailAccount $account, $host, $users, $password, $withHostPrefix = false)
 {
     if (!is_array($users)) {
         $users = [$users];
     }
     // port 25 should be the last one to test
     $ports = [587, 465, 25];
     $protocols = ['ssl', 'tls', null];
     $hostPrefixes = [''];
     if ($withHostPrefix) {
         $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 ($protocols as $protocol) {
                 foreach ($users as $user) {
                     try {
                         $account->setOutboundHost($url);
                         $account->setOutboundPort($port);
                         $account->setOutboundUser($user);
                         $password = $this->crypto->encrypt($password);
                         $account->setOutboundPassword($password);
                         $account->setOutboundSslMode($protocol);
                         $a = new Account($account);
                         $smtp = $a->createTransport();
                         $smtp->getSMTPObject();
                         $this->logger->info("Test-Account-Successful: {$this->userId}, {$url}, {$port}, {$user}, {$protocol}");
                         return true;
                     } catch (\Exception $e) {
                         $error = $e->getMessage();
                         $this->logger->info("Test-Account-Failed: {$this->userId}, {$url}, {$port}, {$user}, {$protocol} -> {$error}");
                     }
                 }
             }
         }
     }
     return false;
 }
Esempio n. 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;
 }