/** * @param \RainLoop\Plugins\Manager $oPlugins * @param \MailSo\Smtp\SmtpClient|null $oSmtpClient * @param \RainLoop\Application $oConfig * @param bool $bUsePhpMail = false * * @return bool */ public function OutConnectAndLoginHelper($oPlugins, $oSmtpClient, $oConfig, &$bUsePhpMail = false) { $bLogin = false; $aSmtpCredentials = array('UseConnect' => true, 'UseAuth' => $this->DomainOutAuth(), 'UsePhpMail' => $bUsePhpMail, 'Ehlo' => \MailSo\Smtp\SmtpClient::EhloHelper(), 'Host' => $this->DomainOutHost(), 'Port' => $this->DomainOutPort(), 'Secure' => $this->DomainOutSecure(), 'Login' => $this->OutLogin(), 'Password' => $this->Password(), 'ProxyAuthUser' => $this->ProxyAuthUser(), 'ProxyAuthPassword' => $this->ProxyAuthPassword(), 'VerifySsl' => !!$oConfig->Get('ssl', 'verify_certificate', false), 'AllowSelfSigned' => !!$oConfig->Get('ssl', 'allow_self_signed', true)); $oPlugins->RunHook('filter.smtp-credentials', array($this, &$aSmtpCredentials)); $bUsePhpMail = $aSmtpCredentials['UsePhpMail']; $oPlugins->RunHook('event.smtp-pre-connect', array($this, $aSmtpCredentials['UseConnect'], $aSmtpCredentials)); if ($aSmtpCredentials['UseConnect'] && !$aSmtpCredentials['UsePhpMail'] && $oSmtpClient) { $oSmtpClient->Connect($aSmtpCredentials['Host'], $aSmtpCredentials['Port'], $aSmtpCredentials['Ehlo'], $aSmtpCredentials['Secure'], $aSmtpCredentials['VerifySsl'], $aSmtpCredentials['AllowSelfSigned']); } $oPlugins->RunHook('event.smtp-post-connect', array($this, $aSmtpCredentials['UseConnect'], $aSmtpCredentials)); $oPlugins->RunHook('event.smtp-pre-login', array($this, $aSmtpCredentials['UseAuth'], $aSmtpCredentials)); if ($aSmtpCredentials['UseAuth'] && !$aSmtpCredentials['UsePhpMail'] && $oSmtpClient) { $iGatLen = \strlen(APP_GOOGLE_ACCESS_TOKEN_PREFIX); $sPassword = $aSmtpCredentials['Password']; if (APP_GOOGLE_ACCESS_TOKEN_PREFIX === \substr($sPassword, 0, $iGatLen)) { $oSmtpClient->LoginWithXOauth2(\base64_encode('user='******'Login'] . "" . 'auth=Bearer ' . \substr($sPassword, $iGatLen) . "")); } else { $oSmtpClient->Login($aSmtpCredentials['Login'], $aSmtpCredentials['Password']); } $bLogin = true; } $oPlugins->RunHook('event.smtp-post-login', array($this, $aSmtpCredentials['UseAuth'], $bLogin, $aSmtpCredentials)); return $bLogin; }
/** * @return array */ public function DoAdminDomainTest() { $this->IsAdminLoggined(); $bImapResult = false; $sImapErrorDesc = ''; $bSmtpResult = false; $sSmtpErrorDesc = ''; $bSieveResult = false; $sSieveErrorDesc = ''; $iImapTime = 0; $iSmtpTime = 0; $iSieveTime = 0; $iConnectionTimeout = 5; $oDomain = $this->DomainProvider()->LoadOrCreateNewFromAction($this, 'domain-test-connection.de'); if ($oDomain) { try { $oImapClient = \MailSo\Imap\ImapClient::NewInstance()->SetLogger($this->Logger()); $oImapClient->SetTimeOuts($iConnectionTimeout); $iTime = \microtime(true); $oImapClient->Connect($oDomain->IncHost(), $oDomain->IncPort(), $oDomain->IncSecure(), !!$this->Config()->Get('ssl', 'verify_certificate', false), !!$this->Config()->Get('ssl', 'allow_self_signed', true)); $iImapTime = \microtime(true) - $iTime; $oImapClient->Disconnect(); $bImapResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sImapErrorDesc = $oException->getSocketMessage(); if (empty($sImapErrorDesc)) { $sImapErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sImapErrorDesc = $oException->getMessage(); } if ($oDomain->OutUsePhpMail()) { $bSmtpResult = \MailSo\Base\Utils::FunctionExistsAndEnabled('mail'); if (!$bSmtpResult) { $sSmtpErrorDesc = 'PHP: mail() function is undefined'; } } else { try { $oSmtpClient = \MailSo\Smtp\SmtpClient::NewInstance()->SetLogger($this->Logger()); $oSmtpClient->SetTimeOuts($iConnectionTimeout); $iTime = \microtime(true); $oSmtpClient->Connect($oDomain->OutHost(), $oDomain->OutPort(), \MailSo\Smtp\SmtpClient::EhloHelper(), $oDomain->OutSecure(), !!$this->Config()->Get('ssl', 'verify_certificate', false), !!$this->Config()->Get('ssl', 'allow_self_signed', true)); $iSmtpTime = \microtime(true) - $iTime; $oSmtpClient->Disconnect(); $bSmtpResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSmtpErrorDesc = $oException->getSocketMessage(); if (empty($sSmtpErrorDesc)) { $sSmtpErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSmtpErrorDesc = $oException->getMessage(); } } if ($oDomain->UseSieve()) { try { $oSieveClient = \MailSo\Sieve\ManageSieveClient::NewInstance()->SetLogger($this->Logger()); $oSieveClient->SetTimeOuts($iConnectionTimeout); $iTime = \microtime(true); $oSieveClient->Connect($oDomain->SieveHost(), $oDomain->SievePort(), $oDomain->SieveSecure(), !!$this->Config()->Get('ssl', 'verify_certificate', false), !!$this->Config()->Get('ssl', 'allow_self_signed', true)); $iSieveTime = \microtime(true) - $iTime; $oSieveClient->Disconnect(); $bSieveResult = true; } catch (\MailSo\Net\Exceptions\SocketCanNotConnectToHostException $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSieveErrorDesc = $oException->getSocketMessage(); if (empty($sSieveErrorDesc)) { $sSieveErrorDesc = $oException->getMessage(); } } catch (\Exception $oException) { $this->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR); $sSieveErrorDesc = $oException->getMessage(); } } else { $bSieveResult = true; } } return $this->DefaultResponse(__FUNCTION__, array('Imap' => $bImapResult ? true : $sImapErrorDesc, 'Smtp' => $bSmtpResult ? true : $sSmtpErrorDesc, 'Sieve' => $bSieveResult ? true : $sSieveErrorDesc)); }
private function smtpSendMessage($oAccount, $oMessage, $rMessageStream, $bAddHiddenRcpt = true) { $oRcpt = $oMessage->GetRcpt(); if ($oRcpt && 0 < $oRcpt->Count()) { $this->Plugins()->RunHook('filter.message-rcpt', array($oAccount, &$oRcpt)); try { $oSmtpClient = \MailSo\Smtp\SmtpClient::NewInstance()->SetLogger($this->Logger()); $oFrom = $oMessage->GetFrom(); $sFrom = $oFrom instanceof \MailSo\Mime\Email ? $oFrom->GetEmail() : ''; $aSmtpCredentials = array('Ehlo' => \MailSo\Smtp\SmtpClient::EhloHelper(), 'Host' => $oAccount->Domain()->OutHost(\MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email())), 'Port' => $oAccount->Domain()->OutPort(), 'Secure' => $oAccount->Domain()->OutSecure(), 'UseAuth' => $oAccount->Domain()->OutAuth(), 'From' => empty($sFrom) ? $oAccount->Email() : $sFrom, 'Login' => $oAccount->OutLogin(), 'Password' => $oAccount->Password(), 'HiddenRcpt' => array()); $this->Plugins()->RunHook('filter.smtp-credentials', array($oAccount, &$aSmtpCredentials)); if (!$bAddHiddenRcpt) { $aSmtpCredentials['HiddenRcpt'] = array(); } $bHookConnect = $bHookAuth = $bHookFrom = $bHookFrom = $bHookTo = $bHookData = $bHookLogoutAndDisconnect = false; $this->Plugins()->RunHook('filter.smtp-connect', array($oAccount, $aSmtpCredentials, &$oSmtpClient, $oMessage, &$oRcpt, &$bHookConnect, &$bHookAuth, &$bHookFrom, &$bHookTo, &$bHookData, &$bHookLogoutAndDisconnect)); if (!$bHookConnect) { $oSmtpClient->Connect($aSmtpCredentials['Host'], $aSmtpCredentials['Port'], $aSmtpCredentials['Ehlo'], $aSmtpCredentials['Secure']); } if (!$bHookAuth) { if ($aSmtpCredentials['UseAuth']) { $oSmtpClient->Login($aSmtpCredentials['Login'], $aSmtpCredentials['Password']); } } if (!$bHookFrom) { $oSmtpClient->MailFrom($aSmtpCredentials['From']); } if (!$bHookTo) { $aRcpt =& $oRcpt->GetAsArray(); foreach ($aRcpt as $oEmail) { $oSmtpClient->Rcpt($oEmail->GetEmail()); } if (isset($aSmtpCredentials['HiddenRcpt']) && is_array($aSmtpCredentials['HiddenRcpt'])) { foreach ($aSmtpCredentials['HiddenRcpt'] as $sEmail) { if (\preg_match('/^[^@\\s]+@[^@\\s]+$/', $sEmail)) { $oSmtpClient->Rcpt($sEmail); } } } } if (!$bHookData) { $oSmtpClient->DataWithStream($rMessageStream); } if (!$bHookLogoutAndDisconnect) { $oSmtpClient->LogoutAndDisconnect(); } } catch (\MailSo\Net\Exceptions\ConnectionException $oException) { if ($this->Config()->Get('labs', 'smtp_show_server_errors')) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ClientViewError, $oException); } else { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ConnectionError, $oException); } } catch (\MailSo\Smtp\Exceptions\LoginException $oException) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::AuthError, $oException); } catch (\Exception $oException) { if ($this->Config()->Get('labs', 'smtp_show_server_errors')) { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::ClientViewError, $oException); } else { throw $oException; } } } else { throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::InvalidRecipients); } }
/** * Sends message out. * * @param CAccount $oAccount Account object. * @param \MailSo\Mime\Message $oMessage Message to be sent out. * @param CFetcher $oFetcher = null. Fetcher object which may override sending settings. * @param string $sSentFolder = ''. Name of Sent folder. * @param string $sDraftFolder = ''. Name of Sent folder. * @param string $sDraftUid = ''. Last UID value of the message saved in Drafts folder. * * @return array|bool * * @throws CApiInvalidArgumentException */ public function sendMessage($oAccount, $oMessage, $oFetcher = null, $sSentFolder = '', $sDraftFolder = '', $sDraftUid = '') { if (!$oAccount || !$oMessage) { throw new CApiInvalidArgumentException(); } $oImapClient =& $this->_getImapClient($oAccount); $rMessageStream = \MailSo\Base\ResourceRegistry::CreateMemoryResource(); $iMessageStreamSize = \MailSo\Base\Utils::MultipleStreamWriter($oMessage->ToStream(true), array($rMessageStream), 8192, true, true, true); $mResult = false; if (false !== $iMessageStreamSize && is_resource($rMessageStream)) { $oRcpt = $oMessage->GetRcpt(); if ($oRcpt && 0 < $oRcpt->Count()) { $sRcptEmail = ''; try { $iConnectTimeOut = CApi::GetConf('socket.connect-timeout', 5); $iSocketTimeOut = CApi::GetConf('socket.get-timeout', 5); $bVerifySsl = !!CApi::GetConf('socket.verify-ssl', false); CApi::Plugin()->RunHook('webmail-smtp-update-socket-timeouts', array(&$iConnectTimeOut, &$iSocketTimeOut)); $oSmtpClient = \MailSo\Smtp\SmtpClient::NewInstance(); $oSmtpClient->SetTimeOuts($iConnectTimeOut, $iSocketTimeOut); $oLogger = $oImapClient->Logger(); if ($oLogger) { $oSmtpClient->SetLogger($oLogger); } $iSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::AUTO_DETECT; if ($oFetcher) { $iSecure = $oFetcher->OutgoingMailSecurity; } else { if ($oAccount->OutgoingMailUseSSL) { $iSecure = \MailSo\Net\Enumerations\ConnectionSecurityType::SSL; } } $sOutgoingMailLogin = ''; if ($oFetcher) { $sOutgoingMailLogin = $oFetcher->IncomingMailLogin; } if (0 === strlen($sOutgoingMailLogin)) { $sOutgoingMailLogin = $oAccount->OutgoingMailLogin; $sOutgoingMailLogin = 0 < strlen($sOutgoingMailLogin) ? $sOutgoingMailLogin : $oAccount->IncomingMailLogin; } $sOutgoingMailPassword = ''; if ($oFetcher) { $sOutgoingMailPassword = $oFetcher->IncomingMailPassword; } if (0 === strlen($sOutgoingMailPassword)) { $sOutgoingMailPassword = $oAccount->OutgoingMailPassword; $sOutgoingMailPassword = 0 < strlen($sOutgoingMailPassword) ? $sOutgoingMailPassword : $oAccount->IncomingMailPassword; } $sEhlo = \MailSo\Smtp\SmtpClient::EhloHelper(); CApi::Plugin()->RunHook('api-smtp-send-ehlo', array($oAccount, &$sEhlo)); if ($oFetcher) { $oSmtpClient->Connect($oFetcher->OutgoingMailServer, $oFetcher->OutgoingMailPort, $sEhlo, $iSecure, $bVerifySsl); } else { $oSmtpClient->Connect($oAccount->OutgoingMailServer, $oAccount->OutgoingMailPort, $sEhlo, $iSecure, $bVerifySsl); } if ($oFetcher && $oFetcher->OutgoingMailAuth || !$oFetcher && $oAccount->OutgoingMailAuth) { $oSmtpClient->Login($sOutgoingMailLogin, $sOutgoingMailPassword); } $oSmtpClient->MailFrom($oFetcher ? $oFetcher->Email : $oAccount->Email, (string) $iMessageStreamSize); $aRcpt =& $oRcpt->GetAsArray(); CApi::Plugin()->RunHook('api-smtp-send-rcpt', array($oAccount, &$aRcpt)); foreach ($aRcpt as $oEmail) { $sRcptEmail = $oEmail->GetEmail(); $oSmtpClient->Rcpt($sRcptEmail); } $oSmtpClient->DataWithStream($rMessageStream); $oSmtpClient->LogoutAndDisconnect(); } catch (\MailSo\Net\Exceptions\ConnectionException $oException) { throw new \CApiManagerException(Errs::Mail_AccountConnectToMailServerFailed, $oException); } catch (\MailSo\Smtp\Exceptions\LoginException $oException) { throw new \CApiManagerException(Errs::Mail_AccountLoginFailed, $oException); } catch (\MailSo\Smtp\Exceptions\NegativeResponseException $oException) { throw new \CApiManagerException(Errs::Mail_CannotSendMessage, $oException); } catch (\MailSo\Smtp\Exceptions\MailboxUnavailableException $oException) { throw new \CApiManagerException(Errs::Mail_MailboxUnavailable, $oException, array(), array('Mailbox' => $sRcptEmail)); } if (0 < strlen($sSentFolder)) { try { if (!$oMessage->GetBcc()) { if (is_resource($rMessageStream)) { rewind($rMessageStream); } $oImapClient->MessageAppendStream($sSentFolder, $rMessageStream, $iMessageStreamSize, array(\MailSo\Imap\Enumerations\MessageFlag::SEEN)); } else { $rAppendMessageStream = \MailSo\Base\ResourceRegistry::CreateMemoryResource(); $iAppendMessageStreamSize = \MailSo\Base\Utils::MultipleStreamWriter($oMessage->ToStream(), array($rAppendMessageStream), 8192, true, true, true); $oImapClient->MessageAppendStream($sSentFolder, $rAppendMessageStream, $iAppendMessageStreamSize, array(\MailSo\Imap\Enumerations\MessageFlag::SEEN)); if (is_resource($rAppendMessageStream)) { @fclose($rAppendMessageStream); } } } catch (\Exception $oException) { throw new \CApiManagerException(Errs::Mail_CannotSaveMessageInSentItems, $oException); } if (is_resource($rMessageStream)) { @fclose($rMessageStream); } } if (0 < strlen($sDraftFolder) && 0 < strlen($sDraftUid)) { try { $this->deleteMessage($oAccount, $sDraftFolder, array($sDraftUid)); } catch (\Exception $oException) { } } $mResult = true; } else { throw new \CApiManagerException(Errs::Mail_InvalidRecipients); } } return $mResult; }
/** * @param \RainLoop\Plugins\Manager $oPlugins * @param \MailSo\Smtp\SmtpClient|null $oSmtpClient * @param \RainLoop\Application $oConfig * @param bool $bUsePhpMail = false * * @return bool */ public function OutConnectAndLoginHelper($oPlugins, $oSmtpClient, $oConfig, &$bUsePhpMail = false) { $bLogin = false; $aSmtpCredentials = array('UseConnect' => true, 'UseAuth' => $this->DomainOutAuth(), 'UsePhpMail' => $bUsePhpMail, 'Ehlo' => \MailSo\Smtp\SmtpClient::EhloHelper(), 'Host' => $this->DomainOutHost(), 'Port' => $this->DomainOutPort(), 'Secure' => $this->DomainOutSecure(), 'Login' => $this->OutLogin(), 'Password' => $this->Password(), 'ProxyAuthUser' => $this->ProxyAuthUser(), 'ProxyAuthPassword' => $this->ProxyAuthPassword(), 'VerifySsl' => !!$oConfig->Get('ssl', 'verify_certificate', false), 'AllowSelfSigned' => !!$oConfig->Get('ssl', 'allow_self_signed', true)); $oPlugins->RunHook('filter.smtp-credentials', array($this, &$aSmtpCredentials)); $bUsePhpMail = $aSmtpCredentials['UsePhpMail']; $oPlugins->RunHook('event.smtp-pre-connect', array($this, $aSmtpCredentials['UseConnect'], $aSmtpCredentials)); if ($aSmtpCredentials['UseConnect'] && !$aSmtpCredentials['UsePhpMail'] && $oSmtpClient) { $oSmtpClient->Connect($aSmtpCredentials['Host'], $aSmtpCredentials['Port'], $aSmtpCredentials['Ehlo'], $aSmtpCredentials['Secure'], $aSmtpCredentials['VerifySsl'], $aSmtpCredentials['AllowSelfSigned']); } $oPlugins->RunHook('event.smtp-post-connect', array($this, $aSmtpCredentials['UseConnect'], $aSmtpCredentials)); $oPlugins->RunHook('event.smtp-pre-login', array($this, $aSmtpCredentials['UseAuth'], $aSmtpCredentials)); if ($aSmtpCredentials['UseAuth'] && !$aSmtpCredentials['UsePhpMail'] && $oSmtpClient) { $oSmtpClient->Login($aSmtpCredentials['Login'], $aSmtpCredentials['Password']); $bLogin = true; } $oPlugins->RunHook('event.smtp-post-login', array($this, $aSmtpCredentials['UseAuth'], $bLogin, $aSmtpCredentials)); return $bLogin; }