Notice: CMSB v2.16 Beta!

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

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

Hi Dave,

>Can you explain more...

In fact, I wondered whether it were possible with the function emailTemplate_loadFromDB(), not to send the email but to record in the _outgoing_mail table and to choose if it will be sent or will not be sent since a cronjob.

'logging' => true,
'backgroundSend' => 0/1,

It would be then possible to use the _outgoing_mail table with different plugins (with need different).

Thanks!

Djulia

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
interactivetools.com

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!

Djulia