Send email on Record Approval?

DebbieS

DaDaBIK Guru
I've not worked with the sending email part of the script until now. Users are looking for an approval system (on click, record is moved from pending table to the approved table) which I've got working great. Problem is sending email when that record is moved.

I tried to use the built-in email functions to send email when the record was sent to the approved table, but that doesn't work (wishful thinking for such a simple solution).

So, if anyone has gotten an approval setup working which also sends an email out, I'd really appreciate the help.

Thanks!

 

billthecat

Well-known member
I'm not sure what method you're using to move records from the pending table to the approved, but why not just insert a send mail into the move function?

$to = 'yourmail@domain.com';
$subject = 'Record Moved';
$message = 'Record moved from pending to approved table';
$headers = 'From: dadabik@domain.com' . "\r\n";

mail($to, $subject, $message, $headers);

 

DebbieS

DaDaBIK Guru
Thanks billthecat!

Since I wrote my post ... I've managed to get the built-in mail functions of the app working. The only thing I'm trying to get working now is to have the app get the email address from the "Email" field of the record being moved (approved) and include that as a cc address so the originator gets notified of the approval.

So far, I've been trying to incorporate the code from http://www.dadabik.org/forum/read.php?f=1&i=4865&t=3579#reply_4865 to get the email address from the db (just the get email address part) with no luck. Still playing with it though ... will update if I figure something out.

 

DebbieS

DaDaBIK Guru
OK ... another update ... I've managed to get something out of the db record by using ...

$sql1 = "SELECT Email from ".$quote.$table_name.$quote." WHERE ".$quote.$where_field.$quote." = '".$where_value."' LIMIT 1";
$approve_notice_email_cc_recipients_ar[1] = execute_db($sql1, $conn);

The problem is, the data returned is not an email address but this:

Object.id.#4@<serveraddress>

Don't understand why it is returning that instead of just the email address value from the db table. Anyone have any ideas?

 

DebbieS

DaDaBIK Guru
Update ... Regardless of what field's data I try to extract from the database and place into the CC field, all I get is "Object.id.#...". It is not getting the actual data from the record's field.

I tried different fields and tried changing the email field to text/alphanum instead of text/email and made no diff -- only "Object.id.#..." is returned.

 

DebbieS

DaDaBIK Guru
Eureka! I got it working!

I'm using the code from the insert record email notification copied into my new function for move_record (see this post (http://www.dadabik.org/forum/read.php?f=1&i=4841&t=4126#reply_4841)). In the email notice code, I've changed the insert_notice.... variables to read "approve_notice...." and added the appropriate variables to the config.php for the installation.

Fixed email addresses are included in the config.php file as normal, however, to include the email address of the originator in the record itself, I did this:

Email notificition code must be placed BEFORE the call to the move_record function inside the case "move" in index.php. In that code, just after the line that reads "$res_details = execute_db($sql, $conn);", I added this to grab the value from the email address field:

$row = fetch_row_db ($res_details);
$emailcc = $row['Email'];
$approve_notice_email_cc_recipients_ar[1] = $emailcc;


Below are code snippets from my install. Note that movetotbl and movetext are variables I've added to my installation so the code can be re-used with other DBs (I'm using one DaDaBIK install to manage all my DBs.

My move record function in business_logic.php looks like this:

function move_record ($movetotbl, $table_name, $where_field, $where_value)
// goal: delete one record
{
global $conn, $quote, $db_name;//INSERT INTO table_b SELECT * FROM table_a WHERE
$sql = "INSERT INTO ".$movetotbl." SELECT * FROM ".$quote.$table_name.$quote." WHERE ".$quote.$where_field.$quote." = '".$where_value."' LIMIT 1";
display_sql($sql);

// execute the select query
$res_contacts = execute_db($sql, $conn);

$sql1 = "DELETE FROM ".$quote.$table_name.$quote." WHERE ".$quote.$where_field.$quote." = '".$where_value."'";
display_sql($sql1);

// execute the select query
$res_contacts = execute_db($sql1, $conn);

} // end function move_record


index.php's case "move" looks like this:

case "move":
if ($enable_delete == "1") {

$location_url = $site_url.$dadabik_main_file.'?table_name='.urlencode($table_name).'&function=search&where_clause='.urlencode($where_clause);
if(isset($page) && isset($order) && isset($order_type)) {
$location_url .= '&page='.$page.'&order='.urlencode($order).'&order_type='.$order_type;
}
else{
$location_url .= '&page=0';
}


if( $enable_authentication === 0 || $enable_delete_authorization === 0 || current_user_is_owner($where_field, $where_value, $table_name, $fields_labels_ar)){
if ($enable_approve_notice_email_sending === 1) {

$unique_field_name = get_unique_field_db($table_name);

$last_inserted_ID = $where_value;

if (isset($unique_field_name) && $unique_field_name !== '' && isset($last_inserted_ID) && $last_inserted_ID !== false) {

$sql = build_select_part($fields_labels_ar, $table_name);

$sql .= " WHERE ".$quote.$table_name.$quote.".".$quote.$unique_field_name.$quote." = '".$last_inserted_ID."'";
$res_details = execute_db($sql, $conn);
$row = fetch_row_db ($res_details);
$emailcc = $row['Email'];
$approve_notice_email_cc_recipients_ar[1] = $emailcc;

// build the insert notice message
$approve_notice_email = build_insert_update_notice_email_record_details($fields_labels_ar, $res_details);

$to_addresses = '';
$cc_addresses = '';
$bcc_addresses = '';

foreach ($approve_notice_email_to_recipients_ar as $approve_notice_email_to_recipient){
$to_addresses .= $approve_notice_email_to_recipient.', ';
} // end foreach

$to_addresses = substr($to_addresses, 0, -2); // delete the last ', '


foreach ($approve_notice_email_cc_recipients_ar as $approve_notice_email_cc_recipient){
$cc_addresses .= $approve_notice_email_cc_recipient.', ';
} // end foreach

$cc_addresses = substr($cc_addresses, 0, -2); // delete the last ', '
foreach ($approve_notice_email_bcc_recipients_ar as $approve_notice_email_bcc_recipient){
$bcc_addresses .= $approve_notice_email_bcc_recipient.', ';
} // end foreach

$bcc_addresses = substr($bcc_addresses, 0, -2); // delete the last ', '

$additional_headers = '';

if ($cc_addresses != '') {
$additional_headers .= "Cc:".$cc_addresses."\n";
} // end if

if ($bcc_addresses != '') {
$additional_headers .= "Bcc:".$bcc_addresses."\n";
} // end if

foreach ($approve_notice_email_from_recipients_ar as $approve_notice_email_from_recipient){
$additional_headers .= "From:".$approve_notice_email_from_recipient."\n";
} // end foreach

$additional_headers .= "From:".$approve_notice_email_from_recipient;

mail($to_addresses, 'Network Condition Request '.$movetext, $approve_notice_email, $additional_headers);
} // end if
} // end if
move_record ($movetotbl, $table_name, $where_field, $where_value);
} // end if
else {
$location_url .= '&just_delete_no_authorization=1';
} // end else

header('Location: '.$location_url);

exit;
} // end if
break;

This code moves one record from one table (in my case, pending) into another table (approved) AND sends an email to assigned addresses in the config.php AND cc's the email address listed in the record being moved.

 

tmoh

New member
Is it possible that the future release of Dadabik includes approval as a built-in function?



Post Edited (08-08-07 21:44)
 
Top