This topic is locked
[SOLVED]

 Email Forwarding

2/12/2012 8:54:34 AM
PHPRunner General questions
A
ant author

Hi
I am trying to forward updated todo items to a consultant email record there are 02 tables involved:

  1. consultant_info (linked via consultant_ID)
  2. todo (linked via ConsultantID)
    When a record is added in the todo with a specific (ConsultantID)and the form is submitted there should be an email sent to the Consultant that is included in the todo item with the content of the todo. The link ield in consultant info is ConsultantID both these tables are linked to consult (ID) which has the consultant name.
    The Email address to which the email is sent should be picked from the consultant_info table with the help of the consultant_ID field.
    I have tried editing the email event in the phprunner (after record added) but I am not sure what to do.
    I am not a PHP expert just a beginer and trying to learn as I go along.
    Table for consult_info

    CREATE TABLE IF NOT EXISTS consult info (

    ID int(11) NOT NULL AUTO_INCREMENT,

    ConsultantID int(11) DEFAULT NULL,

    email varchar(255) NOT NULL,

    UNIQUE KEY ID (ID),

    KEY ID_2 (ID)
    Table for todo

    CREATE TABLE IF NOT EXISTS todo (

    ID int(11) NOT NULL AUTO_INCREMENT,

    consultant_ID int(11) DEFAULT NULL,

    Priority int(11) DEFAULT NULL,

    Complete tinyint(1) DEFAULT '0',

    Date datetime DEFAULT NULL,

    Task text,

    PRIMARY KEY (ID),

    KEY Hotel_ID (consultant_ID)

    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=32
    Below is a copy of my attempt to edit the email event.
    /** Send email with new data ****
    global $conn;

    $str = "select email from consultant_info where ConsultantID=".$values["ConsultantID"];

    $rs = db_query($str,$conn);

    $data = db_fetch_array($rs);

    $email = $data["email"];
    foreach($values as $field=>$value)

    {

    if(!IsBinaryType(GetFieldType($field)))

    $msg.= $field." : ".$value."\r\n";

    }
    $ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from));

    if(!$ret["mailed"])

    echo $ret["message"];

A
ant author 2/12/2012

I've also tried adding this code to the event but I get a "Error description Undefined variable: where"


global $conn,$strTableName;

$strTableName="todo";

$email=$values["consult_info"];

$message="";

$subject="Sample subject";

$rs = db_query("select
from " . $strTableName ." where ". $where,$conn);

if($data=db_fetch_array($rs))

{

foreach($data as $field=>$value)

$message.= $field." : ".$value."\r\n";

runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $message));

}
Its got something to do with line 41;

$rs = db_query("select
from " . $strTableName ." where ". $where,$conn);

C
copper21 2/16/2012



I've also tried adding this code to the event but I get a "Error description Undefined variable: where"


global $conn,$strTableName;

$strTableName="todo";

$email=$values["consult_info"];

$message="";

$subject="Sample subject";

$rs = db_query("select
from " . $strTableName ." where ". $where,$conn);

if($data=db_fetch_array($rs))

{

foreach($data as $field=>$value)

$message.= $field." : ".$value."\r\n";

runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $message));

}
Its got something to do with line 41;

$rs = db_query("select
from " . $strTableName ." where ". $where,$conn);


What table did you put this event on?

A
ant author 2/16/2012

Hi Brian
Thanks for responding.
I added onto the todo table and tried under these to events;
Before record added

//** Send email with new data ****
After record added

// Place event code here.
I need the todo table to look up who the consultant is from consult info table and then send the todo item to the email for that consultant.
At the moment I have the email event set which works but send a email to everyone;

$email="everyone@test.com";

$from="admin@test.com";

$msg="";

$subject="New ToDo item";
foreach($values as $field=>$value)

{

if(!IsBinaryType(GetFieldType($field)))

$msg.= $field." : ".$value."\r\n";

}
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from));

if(!$ret["mailed"])

echo $ret["message"];

C
copper21 2/16/2012



Hi Brian
Thanks for responding.
I added onto the todo table and tried under these to events;
Before record added

//** Send email with new data ****
After record added

// Place event code here.
I need the todo table to look up who the consultant is from consult info table and then send the todo item to the email for that consultant.
At the moment I have the email event set which works but send a email to everyone;

$email="everyone@test.com";

$from="admin@test.com";

$msg="";

$subject="New ToDo item";
foreach($values as $field=>$value)

{

if(!IsBinaryType(GetFieldType($field)))

$msg.= $field." : ".$value."\r\n";

}
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from));

if(!$ret["mailed"])

echo $ret["message"];


I think I have done something similar to what you want to do. You have to link the current table, (todo), to the cunsult_info table. So if the cunsultant_id on the current table, todo, is the same as cunsultantid on the cunsult_info table, it will grab the email, or any field, from the cunsult_info table. It grabs all of the fields with the use of ""; allowing you to know also use any field from the cunsult_info table in the email as a "data" value. I am pretty sure that the "values" fields allow you to use the current table's data in the email. I am new to this as well and learning, so hopefully this works for you:
Put this in the "After Record Added" event.
//Get email from Cunsult Info Table
global $conn;

$strSQLSelect = "SELECT
FROM cunsult_info WHERE ConsultantID='".$values["Consultant_ID"]."'";

$rsSelect = db_query($strSQLSelect,$conn);

$data=db_fetch_array($rsSelect);
//** Send email with new data ****
$email=$data["email"];

$from="admin@test.com";

$msg="";

$subject="New data record";
foreach($values as $field=>$value)

{

if(!IsBinaryType(GetFieldType($field)))

$msg.= $field." : ".$value."\r\n";

}
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from));

if(!$ret["mailed"])

echo $ret["message"];
Hope this helped,
Brian

A
ant author 2/17/2012

Hi Brian
I really appreciate the help as I been racking my brain around this for a long time. I had tried a similar code before which I got from the forum but get the same problem when I tried your suggestion.
Invalid address:
You must provide at least one recipient email address.


I also had a problem with the table consult info because I was stupid when I created it with a space in the name. I think the correct way to name the table is consult info as apposed to consult_info as the second option gives me a PHP error saying it doesn't find it.
In the Querry designer for the todo table I joined the 2 fields as;

INNER JOIN consult info ON todo.consultant_ID = consult info.ConsultantID
but somehow its not picking up $email=$data["email"];
I don't know if this has anything to do with the fact that there are 3 tables involved but all are linked via consultant.
consltant table (which has only consultant name)

ID is primary key (links other tables)
consltant info table (which has consultant info)

ConsultantID links to the consultant table
todo table (which has todo items)

consultant_ID links to the consultant table
the consultant_ID should equal ConsultantID so it should pick it up in the querry
Its very confusing and frustrating I will really appreciate any help in sorting this out.
Thanks again for the assistance..

C
copper21 2/17/2012

The only thing that i can think of is try putting back tick marks on the table name in the query:
$str = "select email from consultant_info where ConsultantID=".$values["Consultant_ID"];
I also searched and found this: http://stackoverflow.com/questions/506826/selecting-a-database-in-mysql-with-spaces-in-its-name
Brian

A
ant author 2/18/2012

Hi Brian
Thank you very much for the assistance, finally got it working. I removed the space from the table name consult info which could have been causing problems but also I had mixed up the field definition with CAPS C and small c in the field definition.
$strSQLSelect = "SELECT FROM consultinfo WHERE ConsultantID='".$values["Consultant_ID"]."'";*
Below is the final code (same as what Brian gave me) if someone is having the same problem. I find it doesn't matter if I add the coded on before or after record added.
A TIP for anyone trying to remove the tags from the email message is to change $subject, 'body' to $subject, 'htmlbody'
Once again thanks for the help, now I can sleep again...
//Get email from Cunsult Info Table
//****
Send email with new data ****

global $conn;

$strSQLSelect = "SELECT * FROM consultinfo WHERE ConsultantID='".$values["consultant_ID"]."'";

$rsSelect = db_query($strSQLSelect,$conn);

$data=db_fetch_array($rsSelect);
//** Send email with new data ****
$email=$data["email"];

$from="admin@test.com";

$msg="";

$subject="New ToDo item";
foreach($values as $field=>$value)

{

if(!IsBinaryType(GetFieldType($field)))

$msg.= $field." : ".$value."\r\n";

}
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $msg, 'from'=>$from));

if(!$ret["mailed"])

echo $ret["message"];**

C
copper21 2/18/2012

Glad to hear you finally got it working...it took me a bit to get that one too!