This topic is locked

Select Recipient of Simple Email

3/20/2006 10:38:13 AM
PHPRunner General questions
T
Tommy B author

I'm trying to create a "time-off request" application. We have several departments, so new time off request notifications are sent to the head of the users department. I'm trying to figure our how to do this using the event feature. I could send a simple email fine, but I don't know how to fix it so that the notification of a new time-off request is sent to the user's supervisor instead of what is entered in the event configuration.
Example:
usertype is a field in the Users table.
UserA is usertype Collector.

UserA's time-off request would go to UserB which is usertype CollectorSupervisor.

UserC is usertype Biller.

UserD's time-off request would go to UserC which is usertype BillerSupervisor.
One thing I thought about was creating another field in the Users table called superemail which would store that user's supervisor's email address. But then I still don't know how to go about correctly implementing this as an event.
Any ideas? I hope I've made sense. Thanks.

Sergey Kornilov admin 3/21/2006

Tommy,
you can create new field called superemail, then add to Before record added or Before record edit events on the Events Tab some code.

See my example below:

function BeforeAdd(&$values)

{

$email=$values["superemail"];

$message="Your message";

$subject="Sample subject";

mail($email, $subject, $message);

return true;

}

T
Tommy B author 3/21/2006

Tommy,

you can create new field called superemail, then add to Before record added or Before record edit events on the Events Tab some code.

See my example below:


Would the 'superemail' field be added to the user table or the table created to store time off requests (I don't see the practicality of having it in the timeoff table)? I assume it would be the user table, but if this is the case would the code you provided correctly pull the email address from the user table or would it assume 'superemail' is part of the timeoff table?
Similarly, the user needs to be notified when their supervisor Approves/Denis the Time-Off request. Currently, each user's email address is stored in the Users table. Is there a way this field can be automatically populated into a hidden field like the username is? If so, then an event similar to the one you described above could be added for when an edit occurs and the original user could be notified that a decision has been made regarding their request.
Any ideas?

Sergey Kornilov admin 3/23/2006

Tommy,
the previous examle assumes that you have "superemail" field in "timeoff" table.
If your "superemail" and user's email fields are in the Users table, you can store them in session variables and then use them in timeoff table events.
Here is the sample code for AfterSuccessfulLogin event:

global $conn;

$sql = "select email,superemail from Users where user='".$_POST["username"]."' and pass='".$_POST["password"]."'";

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

$data=db_fetch_numarray($rs);

$_SESSION["email"] = $data[0];

$_SESSION["superemail"] = $data[1];


where enail,superemail,user, pass, Users are your actual field and table names.

And here is the code for "Before record added" event:

...

$email=$_SESSION["superemail"];

...

T
Tommy B author 3/23/2006

I've added the following code you suggested (replacing the generic table/field names with my own). However, no email is being sent. Records are added with no errors though. Any idea what may be wrong? Hopefully I'm overlooking a typo.
After Successful Login Custom Code:

function AfterSuccessfulLogin()

{

global $conn;

$sql = "select EmailAddress,superemail from Users where UserName='".$_POST["username"]."' and Password='".$_POST["password"]."'";

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

$data=db_fetch_numarray($rs);

$_SESSION["EmailAddress"] = $data[0];

$_SESSION["superemail"] = $data[1];
}


Before Record Added Custom Code

function BeforeAdd(&$values)

{

$email=$_SESSION["superemail"];

$message="Your message";

$subject="Sample subject";

mail($email, $subject, $message);

return true;

}
Sergey Kornilov admin 3/24/2006

Tommy,
code looks right. Please verify that simple email is sending.

It seems that you don't set email settings.

More info about it here:

http://www.php.net/mail

T
Tommy B author 3/24/2006

If I enter a value rather than a session varialble for the simple email, it does send. I've used this and continue to use it on tables within the same project as this one.

T
Tommy B author 3/27/2006

Sergey,
I've tried other code manipulations I've found on developer forums and php documentation, but have not been able to figure this out.
Simple emails with a static value send with no problem. When the recipient is a variable however, it does now. Any ideas?
Thanks!

Sergey Kornilov admin 3/28/2006

Tommy,
the code above will work only if you don't use Password encryption option.

Here is the code for Password encryption turned on:

...

$sql = "select EmailAddress,superemail from Users where UserName='".$_POST["username"]."' and Password='".md5($_POST["password"])."'";

...

Anyway you should ensure that this SQL command returns a correct data.

Echo $data array values to find out what exactly SQL command returns.

T
Tommy B author 3/30/2006

Tommy,

the code above will work only if you don't use Password encryption option.

Here is the code for Password encryption turned on:

Anyway you should ensure that this SQL command returns a correct data.

Echo $data array values to find out what exactly SQL command returns.


Sergey, password encryption is not turned on, and it does not work.

Sergey Kornilov admin 3/31/2006

Insert these two lines:

echo $sql;

print_r($data);

just after this line:

$data=db_fetch_numarray($rs);


Then login your pages and show me the output.

You can send it to support@xlinesoft.com