Loop while sending emails

MichielStr 20 Mar, 2013
I am trying to accomplish the following: I want to send an invitation to perform a certain action to those who have not yet been invited to do so. The problem is, that I want to send a unique code along with the email to each person invited. Since the Email action does not support looping through arrays, I decided to try some creative solution. To no avail. This form should eventually be run as a cronjob.

So I have a table with a field called 'inv_sent', set to 0 by default. In the On Load event, I have a DB Record Loader. On Record Found contains an Event Loop to an event called On Sendmail. Once the form arrives in this event (so at least one record found), it collects some additional data, stores the unique ID in one table, sets inv_sent to 1 in another (both using Save DB actions) and sends the email. After that, another Event Loop sends the form back to On Load event to get the next record which has inv_sent set to 0 etc etc. Until no more records are found and the form stops at No Records Found in the DB Record Loader in the On Load event.

This doesn't work, however. I have managed to get the form set inv_sent of the last record found to 1 while trying and then get into an infinite loop, but in most configurations, the form just stops, showing a blank page.

Does anyone know if it is even possible to create such loop and, if so, how to do this?
GreyHead 20 Mar, 2013
Hi MichielStr,

I suggest that you don't do this by looping through the On Load and On Submit events.

If you want to run from a cron job then you may (I'm not certain) need to run from a new event to avoid the security checks on the On Submit event.

If the number of invitees is reasonably small - say less than 50, perhaps 100 - you could use my Email [GH] action. This will (a) accept an array of email addresses and (b) has an option to send individual emails.

This would then let you use a DB Multi Record Loader (or a Custom Code) action to get the array of email addresses and link the result directly into the Email [GH] action.

If the number is bigger then you could use a loop to break it up into chunks, or probably better link into one of the Joomla! mailer extensions that is designed to handle bulk mailing.

Bob
MichielStr 20 Mar, 2013
Hi Bob,

I am not using the On Submit event, I created a new event called On Sendmail. The problem is, that I want to add a unique key for each recipient to a link within the body of the email.

And although I can use an array of recipients in your Email action, I cannot add an array of unique keys to be used within the body. Or can I?

I think I have to change the form so it updates one record at a time and set the cronjob to run every minute. For the time being 1440 runs a day should be sufficient...

Michiel
GreyHead 20 Mar, 2013
Hi Michiel,

Ah, sorry, I'd forgotten about the unique codes. You are right.

Bob
MichielStr 21 Mar, 2013
Nonetheless I find it strange, that Chronoforms does not always seem to handle actions chronologically. Or I haven't found out the correct usage of the Controller/View settings.

I would expect the actions to be executed step-by-step, or at least the events. But Chronoforms' logic and mine don't always match...
GreyHead 21 Mar, 2013
Hi Michiel,

I'm not certain about Controller/View (and I rarely use View at all). I think that Controller runs in the 'as you see it' sequence in the event; and 'View' is deferred until the Show HTML action is run. Does that help?

Bob
MichielStr 22 Mar, 2013
Hi Bob,

My experience with the two modes is slightly different. If you take, for example the following order of actions:

Debugger -> DB Record Loader -> Custom Code (which changes the data retrieved)

With the Custom Code action is set to Controller mode, the changed data is shown by the Debugger. If it is set to View, the Debugger will not show the changed data, because the data will not be changed until the Event reaches the Custom Code action.

In fact, we might mean the same, just in different contexts...
GreyHead 23 Mar, 2013
Hi Michiel,

I think that is what I would expect. The Debugger shows the values at the point it runs. If you drag a second Debugger after the Custom Code then it should show the changed values.

Bob
MichielStr 25 Mar, 2013
Funny you mention that. Of course you are correct. The second would indeed show the changed values. The problem with debuggers, however, is, that it is of no use having two within the same event. Because the results shown will always be those of the last debugger.

Sometimes I would wish there would be something like a breakpoint-action, so I can see what happens (or more often: doesn't happen) when. Guess it would be possible to show the results of each debugger individually. Now I have to use echo and/or print_r...

Michiel
GreyHead 26 Mar, 2013
Hi Michiel,

Because the results shown will always be those of the last debugger.

I don't think that is correct, but I'd have to check to be sure. I think that at least the Data array shown is correct at the point the debugger runs.

I use Custom Code actions with echo statements at lot, they are much more focused on showing the exact data you need than the broad shot-gun of the Debugger.

Bob
This topic is locked and no more replies can be posted.