This topic is locked
[SOLVED]

Send email when "Status" Is Set to "Closed"

1/3/2024 12:18:41 PM
PHPRunner General questions
D
Damell01 author

I have my email setup correctly already that is out of the way and it sends I tested that functionality. I just want to send an email after the "workorder: page is edited and the "status" field is set to "closed". What am I missing here? Here's my code

$from = "batwaremed2@gmail.com";
$email = "batwaremed2@gmail.com";

if ($values["Status"] === "Closed") {
$from = "batwaremed2@gmail.com";
$email = "batwaremed2@gmail.com";
// Construct the notification message including the Workorder ID
$message = "The status is now closed for Workorder ID: " . $values["WorkorderID"] . ". This is a notification message.";

// Define the subject for the email
$subject = "Status Closed Notification";

// Send the email
$result = mail($email, $subject, $message);

// Check if the email was sent successfully
if ($result) {
// Optionally, provide feedback to the user or log the success
echo "Notification email sent successfully.";
} else {
// Handle the case where the email failed to send
echo "Failed to send notification email.";
}
}

img alt

img alt

fhumanes 1/3/2024

Hello,

The function to send email in phprunner is: runner_mail():
https://xlinesoft.com/phprunner/docs/runner_mail_function.htm

Greetings,
fernando

D
Damell01 author 1/3/2024

So like this $from = "batwaremed2@gmail.com";
$email = "batwaremed2@gmail.com";

if ($values["Status"] === "Closed") {
$from = "batwaremed2@gmail.com";
$email = "batwaremed2@gmail.com";

// Construct the notification message including the Workorder ID
$message = "The status is now closed for Workorder ID: " . $values["WorkorderID"] . ". This is a notification message.";

// Define the subject for the email
$subject = "Status Closed Notification";

// Send the email using runner_mail
$ret = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $message, 'from' => $from));

// Check if the email was sent successfully
if (!$ret["mailed"]) {
// Handle the case where the email failed to send
echo $ret["message"];
} else {
// Optionally, provide feedback to the user or log the success
echo "Notification email sent successfully.";
}

}

D
Damell01 author 1/3/2024

This didn't work for me any insights? I am lost

fhumanes 1/4/2024

Hello,

In this link I explain how you can generate traces (debug) of your PHP code.

https://fhumanes.com/blog/guias-desarrollo/guia-34-metodo-basico-para-depuracion-codigo/

When you include, "echo" sentences you can modify the logic of the Phprunner program.

If you have problems debuging the way I explain, tell me to help you.

Greetings,
fernando

D
Damell01 author 1/4/2024

I'm not sure what to debug this is what I got

[Thu, 04 Jan 2024 09:07:39 -0500] Error 1: URL ejecutada: /login.php
[Thu, 04 Jan 2024 09:07:40 -0500] Error 1: URL ejecutada: /menu.php
[Thu, 04 Jan 2024 09:07:43 -0500] Error 1: URL ejecutada: /Workorder_list.php
[Thu, 04 Jan 2024 09:08:04 -0500] Error 1: URL ejecutada: /Workorder_list.php?orderby=aStatus
[Thu, 04 Jan 2024 09:08:27 -0500] Error 1: URL ejecutada: /Workorder_edit.php?editid1=2745
[Thu, 04 Jan 2024 09:08:43 -0500] Error 1: URL ejecutada: /Workorder_edit.php?page=edit&submit=1&editid1=2745&
[Thu, 04 Jan 2024 09:08:45 -0500] Error 1: URL ejecutada: /Workorder_list.php?a=return&
[Thu, 04 Jan 2024 09:10:27 -0500] Error 1: URL ejecutada: /login.php
[Thu, 04 Jan 2024 09:10:28 -0500] Error 1: URL ejecutada: /menu.php
[Thu, 04 Jan 2024 09:10:31 -0500] Error 1: URL ejecutada: /Workorder_list.php
[Thu, 04 Jan 2024 09:10:36 -0500] Error 1: URL ejecutada: /Workorder_list.php?orderby=aWorkorderID
[Thu, 04 Jan 2024 09:10:49 -0500] Error 1: URL ejecutada: /Workorder_list.php?orderby=aStatus
[Thu, 04 Jan 2024 09:11:09 -0500] Error 1: URL ejecutada: /Workorder_edit.php?editid1=3805
[Thu, 04 Jan 2024 09:11:10 -0500] Error 2: SQL de v_resource: SELECT
WorkorderID, Customer, DateReceived, DateRequired, MakeAndModel, SerialNumber, ProblemDescription, DateTimeStart, DateTimeCompleted, ResolutionDescription, ServiceEngineer, Machine, Parts Used, Parts Cost, Part Vender, Time Start, Time Stop, Status, work order type_ID, Hours, Name, Email, Billing/Contract, ImageTest
FROM Workorder WHERE ( ( ( ( WorkorderID=3805 ) ) ) )

[Thu, 04 Jan 2024 09:11:10 -0500] Error 2: WHERE de v_resource: ( ( WorkorderID=3805 ) )
[Thu, 04 Jan 2024 09:11:14 -0500] Error 1: URL ejecutada: /Workorder_edit.php?page=edit&submit=1&editid1=3805&
[Thu, 04 Jan 2024 09:11:15 -0500] Error 2: SQL de v_resource: SELECT
WorkorderID, Customer, DateReceived, DateRequired, MakeAndModel, SerialNumber, ProblemDescription, DateTimeStart, DateTimeCompleted, ResolutionDescription, ServiceEngineer, Machine, Parts Used, Parts Cost, Part Vender, Time Start, Time Stop, Status, work order type_ID, Hours, Name, Email, Billing/Contract, ImageTest
FROM Workorder WHERE ( ( ( ( WorkorderID=3805 ) ) ) )

[Thu, 04 Jan 2024 09:11:15 -0500] Error 2: WHERE de v_resource: ( ( WorkorderID=3805 ) )
[Thu, 04 Jan 2024 09:11:16 -0500] Error 1: URL ejecutada: /Workorder_list.php?a=return&

fhumanes 1/4/2024

Hello,
When you want to consult a variable, instead of the 'echo' statement you have to execute 'custom_error'.
Regards,

admin 1/5/2024

I would like to provide what I may call a scientific or systematic approach to troubleshooting.

  1. We need to make sure that this code is executed at all
  2. We need to figure out which part of the code doesn't work.

Why this event may not be executed at all? I can see that BeforeEdit event is marked as bold which means there is some code in there. If you do something wrong in BeforeEdit event then AfterEdit event would not be executed at all.

So, to make sure that this code runs at all, I would add the following in the very beginning of the event:
echo "AfterEdit event";

Run this page and lets us know what exactly happens after you click Save. Depending on what happens, we'll pick up the next troubleshooting step.

Note: this approach applies to the regular Edit page. If you use a page in popup or inline edit or selected to redirect the user back to the LIst page after the record was edited - then this code might need to be adjusted.

D
Damell01 author 1/6/2024

I ended up figuring it out. I took over this project for a company on the side and have been adding new features for them. Turns out the "Status" value is actually referring to a "StatusID" number and isn't technically called "closed" it's actually "2"