Notice: CMSB v2.16 Beta!

34 posts by 6 authors in: Forums > CMS Builder
Last Post: August 28, 2012   (RSS)

Re: [Djulia] Notice: CMSB v2.16 Beta!

By Dave - August 27, 2012

Hi Djulia,

That's an interesting idea. Right now that only way to add backgroundSend messages to the _outgoing_mail table is to do so manually with MySQL.

The issue that we had is that when we want to send messages in the background there was usually lots of them (1,000 or more). I was concerned about PHP timing out if I called sendMessage() or emailTemplate_loadFromDB() in a loop.

So we were doing the mail merge in MySQL to avoid putting too much load on the server.

If you just wanted to put a few message in the Background Send queue, though, you could use some code like this:

$placeholders = array(
'datetime' => date('D, d M Y H:i:s'), // example use of placeholders
$emailHeaders = emailTemplate_loadFromDB(array(
'template_id' => 'USER-ACTION-NOTIFICATION',
'template_table' => 'email_templates',
'placeholders' => $placeholders,
if (!@$emailHeaders['disabled']) {
$colsToValues = array_slice_keys($emailHeaders, 'from', 'to', 'subject', 'html');
$colsToValues['createdDate'] = mysql_datetime();
$colsToValues['backgroundSend'] = '1';
$hideMysqlWarnings = true; // hide warnings if you don't define all the fields
mysql_insert('_outgoing_mail', $colsToValues, $hideMysqlWarnings);

If we wanted a function for that maybe we could call it sendBackground() to go with sendMessage() that way you could still wrap the emailTemplate functions and it would be explicitly clear in the code that a message was being queued for the background instead of sent.

eg: sendBackground(emailTemplate_loadFromDB($options)) ;

There's the still issue of timeouts though, so I'm not sure that I want to create that function as it might encourage programmers (at itools and elsewhere) to use it and end up running into timeout issues. Or maybe I'll just put a comment above the function warning about timeout issues so they can choose based on the project.

Let me know what you think and if you'd use that function. Thanks!
Dave Edis - Senior Developer

By Dave - August 27, 2012 - edited: August 27, 2012

After some thought, I decided to give this a try. We'll probably find it useful for smaller mailing jobs or cronjobs where timeouts aren't an issue.

You can call it just like sendMessage() but it saves it to the _outgoing_mail table.

// Alernate to sendMessage(), puts message in _outgoing_mail with 'backgroundSend' set to 1.
// Usage: $mailErrors = sendBackground($options);
// if ($mailErrors) { die($mailErrors); }
// NOTE: Doesn't support headers or attachments yet and 'logging' setting is ignored.
// NOTE: Requires a background-mailer script that is running on a cron-job
// NOTE: For large volumes of email do a mail-merge via MySQL "INSERT FROM ... SELECT ... JOIN" to avoid timeouts
function sendBackground($options = array()) {
if (@$options['disabled']) { return; } // don't send if 'disabled' option set
$hasText = array_key_exists('text', $options);
$hasHTML = array_key_exists('html', $options);

// error checking
$errors = '';
if (!isValidEmail( @$options['to'], true)) { $errors .= "'to' isn't a valid email '" .htmlspecialchars($options['to']). "'!<br/>\n"; }
if (!isValidEmail( @$options['from'] )) { $errors .= "'from' isn't a valid email '" .htmlspecialchars($options['from']). "'!<br/>\n"; }
if (!array_key_exists('subject', $options)) { $errors .= "'subject' must be defined!<br/>\n"; }
if (!$hasText && !$hasHTML) { $errors .= "Either 'text' or 'html' or both must be defined!<br/>\n"; }
if (!isValidEmail( @$options['from'] )) { $errors .= "'from' isn't a valid email '" .htmlspecialchars($options['from']). "'!<br/>\n"; }
if (!array_key_exists('subject', $options)) { $errors .= "'subject' must be defined!<br/>\n"; }
if (array_key_exists('headers', $options)) { $errors .= "'headers' not supported by sendBackground yet.<br/>\n"; }
if (array_key_exists('attachments', $options)) { $errors .= "'attachments' not supported by sendBackground yet.<br/>\n"; }
if ($errors) { return $errors; }

// save message
$colsToValues = array_slice_keys($options, array('from','to','subject','text','html'));
$colsToValues['createdDate='] = 'NOW()';
$colsToValues['sent'] = '';
$colsToValues['backgroundSend'] = '1';
mysql_insert('_outgoing_mail', $colsToValues, true);

Dave Edis - Senior Developer

By Djulia - August 28, 2012

Hi Dave,

I made some test and I find this function very interesting. That also gives a potential interesting for the _outgoing_mail table.

Thanks again for your patience!