Skip to content

Instantly share code, notes, and snippets.

@Mark-H
Created March 8, 2012 14:11
Show Gist options
  • Save Mark-H/2001155 to your computer and use it in GitHub Desktop.
Save Mark-H/2001155 to your computer and use it in GitHub Desktop.
Plugin to send email notifications to all members of a user group
<p>Hi there!</p>
<p>The resource [[+pagetitle]] (ID: [[+id]]) has been [[+mode]].</p>
<p>You can login to the manager at www.mysite.com/manager/ to review and if needed publish the resource.</p>
<p>Thank you!</p>
<?php
// untested
// Update these if you have to
$usergroup = 1; // user group ID (1=admin)
$chunkname = 'notificationtpl'; // name of chunk for the email notification
$emailoptions = array(
'from' => $modx->getOption('emailsender'),
'fromName' => 'Resource Notification',
'subject' => 'Nofication of Created or Updated Resource',
'html' => true,
);
// No need to update below (unless I typo-ed)
$placeholders = $resource->toArray();
$placeholders['mode'] = ($mode == 'upd') ? 'updated' : 'created';
$message = $modx->getChunk($chunkname,$placeholders);
$userIds = $modx->getCollection('modUserGroupMember', array('user_group' => $usergroup));
foreach ($userIds as $userId) {
$user = $modx->getObject('modUser',$userId->get('member'));
if ($user) {
$user->sendEmail($message, $emailoptions);
}
}
@Mark-H
Copy link
Author

Mark-H commented Mar 8, 2012

Needs the OnDocFormSave event ticked for the plugin.

@tommls
Copy link

tommls commented Mar 8, 2012

Feature suggestion: instead of emailing each time a resource add/update, which could be a lot of updates, go through the list of resources once daily say every morning 8 am (just a good arbitrary time), collect them into a list, send the list like:
"These documents have been added or updated:
list of docs
Meanwhile I will work with this and try to figure out how to improve it.
Suggesting this because the above code would fire every time even a letter was changed, one does not want that, too many emails.
Many people I think wil find this more useful than RSS feeds etc. when they have user audiences unfamiliar with or unable to use RSS which is the case with the site I need this for, it's a private site with logins only.
Thank you, Tom

@Mark-H
Copy link
Author

Mark-H commented Mar 8, 2012

Please don't take a finger and next request an entire army..

@tommls
Copy link

tommls commented Mar 8, 2012 via email

@tommls
Copy link

tommls commented Mar 8, 2012

I must first learn how to get the whole array of resources for the context, then how to filter that by mode, then how to put that array into an email, then fire it at OnManagerLogout event, to get all the changed items (for the manager session). I'll update you whenever I finish it. Thank you, Tom

@Mark-H
Copy link
Author

Mark-H commented Mar 8, 2012

I thought I posted here, but looks like it didn't go through..

The frustration comes from you requesting a feature, me making time for it and handing it on a silver plate (well, almost) after which it doesn't even take you five minutes to put forward the next request, which takes an entirely different angle and makes me wonder if I shouldn't have spend time on something that apparently doesn't fit your needs anyway...

So, to answer your tweet, no I'm not mad. At time of my last post I was just frustrated for the above reason. I'm pretty sure someone else will find this gist some day (linked to it from my site) and see it's exactly what they needed.

@tommls
Copy link

tommls commented Mar 8, 2012 via email

@pepebe
Copy link

pepebe commented Oct 23, 2012

Found your gist and it was exactly what I needed. Thanks!!!

@sebastian-marinescu
Copy link

This is exactly what I needed. Many thanks for this exceptional ground-work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment