function run_mail_queue() { global $config; // Get pending mails $filter = array('status' => 0); $mails = get_db_all_rows_filter('tpending_mail', $filter); // No pending mails if ($mails === false) { return; } // Init mailer $mailer = null; try { // Use local mailer if host not provided - Attach not supported !! if (empty($config['smtp_host'])) { // Empty snmp conf. System sendmail transport $transport = mail_get_transport(); $mailer = mail_get_mailer($transport); } else { $mailer = mail_get_mailer(); } } catch (Exception $e) { integria_logwrite(sprintf("Mail transport failure: %s", $e->getMessage())); return; } foreach ($mails as $email) { try { //Check if the email was sent at least once if (mail_send($email, $mailer) > 0) { process_sql_delete('tpending_mail', array('id' => (int) $email['id'])); } else { throw new Exception(__('The mail send failed')); } } catch (Exception $e) { $retries = $email['attempts'] + 1; if ($retries > $config['smtp_queue_retries']) { $status = 1; insert_event('MAIL_FAILURE', 0, 0, $email['recipient'] . ' - ' . $e->getMessage()); } else { $status = 0; } $values = array('status' => $status, 'attempts' => $retries); $where = array('id' => (int) $email['id']); process_sql_update('tpending_mail', $values, $where); $to = trim(ascii_output($email['recipient'])); integria_logwrite(sprintf('SMTP error sending to %s (%s)', $to, $e->getMessage())); } } }
/** * Sends an email. * * @param Swift_Transport $email * @param Swift_Mailer $mailer Mailer (optional). * If the mailer is empty, a mailer will be created using * the $config values. * * @return int Number of sent messages. * * @throws Swift_TransportException if the transport can't connect * @throws Exception Unexpected excptions */ function mail_send($email, $mailer = null) { global $config; if (empty($email)) { return; } if (empty($mailer)) { $mailer = mail_get_mailer(); } // Headers must be comma separated $extra_headers = isset($email["extra_headers"]) ? explode(",", $email["extra_headers"]) : array(); $message = Swift_Message::newInstance($email['subject']); if (empty($email['from'])) { $message->setFrom($config['mail_from']); } else { $message->setFrom($email['from']); } if (!empty($email['cc'])) { $message->setCc($email['cc']); } $to = trim(ascii_output($email['recipient'])); $toArray = array_map('trim', explode(',', $to)); if ($toArray) { $to = $toArray; } $message->setTo($to); if (!empty($email['image_list'])) { $images = explode(',', $email['image_list']); $body_images = ''; foreach ($images as $image) { if (!file_exists($image)) { continue; } $data = file_get_contents($image); if ($data) { $embed_image = $message->embed(Swift_Image::fromPath($image)); $body_images .= '<br><img src="' . $embed_image . '"/>'; } } } $message->setBody('<html><body>' . $email['body'] . $body_images . '</body></html>', 'text/html'); if (!empty($email['attachment_list'])) { $attachments = explode(',', $email['attachment_list']); foreach ($attachments as $attachment) { if (is_file($attachment)) { $message->attach(Swift_Attachment::fromPath($attachment)); } } } return $mailer->send($message); }