N°7082 Ability to forcefully send e-mails asynchronously (#501)

Will be usefull in use cases like sending mass emails in customer-survey
This commit is contained in:
jbostoen
2023-12-21 11:15:08 +01:00
committed by GitHub
parent 34f0053db7
commit 031c90e797
2 changed files with 48 additions and 8 deletions

View File

@@ -304,22 +304,53 @@ class EMailLaminas extends Email
return $aImagesParts;
}
public function Send(&$aIssues, $bForceSynchronous = false, $oLog = null)
/**
* Sends an e-mail.
*
* @param string[] $aIssues Array to add any potentially encountered issues to.
* @param int|bool $iSyncAsync Specify whether the e-mail will be sent per default configuration, or whether there will be a forced (a)synchronous sending. One of Email::ENUM_SEND_* constants. To support legacy, it also allows a boolean (true = send synchronous).
* @param Object|null $oLog Log
*
* @details By default, the send method will respect the preference to send e-mails in an (a)synchronous way as defined in the iTop configuration by the administrator.
* In some use cases, it may be necessary to override this behavior. For example, for some tests it may be best if e-mails are always sent instantly (synchronous).
* Asynchronous may be preferred when sending a lot of bulk e-mails at once, to avoid hitting rate limits of e-mail providers (e.g. customer survey extension).
*
* @since 3.2.0 Previously, $iSyncAsync was a boolean ($bForceSynchronous) and this method only allowed to forcefully send e-mails synchronously even when the default was asynchronous.
*
* @return
*/
public function Send(&$aIssues, $iSyncAsync = Email::ENUM_SEND_DEFAULT, $oLog = null)
{
//select a default sender if none is provided.
if (empty($this->m_aData['from']['address']) && !empty($this->m_aData['to'])) {
$this->SetRecipientFrom($this->m_aData['to']);
}
if ($bForceSynchronous) {
// In previous iTop versions, $iSyncAsync was $bForceSynchronous. To retain backward compatibility, this check is in place.
if($iSyncAsync === true) {
// This legacy mode forces synchronous sending, ignoring whatever default was configured.
return $this->SendSynchronous($aIssues, $oLog);
} else {
$oConfig = $this->LoadConfig();
$bConfigASYNC = $oConfig->Get('email_asynchronous');
if ($bConfigASYNC) {
return $this->SendAsynchronous($aIssues, $oLog);
} else {
return $this->SendSynchronous($aIssues, $oLog);
switch($iSyncAsync) {
case Email::ENUM_SEND_FORCE_SYNCHRONOUS:
return $this->SendSynchronous($aIssues, $oLog);
case Email::ENUM_SEND_FORCE_ASYNCHRONOUS:
return $this->SendAsynchronous($aIssues, $oLog);
case Email::ENUM_SEND_DEFAULT:
default:
// Default behavior.
$oConfig = $this->LoadConfig();
$bConfigASYNC = $oConfig->Get('email_asynchronous');
if($bConfigASYNC) {
return $this->SendAsynchronous($aIssues, $oLog);
} else {
return $this->SendSynchronous($aIssues, $oLog);
}
}
}
}