Community Documentation

Programmatically send newsletter in hook_cron

Last updated July 24, 2012. Created by sapark on April 27, 2010.
Edited by authentictech, geerlingguy. Log in to edit this page.

This sets the sent status of the newsletter to pending. The newsletter will be sent during the next cron runs.

<?php
function yourmodulename_cron() {
 
$nid = 1;
 
$node = node_load($nid);
  if (
yourcondition) {
   
db_query("UPDATE {simplenews_newsletters}
                     SET s_status = '1' 
                     WHERE nid = %d"
, $node->nid);
   
/* Uncomment the line below if using Simplenews 6.x-2.x */
    /* module_load_include('inc', 'simplenews', 'includes/simplenews.mail'); */
   
simplenews_send_node($node);
  }
}
?>

Comments

I've got simplenews running

I've got simplenews running and I've set up a server cron job to be run once a day. I set the throttle to 100 but I've a 1000 subscribers so it would take 10 days to send them all out. I read I don't want to be doing cron runs every hour and that using poormanscron will conflict with a couple modules I have. So what do I do for my client so the newsletters are all sent without them having run cron exposed and them clicking it 10 times. Currently I have the server running cron.php every hour.

I have not actually tried it

I have not actually tried it but I plan to run cron as often as necessary to send all the newsletters in one day. I think running cron often is not so bad, remember to use the -O option in wget if you use wget in a crontab for instance. Otherwise, wget will save the cron.php html as a file in the user home directory, and this could result in thousands of 0-1kb files.

Try to use elysia_cron

Try to use elysia_cron module. You can configure different schedule for each cron task. For example set simplenews module to send mails once a hour while all other cron tasks will run once a day.

If you're using Simplenews

If you're using Simplenews 6.x-2.x (which is still in development), you will need to uncomment the module_load_include() line, as the function has been placed in the simplenews.mail.inc file, rather than the main simplenews.module file.

__________________
Work: Midwestern Mac, LLC | Personal: Life is a Prayer.com

How could do that???

So much time spending while figured out. You can't make documentation with such a mistakes!
this is bad - /* module_load_include('inc', 'simplenews', 'simplenews.mail'); */
it should be - /* module_load_include('inc', 'simplenews', 'includes/simplenews.mail'); */

!!!

Unfortunately, things change

Unfortunately, things change over time, and this change only happened recently ;-)

If you find a problem in any of the docs, you should be able to edit them (the edit link at the top of the page) and fix the problem. I've done so here.

__________________
Work: Midwestern Mac, LLC | Personal: Life is a Prayer.com

It's OK. Sorry, I was a bit

It's OK. Sorry, I was a bit nervous yesterday.
Thanks! )

Here is the code I use to automatically send newsletter

/**
* Implementation of hook_cron().
*/

function yourmodulename_cron() {
  $sql = "select nid from {simplenews_newsletters} where s_status = '0' ";
  $result = db_query($sql);
  while ($record = db_fetch_array($result))  {
    $node = node_load($record['nid']);
    $tid = 10050;   //term id of the newsletter
    if($node->type == 'simplenews') {   //your content type
      db_query("UPDATE {simplenews_newsletters}
                     SET s_status = '1', tid='$tid', sent_subscriber_count=
                (SELECT count(*) FROM {simplenews_subscriptions} s , {simplenews_snid_tid} u
                 WHERE u.tid=$tid and s.snid=u.snid and s.activated=1 and u.status=1)
                     WHERE nid = %d", $record['nid']);

      // Uncomment the line below if using Simplenews 6.x-2.x
      module_load_include('inc', 'simplenews', 'includes/simplenews.mail');

      //The newsletter will be sent during the next cron runs.
      $node->simplenews['tid'] = $tid;    //this is neccesary
      simplenews_send_node($node);

      //I want it sent immediately so I use the code in simplenews_cron()
  simplenews_mail_spool();
  simplenews_clear_spool();
  // Update sent status for newsletter admin panel.
  simplenews_send_status_update();
    }
  }
}

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Programmers
Level
Intermediate

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here