Sending an Email Notification from a batch_action

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
lawrencekeru
Posts: 29
Joined: 2016-06-24 02:51

Sending an Email Notification from a batch_action

Post by lawrencekeru » 2016-07-06 12:30

I want to send a rent reminder email to tenants. I hava a batch action as below
I have followed https://bigprof.com/appgini/help/advanc ... ch-actions

Code: Select all

function applicants_and_tenants_batch_actions(&$args){

		return array(
				array(
						'title' => 'Send Rent Reminders',
						'function' => 'send_rent_reminder',
						'icon' => 'calendar'
				)
		);
	}
Then i have a file called applicants_and_tenants-tv.js with the following code.

Code: Select all

function send_rent_reminder(table_name, ids){	
	// ******** Send email if checkbox is checked *********
	$recipient = $data['emailto'];
	$CC = "";
	$sendemail=sqlValue("SELECT first_name, last_name, late_rent_reminder_date, emailto, balance FROM `applicants_and_tenants` JOIN `residence_and_rental_history` ON `applicants_and_tenants`.`id` = `residence_and_rental_history`.`tenant` WHERE `applicants_and_tenants`.`id`='{$data['selectedID']}'");
	if($sendemail!=0){

	$resetMail=@mail(
	// To
	$recipient,

	// Subject
	"Rent Reminder Date",

	// Content
	"Just add whatever content and fields you want to the email.\n" .
	"Account No.: {$data['contract']}\n" .
	"Name: {$data['name']}\n" .
	"Our Job No.: {$data['id']}\n" .
	"Current Job Status: {$data['status']}\n",

	// From
	"From: [email protected]"
	);
	// then update the send email checkbox back to default of 0
	if(resetMail){
	sql("update jobs set emailadvice=0 where ourref='{$data['selectedID']}'", $abc);
	}
	}
	// ******** End send email if checkbox is checked *******	
}
But the Batch action is not responding even if i try to do a var_dump to test my sql query. How can i go about this? Thank you.

Aya Adel
Posts: 7
Joined: 2016-08-08 07:29

Re: Sending an Email Notification from a batch_action

Post by Aya Adel » 2016-08-08 10:39

The error is most probably caused by writing php code in JavaScript, as PHP ​​run​​s​ ​on the ​server-side​,​ while JavaScript runs on client side only. What you can do is to write the PHP code in a file, and call it using JavaScript.

This link shows an example on how to solve this issue:
https://bigprof.com/appgini/help/advanc ... ch-actions

The JavaScript mainly calls the URL of the PHP file, which ​r​​​un​s the query.

Here is an example of a JavaScript file ​

Code: Select all

function print_mail_labels(table_name, ids){
	/* 
	  we'll open the mail labels page in a new window
	  that page is a server-side PHP script named mail-labels.php
	  but first, let's prepare the parameters to send to that script
	*/
	var url = 'mail-labels.php?table=' + table_name;
	for(var i = 0; i < ids.length; i++){
		url = url + '&' 
			+ encodeURI('ids[]') + '=' 
			+ encodeURIComponent(ids[i]);
	}
	
	window.open(url);
}
And an example of a PHP file

Code: Select all

<?php
	/*
	  Including the following files allows us to use many shortcut
	  functions provided by AppGini. Here, we'll be using the
	  following functions:
	    makeSafe()
			protect against malicious SQL injection attacks
		sql()
			connect to the database and execute a SQL query
		db_fetch_assoc()
			same as PHP built-in mysqli_fetch_assoc() function
	*/
	$curr_dir = dirname(__FILE__);
	include("{$curr_dir}/defaultLang.php");
	include("{$curr_dir}/language.php");
	include("{$curr_dir}/lib.php");
	
	/* receive calling parameters */
	$table = $_REQUEST['table'];
	$ids = $_REQUEST['ids']; /* this is an array of IDs */
	
	/* a comma-separated list of IDs to use in the query */
	$cs_ids = '';
	foreach($ids as $id){
		$cs_ids .= "'" . makeSafe($id) . "',";
	}
	$cs_ids = substr($cs_ids, 0, -1); /* remove last comma */
	
	/* retrieve the records and display mail labels */
	$res = sql( "select * from customers " .
				"where CustomerID in ({$cs_ids})", $eo);
	while($row = db_fetch_assoc($res)){
		?>
		<b><?php echo $row['CompanyName']; ?></b><br>
		<i>C/O <?php echo $row['ContactName']; ?></i><br>
		<?php echo $row['Address']; ?><br>
		<?php echo $row['City']; ?><br>
		<?php echo $row['Region']; ?>
		<?php echo $row['PostalCode']; ?><br>
		<?php echo $row['Country']; ?><br>
		<br>
		<br>
		<hr>
		<?php
	}

Post Reply