/** * 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; }