MODx – Sending an email to registered users when resource is published 1


I’ve recently been working on a project which required an email to be sent to registered users when an article is added. The context of this project is a classified jobs board. Articles are repurposed as job opportunities, when a new article (job) is added, registered users should be notified about it.

To achieve this a couple of MODx packages were utilised. Besides the standard MODx install, two additional packages were used. The first is Articles, to more effectively manage jobs. The second is ResourceWatcher which is used to trigger functionality when a job is added.

[wp_ad_camp_2]

The issue is that ResourceWatcher is quite limiting in terms of what it can do when a resource is added. On its own, it can only send a message to a predefined email address. Here, the emails need to be sent to users. To achieve this, a custom snippet is written. I found very little support online for ResourceWatcher so here I’ll go through the stages to get it working.

After downloading and installing it through the package manager, it needs to be enabled. There are three times when ResourceWatcher will activate: When a resource is created, when a resource is published and when a resource is updated. All three of these, or just the one can be activated.

  1. From the manager interface, navigate to: System -> System Settings.
  2. Select ‘resourcewatcher’ from the first dropdown.
  3. Find the appropriate area, that you wish ResourceWatcher to act upon: ‘Area: New’, ‘Area: Pub’ or ‘Area:Update’.
  4. Set the Active field to ‘Yes’.
  5. Enter the name of the snippet to run under ‘Hooks’ e.g. ‘JobAdded’.

That’s ResourceWatcher setup and now the snippet JobAdded can be written. To start, create a snippet with that name. Then, enter the code below into the snippet. Below the code, I have stepped through what each line of the code does.

[wp_ad_camp_2]

 

Line 3 – $scriptProperties[‘id’] contains the ID of the resource (job) that was just created, we want to make sure there is an ID and that this snippet hasn’t just been activated from some random place.

Line 6 – Using that ID, we retrieve the resource (job) object.

Line 9 – This line gets all user profiles that have been created.

Line 11 – Loop through each user profile.

Lines 11+ – For each user, get their contact details, send them an email. All parts to replace should be identifiable, such as the from address ‘email@address.com’.

This code, together with ResourceWatcher will run every time a resource is published. It will then send an email to every registered user.

You may notice that I don’t actually do anything with the $resource variable, that’s because this code is simplified. You can get any information about the created resource from that object, such as its page title, content, creation date – Anything. It’s there for you to retrieve any information you like and send it in the email to a user.

[wp_ad_camp_2]

The code shown has been simplified a lot. The code I wrote was used to send email alerts about a published job to a user, if that users preference matched the properties of the new job. This was achieved by comparing Template Variables set to the Job resource against extended fields from each user. If that code would be valuable to anyone, please let me know in the comments below and I can share it – I wanted to simplify the code as much as possible for this example.


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “MODx – Sending an email to registered users when resource is published

  • Adam Biddle

    Hi Chris,

    I’m working on a similar site to what you have described. I would be very intrigued to understand how you achieved user defined notifications.

    Looking forward to your response.

    Many thanks in advance.

    Adam