Send email notifications when a document is saved
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.
- Doc Ids: Comma separated list of document ids that you what to receive notifications for
- Email Subject: The Subject of the email you will receive
- Email Address: Where should the email be sent
- Message: The message you want to send. Please keep "%X%" in the body of your message. It will be replaced by the name and id of the saved document.
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
Posts: 9
Reply #16 on : Wed October 07, 2009, 18:51:35
Posts: 9
Reply #15 on : Fri October 23, 2009, 10:50:32
Posts: 9
Reply #14 on : Mon October 26, 2009, 02:14:34
Posts: 7
Reply #13 on : Mon October 26, 2009, 07:05:17
Posts: 7
Reply #12 on : Mon October 26, 2009, 07:35:06
Posts: 9
Reply #11 on : Sun November 15, 2009, 12:40:29
Posts: 7
Reply #10 on : Mon November 16, 2009, 09:12:11
Posts: 9
Reply #9 on : Fri November 20, 2009, 20:34:57
Posts: 7
Reply #8 on : Fri November 20, 2009, 20:51:02
Posts: 9
Reply #7 on : Fri November 20, 2009, 21:34:29
Posts: 9
Reply #6 on : Fri November 20, 2009, 22:59:01
Posts: 7
Reply #5 on : Sat November 21, 2009, 21:58:45
Posts: 9
Reply #4 on : Mon May 10, 2010, 16:22:21
Posts: 7
Reply #3 on : Mon May 10, 2010, 19:16:47
Posts: 9
Reply #2 on : Tue May 11, 2010, 16:17:22
Posts: 7
Reply #1 on : Thu May 13, 2010, 15:16:19