/** * Saves an User Account into the database * @param MailAccount $account * @return MailAccount */ public function save(MailAccount $account) { if (is_null($account->getId())) { return $this->insert($account); } else { $this->update($account); return $account; } }
/** * @param $email * @param $password * @param $name * @param $host * @param $port * @param string|null $encryptionProtocol * @param $user * @return MailAccount */ public function connect($email, $password, $name, $host, $port, $encryptionProtocol, $user) { $account = new MailAccount(); $account->setUserId($this->userId); $account->setName($name); $account->setEmail($email); $account->setInboundHost($host); $account->setInboundPort($port); $account->setInboundSslMode($encryptionProtocol); $account->setInboundUser($user); $password = $this->crypto->encrypt($password); $account->setInboundPassword($password); $a = new Account($account); $a->getImapConnection(); $this->logger->info("Test-Account-Successful: {$this->userId}, {$host}, {$port}, {$user}, {$encryptionProtocol}"); return $account; }
/** * @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; }
/** * Initialize Mapper */ public function setup() { $db = \OC::$server->getDatabaseConnection(); $this->db = new Db($db); $this->mapper = new MailAccountMapper($this->db); $this->account = new MailAccount(); $this->account->setName('Peter Parker'); $this->account->setInboundHost('mail.marvel.com'); $this->account->setInboundPort(159); $this->account->setInboundUser('spiderman'); $this->account->setInboundPassword('xxxxxxxx'); $this->account->setInboundSslMode('tls'); $this->account->setEmail('*****@*****.**'); $this->account->setOutboundHost('smtp.marvel.com'); $this->account->setOutboundPort(458); $this->account->setOutboundUser('spiderman'); $this->account->setOutboundPassword('xxxx'); $this->account->setOutboundSslMode('ssl'); $this->account->setUserId('user12345'); }
/** * @return string|Horde_Mail_Rfc822_List */ public function getEmail() { return $this->account->getEmail(); }
/** * @NoAdminRequired * * @param string $accountName * @param string $emailAddress * @param string $password * @param string $imapHost * @param int $imapPort * @param string $imapSslMode * @param string $imapUser * @param string $imapPassword * @param string $smtpHost * @param int $smtpPort * @param string $smtpSslMode * @param string $smtpUser * @param string $smtpPassword * @param bool $autoDetect * @return JSONResponse */ public function create($accountName, $emailAddress, $password, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $autoDetect) { try { if ($autoDetect) { $this->logger->info('setting up auto detected account'); $newAccount = $this->autoConfig->createAutoDetected($emailAddress, $password, $accountName); } else { $this->logger->info('Setting up manually configured account'); $newAccount = new MailAccount(['accountName' => $accountName, 'emailAddress' => $emailAddress, 'imapHost' => $imapHost, 'imapPort' => $imapPort, 'imapSslMode' => $imapSslMode, 'imapUser' => $imapUser, 'imapPassword' => $imapPassword, 'smtpHost' => $smtpHost, 'smtpPort' => $smtpPort, 'smtpSslMode' => $smtpSslMode, 'smtpUser' => $smtpUser, 'smtpPassword' => $smtpPassword]); $newAccount->setUserId($this->currentUserId); $newAccount->setInboundPassword($this->crypto->encrypt($newAccount->getInboundPassword())); $newAccount->setOutboundPassword($this->crypto->encrypt($newAccount->getOutboundPassword())); $a = new Account($newAccount); $this->logger->debug('Connecting to account {account}', ['account' => $newAccount->getEmail()]); $a->testConnectivity(); } if ($newAccount) { $this->accountService->save($newAccount); $this->logger->debug("account created " . $newAccount->getId()); return new JSONResponse(['data' => ['id' => $newAccount->getId()]], Http::STATUS_CREATED); } } catch (\Exception $ex) { $this->logger->error('Creating account failed: ' . $ex->getMessage()); return new JSONResponse(array('message' => $this->l10n->t('Creating account failed: ') . $ex->getMessage()), HTTP::STATUS_BAD_REQUEST); } $this->logger->info('Auto detect failed'); return new JSONResponse(array('message' => $this->l10n->t('Auto detect failed. Please use manual mode.')), HTTP::STATUS_BAD_REQUEST); }
public static function setUpBeforeClass() { if (false === \getenv('EMAIL_USER')) { throw new \PHPUnit_Framework_SkippedTestError('Please set environment variable EMAIL_USER before running functional tests'); } if (false === \getenv('EMAIL_PASSWORD')) { throw new \PHPUnit_Framework_SkippedTestError('Please set environment variable EMAIL_PASSWORD before running functional tests'); } $user = \getenv('EMAIL_USER'); $password = \getenv('EMAIL_PASSWORD'); $password = \OC::$server->getCrypto()->encrypt($password); $a = new MailAccount(); $a->setId(-1); $a->setName('ownCloudMail'); $a->setInboundHost('imap.gmail.com'); $a->setInboundPort(993); $a->setInboundUser($user); $a->setInboundPassword($password); $a->setInboundSslMode('ssl'); $a->setEmail($user); $a->setOutboundHost('smtp.gmail.com'); $a->setOutboundPort(465); $a->setOutboundUser($user); $a->setOutboundPassword($password); $a->setOutboundSslMode('ssl'); self::$account = new Account($a); self::$account->getImapConnection(); }