How to create queue for email sending I am using PepiPost mail service and I created custom mail system class for integrating PepiPost service. I can successfully send email from Drupal site. If a connection timeout 504 occurs during email sending process, email is not triggered how can I handle this scenairo. This is the code I used for email sending 

class CustomMailSystem implements MailSystemInterface {

  public function format(array $message) {
    // Join message array.

      $message['body'] = implode("\n\n", $message['body']);
      return $message;
  }


  /**
   * Implement mail method to send mail via Sendgrid.
   *
   * @param array $message
   * @return bool
   */
  public function mail(array $message) {
    $email = new Email();

    $data = array(
      'api_key'   =&gt;  '****************',
      'recipients'    =&gt;  $to
      'email_details' =&gt; array(
        'from'          =&gt;  $from,
        'subject'       =&gt;  $subject,
        'content'       =&gt;  $body,
    ),

  try {
    $response = $email-&gt;sendJson( $data );

  catch(Exception $e){
      print 'Call failed due to unhandled exception/error('. $e-&gt;getMessage().')'."\n";
    }
  }
}
you should always use some or the other Mail Transfer Agent (MTA) like Postfix, which helps in managing queue in-case of emails getting lost