problem email send on field value change

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
mavel
User
Posts: 88

problem email send on field value change

Post 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.


mobhar
User
Posts: 11703

Post by mobhar »

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

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


mavel
User
Posts: 88

Post 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;
 }

mobhar
User
Posts: 11703

Post 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.


arbei
User
Posts: 9359

Post 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.


mavel
User
Posts: 88

Post 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


mobhar
User
Posts: 11703

Post 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.


mavel
User
Posts: 88

Post 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.


mobhar
User
Posts: 11703

Post by mobhar »

mavel wrote:

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

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


mavel
User
Posts: 88

Post by mavel »

mobhar wrote:

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

I tried this.
no error message, no mail.


arbei
User
Posts: 9359

Post 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();

mavel
User
Posts: 88

Post by mavel »

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


mobhar
User
Posts: 11703

Post 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.


mavel
User
Posts: 88

Post 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.


mobhar
User
Posts: 11703

Post 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;
 }

mavel
User
Posts: 88

Post by mavel »

it run excellent, thank you.


ahad
User
Posts: 22

Post by ahad »

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


Post Reply