Email Timelimit

Permalink
When writing code with MailHelper, is there anyway to specify not to send multiple emails to the same user/group in a certain period of time?

The site I'm working on has a job that checks a certain value and sends an email to a certain group if that value falls below a threshold. I'm worried that since this value can stay below a certain threshold indefinitely and that the job runs every minute that too many emails will be sent out.

I'd like to "lock down" groups for a period of time after an email is sent to avoid spamming them.
------
e.g. A potential problem I see.
- This value falls below and remains below a certain threshold for 24 hours
- Job runs every minute as values change arbitrarily
- User group gets emailed every minute
------

Any advice?

sethmartin
 
mnakalay replied on at Permalink Reply
mnakalay
Well first are you sure you need your job to run every minute? Couldn't you make it every 30 minutes?

If not, just make your job save a value in the config indicating time of last email sent and check it on subsequent call to decide whether or not to send again.
sethmartin replied on at Permalink Reply
sethmartin
Three questions::


1) Would I just create a file application/config/recentlymailed and check that?
2) There's no reason I couldn't store a list of all my users in there, right?
3) Can you provide an example/pseudocode of how to call that config file from a Job script? I often mess up namespacing/routing.


Much appreciated!
mnakalay replied on at Permalink Best Answer Reply
mnakalay
1) By config I meant a system included in C5 that allows to save simple key/value pairs in the database without having to create your own database system. In your cas your key would be for instance "last.email.time" and value would be the time.

Instead of do it that way, if you need something more granular, you could create a user attribute and add the time value in that attribute. That way each user would have its own time instead of one time for everybody.
You might even be able to do group attribute instead of user attribute.

2) Why would you want to do that? Concrete5 has a very powerful user management system, just use that.

3) The C5 config system just uses get() and save() as functions to do what it's supposed to do.
sethmartin replied on at Permalink Reply
sethmartin
Hmm. Setting a user attribute is a good idea.
Programmatically that might look something like this, correct?
//Generate the list of users for your particular group....
Loader::model('user_list');
    $userList = new UserList();
    $userList->filterByGroup('Target Group')
//Iterate through list of users in particular group and set an attribute for contacted
    foreach ($userList as $user)
    {
        $ui = UserInfo::getByID($user->getUserID());
       $ui->setAttribute('attribute_handle', $attributeValue);
    }


Then when the job is running, it would do a similar call to "get" whether they've been emailed recently. If not, the job can email them and set their recent contacted attribute to a recent timestamp.

Does that seem sound?

Question: Do I need to initialize that user attribute before that block of code or does that code actually create and set that attribute?
mnakalay replied on at Permalink Reply
mnakalay
Yes it seems ok. You need to get the user ID though.
sethmartin replied on at Permalink Reply
sethmartin
Ah good point. Like above? (see minor edit)
mnakalay replied on at Permalink Reply
mnakalay
yep looks better :)
sethmartin replied on at Permalink Reply
sethmartin
Great! I got all of the functionality I was looking for.
Thank you so much for the help.