Topic: Send email on document publish?  (Read 1112 times)

Pages: [1]   Go Down

#1: 12-Sep-2008, 01:40 PM

Testers

dev_cw
Posts: 4,218

WWW
Is there a plugin that will send an email when a document is published or edited? I thought I saw this before but have not found anything. Would OnDocPublished and OnDocFormSave be the best events for this?
Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

Something is happening here, but you don't know what it is.
Do you, Mr. Jones?  -  [bob dylan]

#2: 12-Sep-2008, 03:12 PM


bunk58
Posts: 1,969

David Bunker

WWW
Here's a modified version of one originally written by Rachel Black.
I doubt it needs much explanation.
I've used a chunk named "Emailer" with the placeholders and email text.
The plugin would need the appropriate System Event ticked.
Tested quickly, but it seems to work.
Code:
$e = &$modx->Event;
if($e->name == 'OnDocFormSave') {
$pageid = $_POST['id'];
$pagetitle = $_POST['pagetitle'];
$editor = $modx->getLoginUserID() ;
$editedby = $modx->db->getValue('SELECT `username` FROM `modx_manager_users` WHERE `id` = 1');
$message = $modx->getChunk('Emailer');
$message = str_replace("[+id+]", $_POST['id'], $message);
$message = str_replace("[+pagetitle+]", $_POST['pagetitle'], $message);
$message = str_replace("[+editedby+]", $editedby, $message);
$to = 'someone@somewhere.com';
$subject = "Doc edited report";
$sender = $modx->config['emailsender'];
if(!mail("$to", $emailsubject, $message, "From: ".$sender."\r\n"."X-Mailer: Content Manager - PHP/".phpversion()))
{
$modx->logEvent(1,3,"$to, $subject, $message, $sender", "Email");
}
return true;
}

#3: 12-Sep-2008, 04:00 PM


bunk58
Posts: 1,969

David Bunker

WWW
Getting carried away with this plugin business.
Here's an improved version with configuration parameters for :
which plugin event, to email address, email subject & chunk name.
Plugin code :
Code:
$e = &$modx->Event;

if($e->name == ''.$piEvent.'') {
$pageid = $_POST['id'];
$pagetitle = $_POST['pagetitle'];
$editor = $modx->getLoginUserID() ;
$editedby = $modx->db->getValue('SELECT `username` FROM `modx_manager_users` WHERE `id` = 1');

$message = $modx->getChunk(''.$tplChunk.'');
$message = str_replace("[+id+]", $_POST['id'], $message);
$message = str_replace("[+pagetitle+]", $_POST['pagetitle'], $message);
$message = str_replace("[+editedby+]", $editedby, $message);

$sender = $modx->config['emailsender'];

if(!mail("$to", $subject, $message, "From: ".$sender."\r\n"."X-Mailer: Content Manager - PHP/".phpversion()))
{
$modx->logEvent(1,3,"$to, $subject, $message, $sender", "Email");
}
return true;
}
Plugin configuration settings :
Code:
&to=Mail To;string;someone@somewhere.com &piEvent=Plug In Event;string;OnDocFormSave &tplChunk=Chunk Name;string;Emailer &subject=Email Subject;string;Who's been messing with my site?

#4: 12-Sep-2008, 05:01 PM

Testers

dev_cw
Posts: 4,218

WWW
Thanks, that is a great help indeed.  Grin

BTW - I have often wondered, is $_POST['documentVariable'] the way to access document variables in a plugin? What about template variables, can these be accessed in the same way $_POST['tvName']?
Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

Something is happening here, but you don't know what it is.
Do you, Mr. Jones?  -  [bob dylan]

#5: 12-Sep-2008, 05:43 PM

Administrator

zi
MODx Special Forces /
Posts: 3,688

May Peace Be On You

WWW
This sounds helpful.

Guys, be sure to put in the repo and not let this cool stuff buried in the forums.

Thanks.

#6: 12-Sep-2008, 06:29 PM


bunk58
Posts: 1,969

David Bunker

WWW
This is a print of the TV array using the event OnTVFormSave
Code:
Array
(
    [id] => 11
    [mode] => 301
    [params] =>
    [stay] =>
    [name] => Name
    [caption] => Name
    [description] => Name
    [type] => richtext
    [elements] =>
    [default_text] => @DOCUMENT 37
    [display] =>
    [rank] => 0
    [template] => Array
        (
            [0] => 6
            [1] => 11
        )

    [chkalldocs] => on
    [categoryid] => 16
    [newcategory] =>
    [save] => Submit Query
)
So I suppose you could access them via a plugin using the appropriate $_POST['whatever'] format.
Adding on to the original plugin you can add the conent of the page with $_POST['ta'] so you could see what they'd done!
Pages: [1]   Go Up
0 Members and 1 Guest are viewing this topic.