Answers.Body: <p>We had to write a script to send mails stored in Spool table, below is the code we used to send. You can add more filters to the query to restrict based on dates.</p>
  <pre><code>&lt;?php
  class CRM_HoldEmails_BAO_HoldEmails {
  
    /**
     *
     * Process emails from spool.
     *
     */
    public static function processEmailsFromSpool() {
      $mailerBatchLimit = self::getSettingValue('mailerBatchLimit');
      $sql = 'SELECT cms.* FROM `civicrm_mailing_spool` cms';
      if ($mailerBatchLimit) {
        $sql = &quot; LIMIT {$mailerBatchLimit}&quot;;
      }
      $result = CRM_Core_DAO::executeQuery($sql);
      while ($result-&gt;fetch()) {
        list($to, $header, $body) = self::createSendMailParams($result);
        $isSent = self::sendMail($to, $header, $body);
        if ($isSent) {
          self::deleteFromSpool($result-&gt;id, $result-&gt;job_id);
        }
      }
    }
  
    /**
     *
     * Delete from spool once finished.
     *
     */
    public static function deleteFromSpool($spoolId, $jobId) {
      $count = CRM_Core_DAO::singleValueQuery(
        &quot;SELECT count(cm.id) FROM civicrm_mailing cm
          INNER JOIN civicrm_mailing_job cmj ON cmj.mailing_id = cm.id
          INNER JOIN civicrm_mailing_spool cms ON cms.job_id = cmj.id
          WHERE cm.is_completed = 1 AND cm.is_archived =1 AND cms.id = {$spoolId}
      &quot;);
      if ($count == 1) {
        CRM_Core_DAO::executeQuery(
          &quot;DELETE cm.* FROM civicrm_mailing cm
            INNER JOIN civicrm_mailing_job cmj ON cmj.mailing_id = cm.id
            INNER JOIN civicrm_mailing_spool cms ON cms.job_id = cmj.id
            WHERE cm.is_completed = 1 AND cm.is_archived =1 AND cms.id = {$spoolId}&quot;
        );
      }
    }
  
    /**
     *
     * process email params from spool.
     *
     */
    public static function createSendMailParams($result) {
      $to = explode(';', $result-&gt;recipient_email);
      $body = $result-&gt;body;
      $msgHeader = [];
      $headers = explode(&quot;\n&quot;, $result-&gt;headers);
      foreach ($headers as $header) {
        if (strpos($header, ': ') === FALSE) {
          $string .= &quot;\n{$header}&quot;;
        }
        else {
          list($key, $string) = explode(': ', $header, 2);
        }
  
        $msgHeader[$key] = $string;
      }
      return [$to, $msgHeader, $body];
    }
  
    /**
     *
     * Send emails from spool.
     *
     */
    public static function sendMail($to, $headers, $message) {
      $mailer = \Civi::service('pear_mail');
      if (is_object($mailer)) {
        $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
        $result = $mailer-&gt;send($to, $headers, $message);
        if (is_a($result, 'PEAR_Error')) {
          return FALSE;
        }
        CRM_Utils_Hook::postEmailSend($params);
        return TRUE;
      }
    }
  
  }
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-03T20:13:35.237Z
Answers.Text: Thank you so much, Pradeep!
Answers.UserDisplayName: richardsplayground
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-03T09:56:16.800Z
Answers.LastActivityDate: 2024-07-03T09:56:16.800Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>For a little while during troubleshooting we enabled the outbound e-mail option &quot;redirect to database&quot;.  Now that troubleshooting is finished, we have changed the outbound email setting back to SMTP.  We now want those emails to be sent out.</p>
  <p>I see the emails in civicrm_mailing_spool.  I've tried executing scheduled jobs relating to processing mailings and processing scheduled reminders.  But the emails remain in civicrm_mailing_spool.</p>
  <p>Is there a way to trigger these emails to be sent?</p>
  <p>Thank you!</p>
  <p>PS: If it is a matter of moving the data to another database table manually, I know enough SQL to do that.  Hopefully just something I've missed because mucking around in the database is always scary but just wanted to call that out.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-07-02T08:19:13.670Z
Comments.Text: I had written a script to send mail from the spool, so probably you might need to do the same
Comments.UserDisplayName: Pradeep Nayak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-07-02T22:22:18.670Z
Comments.Text: @PradeepNayak if you want to turn your comment into an answer saying it's not possible without custom code, I'll mark as accepted.  Thank you for your help!
Comments.UserDisplayName: richardsplayground
Tags: <civimail><email><settings>
Title: Send emails that were redirected to database?
ThreadText: # Send emails that were redirected to database?
  
  (Asked by: username_0 on 2024-07-01)
  
  For a little while during troubleshooting we enabled the outbound e-mail option "redirect to database". Now that troubleshooting is finished, we have changed the outbound email setting back to SMTP. We now want those emails to be sent out.
  
  I see the emails in civicrm\_mailing\_spool. I've tried executing scheduled jobs relating to processing mailings and processing scheduled reminders. But the emails remain in civicrm\_mailing\_spool.
  
  Is there a way to trigger these emails to be sent?
  
  Thank you!
  
  PS: If it is a matter of moving the data to another database table manually, I know enough SQL to do that. Hopefully just something I've missed because mucking around in the database is always scary but just wanted to call that out.
  
  **Tags:** civimail, email, settings
  
  > **Comments:**
  > **username_1**: I had written a script to send mail from the spool, so probably you might need to do the same  
  > **username_0**: @username_1 if you want to turn your comment into an answer saying it's not possible without custom code, I'll mark as accepted.  Thank you for your help!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-07-03. Score: 3 | Accepted answer
  
  We had to write a script to send mails stored in Spool table, below is the code we used to send. You can add more filters to the query to restrict based on dates.
  
  ```
  <?php
  class CRM_HoldEmails_BAO_HoldEmails {
  
    /**
     *
     * Process emails from spool.
     *
     */
    public static function processEmailsFromSpool() {
      $mailerBatchLimit = self::getSettingValue('mailerBatchLimit');
      $sql = 'SELECT cms.* FROM `civicrm_mailing_spool` cms';
      if ($mailerBatchLimit) {
        $sql = " LIMIT {$mailerBatchLimit}";
      }
      $result = CRM_Core_DAO::executeQuery($sql);
      while ($result->fetch()) {
        list($to, $header, $body) = self::createSendMailParams($result);
        $isSent = self::sendMail($to, $header, $body);
        if ($isSent) {
          self::deleteFromSpool($result->id, $result->job_id);
        }
      }
    }
  
    /**
     *
     * Delete from spool once finished.
     *
     */
    public static function deleteFromSpool($spoolId, $jobId) {
      $count = CRM_Core_DAO::singleValueQuery(
        "SELECT count(cm.id) FROM civicrm_mailing cm
          INNER JOIN civicrm_mailing_job cmj ON cmj.mailing_id = cm.id
          INNER JOIN civicrm_mailing_spool cms ON cms.job_id = cmj.id
          WHERE cm.is_completed = 1 AND cm.is_archived =1 AND cms.id = {$spoolId}
      ");
      if ($count == 1) {
        CRM_Core_DAO::executeQuery(
          "DELETE cm.* FROM civicrm_mailing cm
            INNER JOIN civicrm_mailing_job cmj ON cmj.mailing_id = cm.id
            INNER JOIN civicrm_mailing_spool cms ON cms.job_id = cmj.id
            WHERE cm.is_completed = 1 AND cm.is_archived =1 AND cms.id = {$spoolId}"
        );
      }
    }
  
    /**
     *
     * process email params from spool.
     *
     */
    public static function createSendMailParams($result) {
      $to = explode(';', $result->recipient_email);
      $body = $result->body;
      $msgHeader = [];
      $headers = explode("\n", $result->headers);
      foreach ($headers as $header) {
        if (strpos($header, ': ') === FALSE) {
          $string .= "\n{$header}";
        }
        else {
          list($key, $string) = explode(': ', $header, 2);
        }
  
        $msgHeader[$key] = $string;
      }
      return [$to, $msgHeader, $body];
    }
  
    /**
     *
     * Send emails from spool.
     *
     */
    public static function sendMail($to, $headers, $message) {
      $mailer = \Civi::service('pear_mail');
      if (is_object($mailer)) {
        $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
        $result = $mailer->send($to, $headers, $message);
        if (is_a($result, 'PEAR_Error')) {
          return FALSE;
        }
        CRM_Utils_Hook::postEmailSend($params);
        return TRUE;
      }
    }
  
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you so much, username_1!  
  
  ---
  
  