Example #1
0
function run_newsletter_queue()
{
    global $config;
    include_once $config["homedir"] . "/include/functions.php";
    require_once $config["homedir"] . "/include/swiftmailer/swift_required.php";
    if (isset($config['news_smtp_host'])) {
        //If Newsletters STMP parameters are not empty
        $total = $config["news_batch_newsletter"];
    } else {
        $total = $config["batch_newsletter"];
    }
    $utimestamp = date("U");
    $current_date = date("Y/m/d H:i:s");
    // Select valid QUEUES for processing
    $queues = get_db_all_rows_sql("SELECT * FROM tnewsletter_queue WHERE status = 1");
    if ($queues) {
        foreach ($queues as $queue) {
            $id_newsletter = get_db_sql("SELECT id_newsletter FROM tnewsletter_content WHERE id = " . $queue["id_newsletter_content"]);
            $id_queue = $queue["id"];
            // Get issue and newsletter records
            $issue = get_db_row("tnewsletter_content", "id", $queue["id_newsletter_content"]);
            //Add void pixel to track campaings
            $issue["html"] .= "<img src='" . $config["public_url"] . "/operation/newsletter/track_newsletter.php?id_content=" . $queue["id_newsletter_content"] . "'>";
            $newsletter = get_db_row("tnewsletter", "id", $id_newsletter);
            // TODO
            // max block size here is 500. NO MORE, fix this in the future with a real buffered system!
            $addresses = get_db_all_rows_sql("SELECT * FROM tnewsletter_queue_data WHERE status = 0 AND id_queue = {$id_queue} LIMIT 500");
            if (!$addresses) {
                process_sql("UPDATE tnewsletter_queue SET status=3 WHERE id = {$id_queue}");
            } else {
                foreach ($addresses as $address) {
                    if (!isset($config["news_smtp_host"])) {
                        //If Newsletters STMP parameters are empty
                        // Compose the mail for each valid address
                        $transport = Swift_SmtpTransport::newInstance($config["smtp_host"], $config["smtp_port"]);
                        $transport->setUsername($config["smtp_user"]);
                        $transport->setPassword($config["smtp_pass"]);
                    } else {
                        // Compose the mail for each valid address
                        $transport = Swift_SmtpTransport::newInstance($config["news_smtp_host"], $config["news_smtp_port"]);
                        $transport->setUsername($config["news_smtp_user"]);
                        $transport->setPassword($config["news_smtp_pass"]);
                    }
                    $mailer = Swift_Mailer::newInstance($transport);
                    $message = Swift_Message::newInstance(safe_output($issue["email_subject"]));
                    if (!empty($issue["from_address"])) {
                        $message->setFrom($issue["from_address"]);
                    } else {
                        $message->setFrom($newsletter["from_address"]);
                    }
                    $dest_email = trim(safe_output($address['email']));
                    $message->setTo($dest_email);
                    // TODO: replace names on macros in the body / HTML parts.
                    $message->setBody(safe_output($issue['html']), 'text/html', 'utf-8');
                    $message->addPart(replace_breaks(safe_output(safe_output($issue['plain']))), 'text/plain', 'utf-8');
                    if (!$mailer->send($message, $failures)) {
                        integria_logwrite("Trouble with address " . $failures[0]);
                        // TODO:
                        // Do something like error count to disable invalid addresses after a
                        // number of invalid counts
                        // process_sql ("UPDATE tnewsletter_address SET status=1 WHERE id_newsletter = $id_newsletter AND email = '$dest_email'");
                        process_sql("UPDATE tnewsletter_queue_data SET status=2 WHERE id_queue = {$id_queue} AND email = '{$dest_email}'");
                    } else {
                        process_sql("UPDATE tnewsletter_queue_data SET status=1 WHERE id_queue = {$id_queue} AND email = '{$dest_email}'");
                    }
                    $total = $total - 1;
                    if ($total <= 0) {
                        return;
                    }
                }
            }
            // end of loop for each address in the queue
        }
    }
    // end of main loop
}
Example #2
0
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()));
        }
    }
}