Actions to do scheduled sending

axel-at-konzepto.net - February 9, 2008 - 02:08
Project:Simplenews
Version:5.x-1.2
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:duplicate
Description

Hello there... quite new to drupal and barely knowing what i do ;-) i hacked some code to do scheduled sending.
This patch contains 1) actions for the actions.module in the file simplenews_actions.inc , 2) actions for the workflow-ng.module in file workflow_ng_simplenews.inc and 3) the corresponding include_once.
Core of the send actions are the following lines
$node->send = 1;
$node->s_status = 1;
which are wrapped into the necessary hooks.
For info how to schedule soch actions via "CCLinks" see this fine workflow-ng handbook page.
(i figured out that the cclinks part of the workflow-ng module does what i need, let me select a target time. actions.module and sched_act did not work for me. suggestions welcome.)

AttachmentSize
simplenews-actions.patch2.49 KB

#1

techninja - February 16, 2008 - 23:46

A valid effort for a good cause! It looks as though the Drupal 6 release of simplenews is a ways off (It should support scheduling natively), not to mention the bleeding edge of 6.0 is a little daunting for a production site launching in less than a month..

After googling and googling, this seems to be the ONLY lead left for getting simplenews to send via scheduled action (Without looking into how code it myself) , and it seems not far off it's mark! but.. I have yet to make this work.

I manually converted the patch to an action, and instead of workflow_ng I'm using workflow and actions. Everything is set up perfectly, with the action showing up, set to trigger with the workflow state change, even the scheduling is working wonderfully. When the action runs, the newsletter is changed from a draft, to sent, and a watchdog message shows in green that it's sending the newsletter (twice, for some odd reason). But then, after showing so much promise, no email is ever sent to any of the subscribers! I've tried twice, with no emailed results, but it acts like everything is fine (No errors in the log).

I've successfully sent two newsletters through the submission form directly, so it must be code used to enact the send (Inside the action->do area), though I can't quite see in what way this is wrong:

$node->send = 1;
$node->s_status = 1;
node_save($node);

Any insight from anyone? Could just be my setup, but I haven't heard any word from others trying out the patch...

--TechNinja

#2

axel-at-konzepto.net - February 17, 2008 - 20:09

hello techninja,

in fact on my own site i also had failures sending, after some successful sends.
perhaps the developers might give us some more insight.
at the moment i'm stepping forward from trialanderror coding to a more thorough understanding of the simplenews code.
although for some other reasons i cannot do tests at the moment, i can give you my latest insights:

1. the following code should mimic a form submission more closely:

      $node->send = 1;
      $node = node_submit($node);
      node_save($node);

2. i had another error which prevented sending and which i fixed by correcting one line:
in simplenews.module line about 385 in function simplenews_validate_taxonomy($taxonomy)
change

    $taxes[] = $tax;

to
    $taxes[] = $tax->tid;

these are my insights so far, hope this helps.
best
aexl

#3

techninja - February 18, 2008 - 02:48

Noo! Anything but Taxes!!

Hey! wonderful to hear back from you.. Strange that bug in simplenews.. it was line 375 for me (simplenews 5.x-1.2)

Hmm.. I slapped in the new line (You're really just replacing $node->s_status = 1; with $node = node_submit($node);) into the send action and hay sheesh it works! Now I didn't check to see weather the same error was happening to me, but I went ahead and changed the line anyways.

Thanks a million! This was one of the last pieces to getting my site in ship-shape... Now the workflow actions aren't very pretty at the moment, but I can deal with usability any day (Ok, maybe only wednesdays)

Now I just wish for testing purposes there was a non-hacky way to resend a newsletter (Without resetting things in the DB)

Thanks again! I hope someone else will find this helpful. I also hope this continues to work untill Simplenews 6.x-2.0 is ready

--TechNinja

#4

axel-at-konzepto.net - February 18, 2008 - 16:36

>Now I just wish for testing purposes there was a non-hacky way to resend a newsletter (Without resetting things in the DB)

you could try adding a "make unsent" action ($node->sent = 0 )
might work ;-)

#5

axel-at-konzepto.net - February 20, 2008 - 22:51

concerning #2 above, i found out, that there is an inconsistency in $node->taxonomy (see #224566: $node->taxonomy schema), so that
n simplenews.module line about 385 in function simplenews_validate_taxonomy($taxonomy)

    $taxes[] = $tax;

is the right thing to do when a form is submitted

and

    $taxes[] = $tax->tid;

is the right thing to do in an action.

together they make:

      $taxes[] = is_object($tax)?$tax->tid:$tax;

Here is the patch with the actions and said bugfix against head.

AttachmentSize
simplenews-actions-2.patch 3.4 KB

#6

axel-at-konzepto.net - February 20, 2008 - 22:58

OOPS!
forget the last patch, here is the right one.

AttachmentSize
simplenews-actions-2.patch 2.93 KB

#7

rickvug - February 23, 2008 - 21:56

Nice work! Definitely a +1 here for the functionality.

#8

Sutharsan - February 29, 2008 - 12:50

aexl, thanks for the patch. Once the Simplenews 6.x is out I will consider to implement it in 5.x.
In the mean time, inspired by your work, I added a basic actions and triggers implementation in HEAD (simplenews_actions.inc). Sending newsletters is not available yet since the send mechanism needs to be rewritten.

#9

axel-at-konzepto.net - March 1, 2008 - 00:30

Sutharsan, although i dont understand the send mechanism thoroughly (which might change in a rewritten version),
i had a brief look at your code and wondered:
why mess around with _simplenews_send in your send action?
as you see in #2, just setting the send flag and relying on cron works too.
furthermore, there may be more actions to process in a request, so we should not consume more cpu time than necessary.

#10

Sutharsan - March 1, 2008 - 09:58

in HEAD_simplenews_send() is currently only used there by cron action, the rest is commented out. Send mechanism needs to be re-written to work with the new mail mechanisme used in core's mail.inc. Minimizing cpu load is on my mind during the development.

#11

techninja - March 26, 2008 - 03:40

Hey guys, just found something interesting...

Turns out this actually might not work... sort of.

All my previously fruitful tests on this code were done running cron manually. There's nothing wrong with this, EXCEPT for the fact that apparently the email send FAILS somehow when running cron Anonymously.

When cron is run without being logged in as admin, the first alert message that shows up says that the newsletter is trying to send (Twice, oddly enough), just as it does when you're logged in, but this results with no emails being sent. When managing draft newsletters after running the anonymous cron, the newsletter shows that it was published, but with a red x under the "sent" column. What the heck?

Does the action code need permissions tweaked to work anonymously? Is there a means of running cron as admin in crontab? Does anyone else see this behavior?

Should I just keep talking in questions to make things seem more interesting?!

#12

Sutharsan - April 19, 2008 - 23:14
Status:needs review» won't fix

Actions to send newsletter(s) has been implemented in 6.x-1.x-dev. Backporting to 5.x is not in my plans

#13

axel-at-konzepto.net - April 24, 2008 - 23:05
Status:won't fix» needs review

Sorry for not looking after this for so long.

techninja, you are right. Node_save does not let anonymous send newsletters.
So we have to set the send flag in the databese.

Here is a patch, which works here.
You like to test it?

AttachmentSize
simplenews-actions-3.patch 3.07 KB

#14

krunar - April 27, 2008 - 23:01

Hi there...
first of all the patch is falling with

Hunk #2 FAILED at 386.
1 out of 2 hunks FAILED -- saving rejects to file simplenews.module.rej

more simplenews.module.rej
*************** function simplenews_validate_taxonomy($t
*** 376,382 ****
    if (isset($tids) && !empty($taxonomy)) {
      $taxes = array();
      foreach ($taxonomy as $tax) {
-       $taxes[] = $tax;
      }
      $selected_terms = array_intersect($tids, $taxes);
      return empty($selected_terms) ? FALSE : $selected_terms;
--- 386,392 ----
    if (isset($tids) && !empty($taxonomy)) {
      $taxes = array();
      foreach ($taxonomy as $tax) {
+       $taxes[] = is_object($tax)?$tax->tid:$tax;
      }
      $selected_terms = array_intersect($tids, $taxes);
      return empty($selected_terms) ? FALSE : $selected_terms;

You can by pass this manualy...

The important is that it seems to work just FINE with workflow-ng.

I have hack the latter a little in order to create a newsletter node as soon as a new page is created and then use the link to send it using the cclink.

Thank you very much

#15

axel-at-konzepto.net - April 30, 2008 - 15:27

thank you krunar for pointing this out.
i removed that chunk, as it should not be necessary in this version any more.

AttachmentSize
simplenews-actions-4.patch 2.71 KB

#16

dgtlmoon - June 3, 2008 - 04:44

Another take on this is the simplenews scheduler module available for 5.x that resends newsletters to a schedule

#17

Sutharsan - July 16, 2008 - 07:58
Status:needs review» duplicate

I've combined two peices of workflow_ng integration here: http://drupal.org/node/182218
I mark this issue duplicate. Please continue comment at the above issue.
Thanks for your effort so far!

 
 

Drupal is a registered trademark of Dries Buytaert.