Send email notifications when a document is saved

on 05-Oct-2009 | Comments ( 20 ) Tags: Evolution

To send an email notification when a document is saved you need to understand how modx events work. To see all the events go to the modx wiki page.

In order to send notifications we need to build a plugin that will run when the document is saved. In this case I will use the OnDocFormSave event.

The Plugin

Create a new Plugin by going to Elements/Manage Elements/Plugins/New Plugin

Paste the code below

$e = &$modx->Event;

if ($e->name == "OnDocFormSave"){
	if($ids){//check Ids
		$aIds = explode(',',$ids);
		foreach($aIds as $dId){//loop ids
			if($id == trim($dId)){
				global $modx;
				$aTitle = $modx->getPageInfo($id,'','pagetitle'); //get pagetitle 
				$message = str_replace("%X%", '"'. $aTitle['pagetitle']. '" ('. $id . ')', wordwrap($message, 100));//create message
				sendEmail($email, $subject, $message);
			}
		}
	}
}

 // Send email
function sendEmail($to, $subject, $message) {
	$headers ="MIME-Version: 1.0\n".
              "Content-type: text/html; charset=iso-8859-1\n".
              "From: ".$_SERVER['SERVER_NAME']."<no-reply@{$_SERVER['HTTP_HOST']}>\n".
              "X-Mailer: PHP/".phpversion();

$body = <<<EOD
$message
EOD;

   $mail = mail($to, $subject, $body, $headers);
}

Name the plugin accordingly: Eg: Notification on Save Document

Configure the plugin

Without necessarily saving the plugin, click on the Configuration tab and paste the code below in Plugin configuration area.

&ids=Doc Ids;text; &subject=Email Subject;text;Document Modified &email=Email Address;text;your-email@address.com &message=Message (%X% will be replaced);textarea;Document %X% has been changed.

Click Update parameter display and change the configuration parameters.

Activate the pluging

In order for the plugin to have effect, there is one more step that you need to do.

Click on the System Events tab and check the OnDocFormSave check-box.

Note: I find it easy to press "CTRL + F" or "CMD + F" and paste the OnDocFormSave in the search box. Press enter and you'll see the highlighted event.

Now you can save the plugin.

Testing the plugin

In order to test the plugin you have to open a document and save it. This plugin does not detect if you made any changes to the document as there is no point in saving a document if you didn't change it.

 

That's all. If you have any ideas on how to improve this plugin leave a message.

Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
 
mrhaw
Posts: 12
Comment
Sweet!
Reply #20 on : Wed October 07, 2009, 17:51:35
Very nice plugin! : )
Aaron Ladage
Posts: 12
Comment
Very cool
Reply #19 on : Fri October 23, 2009, 09:50:32
This is a great tutorial, and a plugin that might end up being very useful for me.

Just curious: I'm looking for a way to notify a group of "subscribed" users could receive notification whenever docs in a certain subsection are added. What would I have to modify above to pull this off?
Ron Bolton
Posts: 12
Comment
email notification
Reply #18 on : Mon October 26, 2009, 01:14:34
If the programmer provides an ID list doesn't your code send an email for all pages that are in the list when just one of the pages in the list is saved?
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #17 on : Mon October 26, 2009, 06:05:17
Hi Ron,

Line 7 should prevent that. $id contains the id of the saved document
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #16 on : Mon October 26, 2009, 06:35:06
@Aaron

How do you know which user is subscribed and to which page(s)?

If you can get that information in the plugin it should very easy to do.
Last Edit: October 26, 2009, 06:35:35 by cipa  
Aaron Ladage
Posts: 12
Comment
Re: Send email notifications when a document is saved
Reply #15 on : Sun November 15, 2009, 11:40:29
That's what I'm not sure about. The list of subscribed users could be something as simple as an eform2db form that adds the person's e-mail address to a database table. The docs they would be subscribing to would be the same every time -- can you tell me how to make it a subset of a certain doc, rather than the doc itself?
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #14 on : Mon November 16, 2009, 08:12:11
@Aaron
Please post your request in the modx forums. This is a complex task and I do not think we should "clutter" the comments area. Thx
ryan
Posts: 12
Comment
ids
Reply #13 on : Fri November 20, 2009, 19:34:57
is there way to have it send an update for all documents saves? what if we are totally paranoid and every time a document is modified we get email. how do we make it global?
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #12 on : Fri November 20, 2009, 19:51:02
Try removing lines 4, 5, 6, 7, 12, 13, 14.
Ryan
Posts: 12
Comment
awesome!
Reply #11 on : Fri November 20, 2009, 20:34:29
removed the lines
removed the first parameter
removed internal paranoia

thanks cipa!
ryan
Posts: 12
Comment
Modification Help
Reply #10 on : Fri November 20, 2009, 21:59:01
Just curious how I can tie in the creator - to show who edited it.

$e = &$modx->Event;

if ($e->name == "OnDocFormSave"){
global $modx;
$aTitle = $modx->getPageInfo($id,'','pagetitle','creator'); //get pagetitle
$message = str_replace("%X%", '"'. $aTitle['pagetitle']. '" ('. $id . ')', str_replace("%Y%", '"'. $aTitle['creator']. '"', wordwrap($message, 100));//create message
sendEmail($email, $subject, $message);
}

// Send email
function sendEmail($to, $subject, $message) {
$headers ="MIME-Version: 1.0\n".
"Content-type: text/html; charset=iso-8859-1\n".
"From: ".$_SERVER['SERVER_NAME']."<no-reply@{$_SERVER['HTTP_HOST']}>\n".
"X-Mailer: PHP/".phpversion();

$body = <<<EOD
$message
EOD;

$mail = mail($to, $subject, $body, $headers);
}
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #9 on : Sat November 21, 2009, 20:58:45
1. Using the doc id get the value of the editedby field in the modx_site_content table
2. Using the value in the editedby field get the name of the user from the modx_manager_users.
3. Use the name in the email you send

Good Luck
Sebastian
Posts: 12
Comment
Re: Send email notifications when a document is saved
Reply #8 on : Mon May 10, 2010, 15:22:21
Hi,

that's a very cool plug in - i will text it right after post this comment.

Do you have a clou how to let only messages be sent whether the document has a certain parent.

Would like to let a person know that in her group of documents a new document has been post.

greez

Sebastian
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #7 on : Mon May 10, 2010, 18:16:47
Try this

$aInfo = $modx->getPageInfo($id,'','pagetitle, parent');
if($aInfo['pagetitle'] == YOUR_ID){
$message = str_replace("%X%", '"'. $aInfo['pagetitle']. '" ('. $id . ')', wordwrap($message, 100));
sendEmail($email, $subject, $message);
}
Sebastian
Posts: 12
Comment
Re: Send email notifications when a document is saved
Reply #6 on : Tue May 11, 2010, 15:17:22
Hi,

did my own trials, but without success.
Do you have an idea what could be the problem?
Would like to define ultimate parents (=sections of my page) to be checked instead of only one parent:
==
defined $parents like you did with $ids
==

if ($e->name == "OnDocFormSave"){
$parent = $modx->runSnippet("UltimateParent");
if($parents){//check Ids
$allParents = explode(',',$parents);
foreach($allParents as $oneParent){//loop ids
if($parent == trim($oneParent)){
global $modx;
$aTitle = $modx->getPageInfo($id,'','pagetitle'); //get pagetitle
$message = str_replace("%X%", '"'. $aTitle['pagetitle']. '" ('. $id . ')', wordwrap($message, 100));//create message
sendEmail($email, $subject, $message);
}
}
}
}
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #5 on : Thu May 13, 2010, 14:16:19
Instead of using foreach you could try to use in_array php function
http://php.net/manual/en/function.in-array.php

Let me know if you still have problems. Contact me here http://modxrules.com/contact
Pietro
Posts: 12
Comment
cool!
Reply #4 on : Fri September 24, 2010, 05:36:33
Great plugin-
do you think it would be possible to only send the e-mail notification if a certain user(s) saved the document?
cipa
Posts: 8
Comment
Re: Send email notifications when a document is saved
Reply #3 on : Fri September 24, 2010, 07:32:48
Yes it is possible! You can define a list of users and compare the list with the $_SESSION['mgrShortname'] value. Basically you need to add a configuration option in the configuration tab, than in the code, split the list of users, loop through it and send the email when the $_SESSION['mgrShortname'] is found in the loop.
Last Edit: September 24, 2010, 07:33:31 by cipa  
Pietro
Posts: 12
Comment
Re: Send email notifications when a document is saved
Reply #2 on : Fri September 24, 2010, 10:08:01
@cipa:
it works; thanks again & keep up the good work!
Kamersa
Posts: 12
Comment
http://video.xrumer.tv/
Reply #1 on : Thu May 24, 2012, 04:41:03
У нас можно купить: XRumer 7 & Hrefer - профессиональный софт для SЕО - ЖМИТЕ

Quick modx Evolution Tags

  • cached [[snippet]] or uncached [!snippet!]
  • {{chunk}}
  • [+placeholder+]
  • [*resourceField/TV*]
  • [^timing^]
  • [~link~]

Quick modx Revolution Tags

  • [[snippet]]
  • [[$chunk]]
  • [[+placeholder]]
  • [[*resourceField/TV]]
  • [[~link]]
  • [^timing^]
  • [[++systemSetting]]
  • [[%languageStringKey]]
  • all tags can be called un-cached like: [[! snippet]]

Timing Tags (Evo and Revo)

  • [^qt^] - Query time
  • [^q^] - Query count
  • [^p^] - Parse time
  • [^t^] - Total time
  • [^s^] - Source
© modxRULES! 2009-2014