We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51565
    • 2 Posts
    Hi there!

    Currently I'm developing with modx revo.
    Today I stumbled against a "problem", I want a piece of php code which mails a user every 5 years since his register date.

    The solution I was thinking about is using a cronjob (which runs daily) loops through all users and sends an email to every user that registered 5,10,15 etc. years.

    The problem with this is if the server is down the user will never be able to get the mail. So I thought u can use a database table outside modx where you write the date to so you can see if the cronjob executed yesterday.

    I realise that this is not the right way if you are working with modx, so I am looking for the best way to accomplish this with modx.

    Yours sincerily,
    Roy Stijsiger
      • 46886
      • 1,154 Posts
      Ok first thing I can think of is you will need to calculate the register date for the user, the system doesn't automatically do that. Here is a link to a snippet that fires on registration and saves the value, courtesy of user Whitebyte:

      https://forums.modx.com/thread/95745/how-to-add-registration-date#dis-post-518264

      As for the database outside Modx, I am not sure, let's let someone more knowledgeable jump in.
        • 51565
        • 2 Posts
        Quote from: nuan88 at Dec 21, 2015, 07:15 AM
        Ok first thing I can think of is you will need to calculate the register date for the user, the system doesn't automatically do that. Here is a link to a snippet that fires on registration and saves the value, courtesy of user Whitebyte:

        https://forums.modx.com/thread/95745/how-to-add-registration-date#dis-post-518264

        As for the database outside Modx, I am not sure, let's let someone more knowledgeable jump in.


        The extended field is already registered. I am still looking for the right way to do the other things.
          • 13428 ☆ A M B ☆
          • 1,031 Posts
          You have to save two dates. registered and last_sent. last_sent should be compared with the today date, sending a mail on > 5 years. After that the last sent has to be set to the current date. That way the mail is sent after 5 years once.j
            • 46886
            • 1,154 Posts
            But why does he need a db outside of Modx? It doesn't make much sense to me, but I am not very skilled. It does seem to bring up a bunch of new problems, like now you need to export the user list to an external source...

            Can't you just have a snippet within Modx that will get the user list, check registered date against last_sent, and then send the email for users for whom the difference is >5 years.

            As far as the danger of the server being down, well last_sent should include a validation when being set. Then a value is only fixed when the email actually goes out.
              • 3749
              • 24,544 Posts
              If there is a field in the User Profile that you're not using (e.g., 'fax', 'website', or 'photo', you could store the 'lastSent' value there.

              I would store it as a Unix timestamp.

              It should be possible to use a single query that would retrieve profiles with a 'lastSent' value that's more than 5 years old. If the query results are not empty, you could then cycle through them, send the email, reset the 'lastSent' value, save the profile, and send the email.

              The reference point would be:

              $t = strtotime('-5 years');


              Your query would look for profiles where the field you use is < $t.

              You could use the MandrillX class to send the emails via Mandrill and periodically check at Mandrill to see if any of them bounced.

              You could put the code in a file and set a cron job to run it once a day, once a week, or whatever is appropriate.

                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 46886
                • 1,154 Posts
                I suspect BobRay provided basically all the needed info. He does that sometimes smiley Of course questions are welcome.

                I would just add that it seems BobRay assumes that the initial value of dateRegistered is going to be equal to lastSent at registration, then the calculation of whether a lastSent is too old can be made. I think this is needed for his workflow, I may be mistaken.

                So on dateRegistered, copy directly to lastSent, then compare lastSent to today's date, and >5 years triggers an email and then re-sets lastSent. This may mean you don't really need a value for dateRegistered itself, but of course that is a typical value to save.
                  • 3749
                  • 24,544 Posts
                  Thanks nuan88, I think you're right that you'd want to set the lastSent date to the current time at the time of registration (I'd just use time() rather than the date registered -- should be close enough).

                  One nit, this isn't quite what I suggested (though maybe you mean the same thing):

                  then compare lastSent to today's date, and >5 years triggers and email

                  I suggested calculating today's date - 5 years (once), then doing a query for users with a lastSent smaller than that value. That way you get just the users who need an email with a single query and don't have to test their lastSent values or make any comparisons in your code.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 41144
                    • 15 Posts
                    I would:


                    1. once a day (probably night time) generate a list of users that registered 5,10.. years ago today. Save it to DB.
                    2. every few minutes run a cron job that sends the emails in batches (and removes users from the queue of course)

                    IMO less error prone ;-)