Is it possible for a user group to be notified by email when a post is submitted using either the backend or a form on the front end?
4 Answers
Currently there isn't a native way to do this, but you could write a plugin which would work like this:
Wait for an event like:
content.onSaveContent
orentries.onSaveEntry
Get all users from a specific usergroup using the ElementCriteriaModel.
Use the EmailService to send a mail to each of them.
.
Quick-and-Dirty:
public function init()
{
parent::init();
//Event: onSaveEntry
craft()->on('entries.saveEntry', function(Event $event) {
//Get all users from UserGroup
$user_criteria = craft()->elements->getCriteria(ElementType::User);
$user_criteria->groupId = '1';
$users = $user_criteria->find();
//Send to each User
foreach ($users as $user) {
//Send Mail
$email = new EmailModel();
$email->toEmail = $user->email;
$email->subject = 'Heyyyyy';
$email->body = 'How you doin?';
craft()->email->sendEmail($email);
}
});
}
-
Thanks for this Victor, I am trying to get into simple plugin development so this is really useful. Is there are very simple boilerplate set of example files to learn how to add what you have given me and make it work as a plugin?– GarethCommented Jun 22, 2014 at 9:24
-
1Except for the $user_criteria part, which I got from the 'G+ Dev Group' the rest of the code is copied out of the docs (see my links in the list above). Also 'reverse-engineering' other peoples plugins explains a lot (list: straightupcraft.com/craft-plugins ). Otherwise it's 'Try & Error' or asking ;)– VictorCommented Jun 22, 2014 at 10:28
-
1Oh, and check out: buildwithcraft.com/docs/plugins/introduction which will help you get started. The
init()
functions goes into the main plugin-file.– VictorCommented Jun 22, 2014 at 10:30
For those that are looking for a plugin that simply notifies users (without all the overhead), take a look at Brief. It's a super-light plugin that simply notifies users whenever a new entry is added to a Section.
If anybody has a specific need for it, I'd be glad to add features.
More information: Brief
-
2Nice work! You should submit it to Straight Up Craft, the official unofficial plugins listing. Commented Sep 17, 2015 at 5:17
Alternatively, you could get in touch with Ben Parizek of Barrel Strength Design who has a plugin called Sprout Email. Currently in beta but you can request a preview.
I am using it as we speak.