MODX Community

Upon Submit Cannot Send Email Notification in MIGXdb

You are missing all the code that actually sends the email. In addition you have to move the code to the else-clause of the if-statement or it will be executed even if files doesn’t change.

$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!');
	//create email message
	$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);

	//send email
	$modx->getService('mail', 'mail.modPHPMailer');
	$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');
	if (!$modx->mail->send()) {
		$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$err);

Hi halftrainedharry,

I copied the above and just changed the mail FROM, FROM_NAME, SENDER & ‘to’ details.

The upload doc worked as normal and I got the following errors in the error log:

[2020-07-07 16:37:02] (ERROR @ /home/website/public_html/core/cache/includes/elements/modsnippet/66.include.cache.php : 2) aftersave-hook called
[2020-07-07 16:37:02] (ERROR @ /home/website/public_html/core/cache/includes/elements/modsnippet/66.include.cache.php : 9) files has changed!
[2020-07-07 16:37:02] (ERROR @ /home/website/public_html/core/cache/includes/elements/modsnippet/66.include.cache.php : 16) email message is: Hi, a report has been uploaded: Team Soon with email
[2020-07-07 16:37:02] (ERROR @ /home/website/public_html/core/cache/includes/elements/modsnippet/66.include.cache.php : 28) An error occurred while trying to send the email: 

But no email was received (i did input a “from” server authorised email address and a “to” address to a legitimate email account)

The “from” address is the same as in the MODX manager settings.

I’m thinking the last error is the one I need to investigate. Is this where Bob Rays earlier mentioned extra may come in handy?

Not quite sure, but I think the problem is the line MAIL_SENDER. Set it to the same value as MAIL_FROM.
Or use this (reads the values from the system settings):


@halftrainedharry @bruno17

That worked like a charm. Email received and no untoward errors in the log :smile:
Thank you so much for all your input.

The only dilemma I’m left with now is which post I should mark up as the definitive answer :thinking:

There were great suggestions in many posts that ultimately led to resolving a very interesting (well for me at least) issue.

Is it possible to mark multiple posts as the answer?