Page 1 of 1

problem email send on field value change

Posted: Mon Dec 28, 2020 2:17 am
by mavel

Hi all,

I'm trying to send mail when the field value is updated.

i wrote this code in "Email_Sending":

public function emailSending($email, &$args) {

if (CurrentPageID() == "add" || CurrentPageID() == "edit") {
    		
			if ($args["rsnew"]["status"] == "1") { // when the value is 1    
    		 	$email->Format = "HTML";
    		 	$email->Charset = "UTF-8";
    		 	$email->Sender .= CurrentUserInfo("email");
    			$email->Recipient = "user@domain.com";
    			$email->Subject = "lorem ipsum";
    			$email->Content .= "\nlorem ipsum" . CurrentPageUrl(); 
    		}
			
    		if ($args["rsnew"]["status"] == "2") { // when the value is 2 
    		 	$email->Format = "HTML";
    		 	$email->Charset = "UTF-8";
    		 	$email->Sender .= CurrentUserInfo("email");
    			$email->Recipient = "user@domain.com";
    			$email->Subject = "lorem ipsum";
    			$email->Content .= "\nlorem ipsum" . CurrentPageUrl(); 
    		}
    	}
    	return true;
    }

Can you help me? I did some mistake ?
Thank you.


Re: problem email send on field value change

Posted: Mon Dec 28, 2020 8:01 am
by mobhar

Change this code:
$email->Sender .= CurrentUserInfo("email");

to:
$email->Sender = CurrentUserInfo("email");


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 1:24 am
by mavel

All email events in the project are working fine. on add, on edit, when email export etc.

But I can't run this code.
When the value of the Status field in my table changes, I want an automatic mail sent to the relevant people when it is 1 or 2.

Where is there an error in this code?

// Email Sending event
function Email_Sending($email, &$args) 
{
	if (CurrentPageID() == "add" || CurrentPageID() == "edit") {  
    		
		if ($args["rsnew"]["Status"] == "1") { // when the value is 1    
    		 	$email->Sender = CurrentUserInfo("email");
    			$email->Recipient = "user@domain.com";
    			$email->Subject = "status one";
    			$email->Content .= "lorem ipsum" . CurrentPageUrl(); 
    		}

    		if ($args["rsnew"]["Status"] == "2") { // when the value is 2 
    		 	$email->Sender = CurrentUserInfo("email");
    			$email->Recipient = "user@domain.com";
    			$email->Subject = "status two";
    			$email->Content .= "lorem ipsum" . CurrentPageUrl(); 
    		}
    	}
    	return true;
 }

Re: problem email send on field value change

Posted: Tue Dec 29, 2020 9:20 am
by mobhar

mavel wrote:

But I can't run this code.

Please explain it in more detail.

  1. Why can't you run that code?
  2. Did you enable Debug mode?
  3. Did you see any error message?

Please post any error message for more discussion.


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 9:22 am
by arbei

If the email cannot be sent successfully, you should see failure message. If you don't see it, enable Debug and try again.


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 3:54 pm
by mavel

Hi,

show error and debug features are enable in the project. I only see sql queries in the debug section. no function output or error details. There is also no error shown on the page.

I am working on localhost. other mail features fully work. (mail export, on add, on edit etc.)

  • I have a table named orders.
  • I have a field named Status in my table
  • Field properties: field type varchar 255 and edit tag is select (value 0, label = cancelled, value 1, label delivered etc.)
  • when the field value changes, mail will be sent to the relevant people with the above coding

I put the code here: Server Events> Table Specific> Common> Email_Sending


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 4:03 pm
by mobhar

Make sure CurrentUserInfo("email") returns a valid email.

In addition, make sure the email address that assigned to Recipient property that belongs to $email object is already valid, too.


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 4:53 pm
by mavel

$email->Recipient = "user@domain.com";

This recipient is also the recipient defined in PHP> Email Settings in the project.
He is already receiving mail in other mail events in the application.
I have the "Mail" field in my Employees table.
The user logged in to the application has an e-mail account.


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 5:38 pm
by mobhar

mavel wrote:

I have the "Mail" field in my Employees table.

Then it should be CurrentUserInfo("Mail") instead of CurrentUserInfo("email").


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 5:51 pm
by mavel

mobhar wrote:

Then it should be CurrentUserInfo("Mail") instead of CurrentUserInfo("email").

I tried this.
no error message, no mail.


Re: problem email send on field value change

Posted: Tue Dec 29, 2020 6:00 pm
by arbei

You may debug by adding the following lines at the end of your server event (before return true;) and check the property values, e.g.

var_dump($email);
die();

Re: problem email send on field value change

Posted: Tue Dec 29, 2020 7:37 pm
by mavel

I did.
There is nothing about mail events in debug. there are only sql queries.


Re: problem email send on field value change

Posted: Wed Dec 30, 2020 8:57 am
by mobhar

Have you already tried to add or edit a record in order to trigger Email_Sending server event? If so, then you should see the output of var_dump($email); code.


Re: problem email send on field value change

Posted: Sat Jan 02, 2021 4:08 pm
by mavel

I found my mistake.
When I checked "on add, on edit" in the table options section, it started sending mail.

I have two problems now.

My 1st problem is I cannot show the email address of the employee registered as the sender.
$ email-> Sender = CurrentUserInfo ("Email"); this code doesn't work. The sender part of the incoming mail is blank.

My second problem is that it continues to send e-mails even if the data is not changed on the edit page. When I change the data value of the status field to 1 as in the code line below, the mail comes. but when I open the edit page again and save it without any changes, the mail comes again. how can i stop this

if ($ args ["rsnew"] ["status"] == "1") {// when the value is 1

thanks.


Re: problem email send on field value change

Posted: Sat Jan 02, 2021 5:26 pm
by mobhar

Instead of using CurrentUserInfo ("Email"); to return current email of logged-in user, then you may simply use ExecuteScalar() global function; see Some Global Functions sub-topic under Server Events and Client Scripts topic from PHPMaker Help menu for more info and example.

To prevent system send email in Email_Sending server event, then you need to write return false; for your desired logic, for example, adjust your code to:

// Email Sending event
function Email_Sending($email, &$args) 
{
	if (CurrentPageID() == "add" || CurrentPageID() == "edit") {  
    		if ($args["rsnew"]["Status"] == "1") { // when the value is 1    
    		 	$email->Sender = CurrentUserInfo("email");
    			$email->Recipient = "user@domain.com";
    			$email->Subject = "status one";
    			$email->Content .= "lorem ipsum" . CurrentPageUrl(); 
    		} elseif ($args["rsnew"]["Status"] == "2") { // when the value is 2 
    		 	$email->Sender = CurrentUserInfo("email");
    			$email->Recipient = "user@domain.com";
    			$email->Subject = "status two";
    			$email->Content .= "lorem ipsum" . CurrentPageUrl(); 
    		} else {
    			return false; // do not send email for condition other than two conditions above
    		} 
    	}
    	return true;
 }

Re: problem email send on field value change

Posted: Mon Jan 04, 2021 12:46 am
by mavel

it run excellent, thank you.


Re: problem email send on field value change

Posted: Tue Dec 06, 2022 9:18 pm
by ahad

This discussion solved my problem, Loved all in my heart.