Upon Submit Cannot Send Email Notification in MIGXdb

I am facing a challenge to send an email notification upon uploading a document through MIGXdb.

So far I have not foumd any examples of this working but I would have thought someone has done this before.

So far I have tried using a code found elsewhere in MODX and adapted it, unsuccessfully :sweat:

In MIGXdb Settings I have placed the following line in the field Hook Snippets:

{"aftersave":"hookComEmail"}

Then in the snippet hookComEmail I have this:

$message = 'Hi, a report has been uploaded: '.$hook->getValue('contractor')
 . ' with email '.$hook->getValue('mailadd').'.';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'myemail@website.com');
$modx->mail->set(modMail::MAIL_FROM_NAME,'This Company');
$modx->mail->set(modMail::MAIL_SENDER,'My Name');
$modx->mail->set(modMail::MAIL_SUBJECT,'Document Uploaded');
$modx->mail->address('to','me@website.com');
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$err);
}
$modx->mail->reset();
/* tell our snippet we're good and can continue */
return true;

getValue ‘contractor’ & getValue ‘mailadd’ are both in the schema and in the database

So far I can upload the documents but no email is being sent.

Appreciate if anyone has any ideas how to rectify my code so it can send email notifications or suggest where I am going wrong

I think, $hook->getValue() is just for FormIt-Hooks.
In the MIGX aftersave-hook you have to do something like this

<?php
$object = & $scriptProperties['object'];
$contractor = $object->get('contractor');

Hi halftrainedharry,

Thanks for the response, I tried this but it still did not send after submit and upload the doc, here’s the new code:

$message = 'Hi, a report has been uploaded: '.$object = & $scriptProperties['object'];
$contractor = $object->get('contractor');
$email = $object->get('mailadd');
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'myemail@website.com');
$modx->mail->set(modMail::MAIL_FROM_NAME,'This Company');
$modx->mail->set(modMail::MAIL_SENDER,'My Name');
$modx->mail->set(modMail::MAIL_SUBJECT,'Document Uploaded');
$modx->mail->address('to','me@website.com');
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the 
email: '.$err);
}
$modx->mail->reset();
/* tell our snippet we're good and can continue */
return true;

Did I implement it correctly?

Probably cleaner to do it this way.

<?php
$object = & $scriptProperties['object'];
$contractor = $object->get('contractor');
$email = $object->get('mailadd');
$message = 'Hi, a report has been uploaded: ' . $contractor . ' with email ' . $email . '.';

Is there an entry in your error log?
This line should log a message if the sending of the mail fails.

$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the 
email: '.$err);

Maybe you could also use this log-function to debug your code. For example to check if your snippets gets called at all.

$modx->log(modX::LOG_LEVEL_ERROR,'my log message');

Well it did not work again, but the alarms generated are just ‘deprecated alarms’ maybe not related. I’m thinking this code has not been triggered. Here’s the alarms"

[2020-07-04 12:39:35] (ERROR in modProcessor::run @ … /core/model/modx/modprocessor.class.php : 177) Flat file processor support, used for action mgr/migxdb/getList with path /home/telefina/public_html/core/components/migx/processors/mgr/migxdb/getlist.php, is deprecated since version 2.7.0.
[2020-07-04 12:39:35] (ERROR in modProcessor::run @ … /core/model/modx/modprocessor.class.php : 177) Flat file processor support, used for action mgr/migxdb/update with path /home/telefina/public_html/core/components/migx/processors/mgr/migxdb/update.php, is deprecated since version 2.7.0.

Actually I’m unsure about where to place the code to trigger your snippet, I’m using:

{"aftersave":"hookComEmail"}

This is placed in ‘Hook Snippets’ under the tab ‘MIGXdb Settings’

I believe your code is ok, but I’m unsure where to place the call in the MIGXdb CMP

You could just place some temporary calls to $modx->log() in the code to make sure the first part is correct.

<?php
$modx->log(modX::LOG_LEVEL_ERROR,'aftersave-hook called'); //just temporarily, for debugging purposes
$object = & $scriptProperties['object'];
$contractor = $object->get('contractor');
$email = $object->get('mailadd');
$message = 'Hi, a report has been uploaded: ' . $contractor . ' with email ' . $email . '.';
$modx->log(modX::LOG_LEVEL_ERROR,'email message is: '.$message); //just temporarily, for debugging purposes
...

For the email-sending part:
Have you checked your spam-folder?
Make sure the from and the to email-addresses are different.

If you haven’t already, you might want to install QuickEmail and use the same credentials to make sure the mail system is working as it should.

@ halftrainedharry

I input the debugging code and went through the complete process of filling in the MIGXdb CMP form and then successfully uploading a pdf file but no email was sent and no related alarms or warnings were recorded in the log file.

I can only conclude that the code input into ‘Hook Snippets’ under the tab ‘MIGXdb Settings’ is probably the incorrect location or method to trigger sending the email.

@ bobray Thanks for the suggestion. At this point I’m thinking it is not an email issue. On the front end I have implemented a Formit2db which generates emails successfully.

I think I jut need to find the correct method of implementing some code to send an email within MIGXdb after the action of a file being uploaded. However, after some extensive searching I cannot find any examples in the forums or on Google where anyone has done this before.

That being said, I do not believe it is a difficult task, it’s probably me that has overlooked something very simple.

could you post your whole exported config, please?
and the whole code of your aftersave - hooksnippet?

Hi Bruno,

The aftersave - hooksnippet is as follows:

<?php
$modx->log(modX::LOG_LEVEL_ERROR,'aftersave-hook called');
$object = & $scriptProperties['object'];
$contractor = $object->get('contractor');
$email = $object->get('mailadd');
$message = 'Hi, a report has been uploaded: ' . $contractor . ' with email ' . $email . '.';
$modx->log(modX::LOG_LEVEL_ERROR,'email message is: '.$message);

For the whole exported config, how do I export it? I found an action button for ‘importexportmigx’ and I have ticked that box and the button appears on the CMP, however when I click it nothing happens.

Am I missing something in the export process?

In the ‘MIGX Management’ -> Tab ‘MIGX’ right-click on the Config and select ‘Export/Import’. A new window appears with a field ‘Json’. Copy the content of this field.

Below main exported config:

     {
 "formtabs":[
  {
  "MIGX_id":1,
  "caption":"tfsurvey",
  "print_before_tabs":"0",
  "fields":[
    {
      "MIGX_id":46,
      "field":"contractor",
      "caption":"Contractor Name",
      "description":"Use same name for all your sites",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":1
    },
    {
      "MIGX_id":47,
      "field":"mailadd",
      "caption":"Email Address",
      "description":"Email address where the contractor wants to receive TSSR approved notification",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":2
    },
    {
      "MIGX_id":1,
      "field":"region",
      "caption":"Region",
      "description":"Central, Northern, Eastern, Southern, East Malaysia",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":3
    },
    {
      "MIGX_id":2,
      "field":"state",
      "caption":"State",
      "description":"Selangor, Penang, Pahang ... etc",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":4
    },
    {
      "MIGX_id":3,
      "field":"area",
      "caption":"Area",
      "description":"Puchong, Klang, Georgetown, ... etc local town where installation takes place",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":5
    },
    {
      "MIGX_id":5,
      "field":"shellLrd",
      "caption":"Shell Lrd",
      "description":"",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":6
    },
    {
      "MIGX_id":6,
      "field":"neSiteName",
      "caption":"NE Site Name",
      "description":"Near End, Shell site name",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":7
    },
    {
      "MIGX_id":7,
      "field":"latitude",
      "caption":"Latitude",
      "description":"Shell station latitude",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":8
    },
    {
      "MIGX_id":8,
      "field":"longitude",
      "caption":"Longitude",
      "description":"Shell station longitude",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":9
    },
    {
      "MIGX_id":9,
      "field":"siteAddress",
      "caption":"Site Address",
      "description":"Shell station address",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":10
    },
    {
      "MIGX_id":10,
      "field":"feMaxisLrd",
      "caption":"FE Maxis Lrd",
      "description":"Maxis site Lrd",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":11
    },
    {
      "MIGX_id":11,
      "field":"latitude2",
      "caption":"FE Latitude",
      "description":"Far End Maxis latitude",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":12
    },
    {
      "MIGX_id":12,
      "field":"longitude2",
      "caption":"FE Longitude",
      "description":"Far end Maxis longitude",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":13
    },
    {
      "MIGX_id":13,
      "field":"feSiteAddress",
      "caption":"FE Site Address",
      "description":"Far end Maxis address",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":14
    },
    {
      "MIGX_id":57,
      "field":"tssrSubmissionActual",
      "caption":"TSSR Submitted",
      "description":"Date TSSR submitted (today's date)",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":15
    }
  ],
  "pos":1
  },
  {
  "MIGX_id":5,
  "caption":"Files",
  "print_before_tabs":"0",
  "fields":[
    {
      "MIGX_id":54,
      "field":"files",
      "caption":"Files",
      "description":"",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"migx",
      "validation":"",
      "configs":"survey_files",
      "restrictive_condition":"[[migxIsNewObject]]",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":1
    }
  ],
  "pos":2
 },
 {
  "MIGX_id":6,
  "caption":"Images",
  "print_before_tabs":"0",
  "fields":[
    {
      "MIGX_id":55,
      "field":"images",
      "caption":"Images",
      "description":"",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"migx",
      "validation":"",
      "configs":"survey_images",
      "restrictive_condition":"[[migxIsNewObject]]",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":1
    }
  ],
  "pos":3
 },
 {
  "MIGX_id":7,
  "caption":"Drawings",
  "print_before_tabs":"0",
  "fields":[
    {
      "MIGX_id":56,
      "field":"drawings",
      "caption":"Drawings",
      "description":"",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"migx",
      "validation":"",
      "configs":"survey_drawings",
      "restrictive_condition":"[[migxIsNewObject]]",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":1
    }
  ],
  "pos":4
}
],
"contextmenus":"update||duplicate||addbefore||addafter||publish||unpublish||recall_remove_delete||remove",
"actionbuttons":"addItem||exportview||exportimportmigx",
"columnbuttons":"",
"filters":"",
"extended":{
"migx_add":"Add TSSR",
"disable_add_item":"",
"add_items_directly":"",
"formcaption":"",
"update_win_title":"",
"win_id":"tfsurvey",
"maxRecords":"",
"addNewItemAt":"bottom",
"media_source_id":"",
"multiple_formtabs":"",
"multiple_formtabs_label":"",
"multiple_formtabs_field":"",
"multiple_formtabs_optionstext":"",
"multiple_formtabs_optionsvalue":"",
"actionbuttonsperrow":4,
"winbuttonslist":"",
"extrahandlers":"",
"filtersperrow":4,
"packageName":"tfsurvey",
"classname":"TfSurvey",
"task":"",
"getlistsort":"pos",
"getlistsortdir":"",
"sortconfig":"",
"gridpagesize":100,
"use_custom_prefix":"0",
"prefix":"",
"grid":"",
"gridload_mode":1,
"check_resid":1,
"check_resid_TV":"",
"join_alias":"",
"has_jointable":"yes",
"getlistwhere":"",
"joins":"",
"hooksnippets":"",
"cmpmaincaption":"TF TSS",
"cmptabcaption":"TF TSS",
"cmptabdescription":"",
"cmptabcontroller":"",
"winbuttons":"",
"onsubmitsuccess":"",
"submitparams":""
},
"columns":[
{
  "MIGX_id":6,
  "header":"id",
  "dataIndex":"id",
  "width":"",
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":""
},
{
  "MIGX_id":1,
  "header":"Region",
  "dataIndex":"region",
  "width":"",
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":""
},
{
  "MIGX_id":2,
  "header":"State",
  "dataIndex":"state",
  "width":"",
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":""
},
{
  "MIGX_id":3,
  "header":"Area",
  "dataIndex":"area",
  "width":"",
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":""
},
{
  "MIGX_id":4,
  "header":"Shell Lrd",
  "dataIndex":"shellLrd",
  "width":"",
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":""
},
{
  "MIGX_id":5,
  "header":"FE Maxis Lrd",
  "dataIndex":"feMaxisLrd",
  "width":"",
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":""
}
],
"category":""
}

Below second exported config (where files are uploaded)

{
"formtabs":[
{
  "MIGX_id":2,
  "caption":"File",
  "print_before_tabs":"0",
  "fields":[
    {
      "MIGX_id":48,
      "field":"title",
      "caption":"Title",
      "description":"",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":"",
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":1
    },
    {
      "MIGX_id":49,
      "field":"file",
      "caption":"Document",
      "description":"Select\/Upload a Document",
      "description_is_code":"0",
      "inputTV":"",
      "inputTVtype":"image",
      "validation":"",
      "configs":"",
      "restrictive_condition":"",
      "display":"",
      "sourceFrom":"config",
      "sources":[
        {
          "MIGX_id":1,
          "context":"mgr",
          "sourceid":2
        },
        {
          "MIGX_id":2,
          "context":"web",
          "sourceid":2
        }
      ],
      "inputOptionValues":"",
      "default":"",
      "useDefaultIfEmpty":"0",
      "pos":2
    }
  ],
  "pos":1
}
],
"contextmenus":"",
"actionbuttons":"",
"columnbuttons":"",
"filters":"",
"extended":{
"migx_add":"Add File",
"disable_add_item":"",
"add_items_directly":"",
"formcaption":"File",
"update_win_title":"",
"win_id":"survey_files",
"maxRecords":"",
"addNewItemAt":"bottom",
"media_source_id":2,
"multiple_formtabs":"",
"multiple_formtabs_label":"",
"multiple_formtabs_field":"",
"multiple_formtabs_optionstext":"",
"multiple_formtabs_optionsvalue":"",
"actionbuttonsperrow":4,
"winbuttonslist":"",
"extrahandlers":"",
"filtersperrow":4,
"packageName":"",
"classname":"",
"task":"",
"getlistsort":"",
"getlistsortdir":"",
"sortconfig":"",
"gridpagesize":"",
"use_custom_prefix":"0",
"prefix":"",
"grid":"",
"gridload_mode":1,
"check_resid":1,
"check_resid_TV":"",
"join_alias":"",
"has_jointable":"yes",
"getlistwhere":"",
"joins":"",
"hooksnippets":{
  "aftersave":"hookComEmail"
},
"cmpmaincaption":"",
"cmptabcaption":"",
"cmptabdescription":"",
"cmptabcontroller":"",
"winbuttons":"",
"onsubmitsuccess":"",
"submitparams":""
},
"columns":[
{
  "MIGX_id":1,
  "header":"Title",
  "dataIndex":"title",
  "width":20,
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":"this.textEditor"
},
{
  "MIGX_id":2,
  "header":"Document",
  "dataIndex":"file",
  "width":20,
  "sortable":"false",
  "show_in_grid":1,
  "customrenderer":"",
  "renderer":"this.renderImage",
  "clickaction":"",
  "selectorconfig":"",
  "renderchunktpl":"",
  "renderoptions":"",
  "editor":""
}
],
"category":""
}

Your second config survey_files is just a migx configuration, not a migxdb. So you can’t use the aftersave-hook here.

You would have to call it in your main config tfsurvey, but I am not sure how you could determine here if something was added to the nested migx-variable.

Hi halftrainedharry,
Thank you for the explanation, I was able to export the configuration by following your instruction

Aaaahhh ok, that explains why it is not working then. Maybe there could be a work around of some sort, a different method to generate an email notification? :thinking:

Like @halftrainedharry said, you could have the aftersave - hooksnippet at the main - config.
have a (hidden) field with the old images-items as json and compare the new images-items with the old one in your hooksnippet. If they are different, you could send the mail

Hi Bruno,

That sounds simple, but I’ve been scratching my head for 4 hours trying to work out how to do this.

Would I have to create this new ‘hidden’ field in the snippet by calling the old images-items as json markers and write some code to compare a difference which then sends an email to the person who uploaded the document?

Just wondering how to differentiate who uploaded the document if it is just comparing differences in the docs uploaded?

Actually, I have no idea how to create this, any guidance or examples would be appreciated.

In your main config tfsurvey add a new field with Fieldname files_original and standard-type text (You can hide it later when everything works).

Now you have to add an aftergetfields-hook to set the value of this new field. Create a new snippet aftergetfields_files with this code

<?php
if ($object) {
    $record_fields = $object->get('record_fields');
    $record_fields['files_original'] = $record_fields['files']; //copy the content of the field 'files' to the field 'files_original'
    $object->set('record_fields',$record_fields);
}

then append the ‘Hook Snippets’ in your config tfsurvey

{"aftersave":"hookComEmail","aftergetfields":"aftergetfields_files"}

In your snippet hookComEmail put this code

<?php
$modx->log(modX::LOG_LEVEL_ERROR,'aftersave-hook called');

$properties = $modx->getOption('scriptProperties',$scriptProperties,array());
$data = $modx->fromJson($properties['data']);
if ($data['files'] == $data['files_original']){
    $modx->log(modX::LOG_LEVEL_ERROR,'no changes!');
} else {
    $modx->log(modX::LOG_LEVEL_ERROR,'files has changed!');
}

Now if you change something in the files-migx-variable it should log the text ‘files has changed!’

1 Like

sounds good to me, @halftrainedharry

Hi halftrainedharry,

It did, this is what I got in the log:

    [2020-07-07 15:08:50] (ERROR @ /home/website/public_html/core/cache/includes/elements/modsnippet/66.include.cache.php : 2) aftersave-hook called
[2020-07-07 15:08:50] (ERROR @ /home/website/public_html/core/cache/includes/elements/modsnippet/66.include.cache.php : 9) files has changed!
[2020-07-07 15:08:50] (ERROR @ /home/website/public_html/core/cache/includes/elements/modsnippet/66.include.cache.php : 15) email message is: Hi, a report has been uploaded: Team Soon with email michael@website.com

But the email did not send. This is the code in hookComEmail:

$modx->log(modX::LOG_LEVEL_ERROR,'aftersave-hook called');

$properties = $modx->getOption('scriptProperties',$scriptProperties,array());
$data = $modx->fromJson($properties['data']);
if ($data['files'] == $data['files_original']){
$modx->log(modX::LOG_LEVEL_ERROR,'no changes!');
} else {
$modx->log(modX::LOG_LEVEL_ERROR,'files has changed!');
}
$object = & $scriptProperties['object'];
$contractor = $object->get('contractor');
$email = $object->get('mailadd');
$message = 'Hi, a report has been uploaded: ' . $contractor . ' with email ' . $email . '.';
$modx->log(modX::LOG_LEVEL_ERROR,'email message is: '.$message);

Well,it recognised the file change so the next challenge is actually sending the email.

I made an assumption to leave the email code in the snippet, was this correct?
Or is there another step to get it to send an email?