protected function WebformScheduledEmailManager::cronSend in Webform 6.x
Same name and namespace in other branches
- 8.5 modules/webform_scheduled_email/src/WebformScheduledEmailManager.php \Drupal\webform_scheduled_email\WebformScheduledEmailManager::cronSend()
Sending schedule emails.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: A webform or webform submission.
string|null $handler_id: A webform handler id.
int $limit: The maximum number of schedule emails to be sent per request.
Return value
array An associative array containing stats.
1 call to WebformScheduledEmailManager::cronSend()
- WebformScheduledEmailManager::cron in modules/
webform_scheduled_email/ src/ WebformScheduledEmailManager.php - Cron task for scheduling and sending emails.
File
- modules/
webform_scheduled_email/ src/ WebformScheduledEmailManager.php, line 616
Class
- WebformScheduledEmailManager
- Defines the webform scheduled email manager.
Namespace
Drupal\webform_scheduled_emailCode
protected function cronSend(EntityInterface $entity = NULL, $handler_id = NULL, $limit = 500) {
$stats = [
WebformScheduledEmailManagerInterface::EMAIL_SENT => 0,
WebformScheduledEmailManagerInterface::EMAIL_NOT_SENT => 0,
WebformScheduledEmailManagerInterface::EMAIL_SKIPPED => 0,
];
if (empty($limit)) {
return $stats;
}
list($webform, $webform_submission, $source_entity) = $this
->getEntities($entity);
// IMPORTANT: Only scheduled emails with state = ::SUBMISSION_SEND will
// be sent.
$query = $this->database
->select('webform_scheduled_email', 'w')
->fields('w', [
'eid',
'sid',
'webform_id',
'entity_type',
'entity_id',
'handler_id',
'send',
])
->condition('w.state', WebformScheduledEmailManagerInterface::SUBMISSION_SEND)
->condition('w.send', time(), '<')
->orderBy('w.send')
->range(0, $limit);
$this
->addQueryConditions($query, $webform, $webform_submission, $source_entity, $handler_id);
// Reset $webform, $webform_submission, and $handler_id so that they
// can be safely used below.
$webform = NULL;
$webform_submission = NULL;
$handler_id = NULL;
// Get pending emails.
$result = $query
->execute();
$eids = [];
foreach ($result as $record) {
$sid = $record->sid;
$webform_id = $record->webform_id;
$handler_id = $record->handler_id;
$eids[] = $record->eid;
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
$webform_submission = $this
->getSubmissionStorage()
->load($sid);
// This should rarely happen and the orphaned record will be deleted.
if (!$webform_submission) {
continue;
}
$webform = $webform_submission
->getWebform();
/** @var \Drupal\webform_scheduled_email\Plugin\WebformHandler\ScheduleEmailWebformHandler $handler */
$handler = $webform_submission
->getWebform()
->getHandler($handler_id);
// This should rarely happen and the orphaned record will be deleted.
if (!$handler) {
continue;
}
if (!$handler
->checkConditions($webform_submission)) {
// Skip sending email.
$action = $this
->t('skipped (conditions not met)');
$operation = 'scheduled email skipped';
$stat = WebformScheduledEmailManagerInterface::EMAIL_SKIPPED;
}
else {
// Switch to submission language.
$original_language = $this->languageManager
->getConfigOverrideLanguage();
$switch_languages = $webform_submission
->language()
->getId() !== $original_language
->getId();
if ($switch_languages) {
$this->languageManager
->setConfigOverrideLanguage($webform_submission
->language());
// Reset the webform, submission, and handler.
$this
->getWebformStorage()
->resetCache([
$webform_id,
]);
$this
->getSubmissionStorage()
->resetCache([
$sid,
]);
// Reload the webform, submission, and handler.
$webform = $this
->getWebformStorage()
->load($webform_id);
$webform_submission = $this
->getSubmissionStorage()
->load($sid);
$handler = $webform
->getHandler($handler_id);
}
// Send (translated) email.
$message = $handler
->getMessage($webform_submission);
$status = $handler
->sendMessage($webform_submission, $message);
// Switch back to original language.
if ($switch_languages) {
$this->languageManager
->setConfigOverrideLanguage($original_language);
// Reset the webform, submission, and handler.
$this
->getWebformStorage()
->resetCache([
$webform_id,
]);
$this
->getSubmissionStorage()
->resetCache([
$sid,
]);
// Reload the webform, submission, and handler.
$webform = $this
->getWebformStorage()
->load($webform_id);
$webform_submission = $this
->getSubmissionStorage()
->load($sid);
$handler = $webform
->getHandler($handler_id);
}
$action = $status ? $this
->t('sent') : $this
->t('not sent');
$operation = $status ? $this
->t('scheduled email sent') : $this
->t('scheduled email not sent');
$stat = $status ? WebformScheduledEmailManagerInterface::EMAIL_SENT : WebformScheduledEmailManagerInterface::EMAIL_NOT_SENT;
}
$channel = $webform
->hasSubmissionLog() ? 'webform_submission' : 'webform';
$context = [
'@title' => $webform_submission
->label(),
'@action' => $action,
'@handler' => $handler
->label(),
'link' => $webform_submission
->toLink($this
->t('View'))
->toString(),
'webform_submission' => $webform_submission,
'handler_id' => $handler_id,
'operation' => $operation,
];
$this
->getLogger($channel)
->notice('Scheduled email @action for @handler handler.', $context);
// Increment stat.
$stats[$stat]++;
}
// Delete sent emails from table.
if ($eids) {
$this->database
->delete('webform_scheduled_email')
->condition('eid', $eids, 'IN')
->execute();
}
return $stats;
}