Page 1 of 1

email query

Posted: Thu Oct 26, 2006 8:51 pm
by misslilbit02
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, 

This is just hard to explain. I'm coding a help desk. I have right now 4 different email meassages in my database. I would like to send out an email message automatically based on what the user does. For instance, If a new user would like to sign up I want to send out the email message in the database name user-new user signup. 

So my thoughts were to include the variables from the form being submitted in the email messages that are located in the database. Pull the message out of the database and mail the message using PHPmail function in the processing script file. 

That doesn't work. The delimna is to get the information from the form and put it in the email that's going to be sent out to the user. 

If you can think of a better way to do this your help is greatly appreciated. Some code is below. 

This is one of the messages in the database:

Code: Select all

<? 
mssql_query("INSERT INTO email(email_name, subject, email) VALUES ('User-New User Signup', 'Keiser Support - User Information', 
'Thank you for signing up with Keiser Support. Please review your user information below: <br><br> 
User Information: <br> 
-----------------------------------<br> 
User Name:'$e_user'<br> 
First Name:'$first'<br> 
Last Name:'$last'<br> 
Email Address:'$email'<br> 
Location:'$loc'<br> 
Password:'$newpass'<br><br> 

If any of your user information is incorrect you can log into Keiser Support and edit your account.<br> 
Please do not respond to this email this is an unmonitored email address. Keep this information for your records.<br><br> 
Regards,<br> 
Keiser Support Team<br>')"); 

mssql_close($connect); 
?>
This is the processing script for the new user page:

Code: Select all

<?include('config.php');?> 
<?php 
session_start(); 
//Next...Moving on to connecting to the database 
$connect=mssql_connect($server, $username, $password); 
mssql_select_db($db, $connect); 

//assigning variables 

$user=$_POST['name']; 
$first=$_POST['fname']; 
$last=$_POST['lname']; 
$email=$_POST['email']; 
$ph=$_POST['phone']; 
$loc=$_POST['location']; 
$newpass=$_POST['password']; 
$connew=$_POST['con_pass']; 


//error messages 
if(strlen($user) == 0) 
{ 
header("Location: http://webdemo/newusernameerr.html"); 
} 
if(strlen($email) == 0) 
{ 
header("Location: http://webdemo/newuseremailerr.html"); 
} 
if(strlen($user)== 0 && strlen($email)==0) 
{ 
header("Location: http://webdemo/newusercomberror.html"); 
} 
//querying database 
$sql="SELECT username 
FROM info 
WHERE username = '$user'"; 

$result = mssql_query($sql) 
or die('result not added'); 
//inserting name, email, password, and location into the database if it's not already there 
if(!mssql_num_rows($result) == $user) 
{ 
//insert values into the database 
$query = "INSERT INTO info (username, password, firstname, lastname, email, phone, location) VALUES ('$user', '$newpass', '$first', '$last', '$email', '$ph', '$loc')"; 
//add values to database by using mssql_query 
mssql_query($query) or die('Error, insert query failed'); 



$sql="SELECT email FROM email WHERE id=1"; 

$result = mssql_query($sql) 
or die('Query failed. '); 

for ($i=0; $i < mssql_num_rows($result); $i++) { 
$body=mssql_result($result,$i,"email"); 
} 

$sql="SELECT subject FROM email WHERE id=0"; 

$result = mssql_query($sql) 
or die('Query failed. '); 

for ($i=0; $i < mssql_num_rows($result); $i++) { 
$subject=mssql_result($result,$i,"subject"); 
} 


//intializing session variable 
foreach($HTTP_POST_VARS as $key=>$value) 
{ 
$thisData[$key]=$value; 
$_SESSION[$key]=$value; 
} 

//intializing mail varables 
$to = stripslashes($_POST['email']); 
$from ="KCSIT@Keisercollege.edu"; 
if(mail($to, $subject, $body, "From: $from")){ 
header("Location: http://webdemo/thankyou.php");} 
else{ header("Location: http://webdemo/failedmailerr.html");} 
} 
else 
{ 
header("Location: http://webdemo/user_nmerror.html"); 
} 
mssql_close($connect); 
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Oct 26, 2006 9:15 pm
by akimm
I'm not sure if this is what you were searching for, but I think it may be of assistance.

Code: Select all

<?php
$query = $_POST['user_action'];
switch ($query){
	case "Action 1":
		#do this;
		break;	
	case "Action 2":
		#do that;
		break;	
	case "Action 3":
		#do something!;
		break;	
	case "Action 4":
		#something else?;
		break;	
	case "default":
		#default action;
                               break;
}
?>

Email Query part two.

Posted: Thu Oct 26, 2006 11:05 pm
by misslilbit02
Thanks for your help but I don't need a case stmt. At least not yet. Let's start with this...I put into my database table named email a message.

(1) How do I format that message so that it's not a line of text? I'm using <br> and that shows up in the email that is sent. I tried \n and that didn't seem to work either.

(2) I'm pulling a message out of the database that has variables in it. How do I get PHP to recognize that the message isn't just a string but a string with variables it needs to recognize.

I hope that makes sense.

Re: Email Query part two.

Posted: Fri Oct 27, 2006 1:11 am
by Chris Corbyn
misslilbit02 wrote:Thanks for your help but I don't need a case stmt. At least not yet. Let's start with this...I put into my database table named email a message.

(1) How do I format that message so that it's not a line of text? I'm using <br> and that shows up in the email that is sent. I tried \n and that didn't seem to work either.

(2) I'm pulling a message out of the database that has variables in it. How do I get PHP to recognize that the message isn't just a string but a string with variables it needs to recognize.

I hope that makes sense.
1. Use a text/html content type although text/plain should keep those newlines anyway. Try using mysql_real_escape_string() on your SQL data.

2. Those variables will have been parsed before they ever go into the database since they are in double quoted strings. You'd use eval() or str_replace() if they were actually in the string; preferably str_replace() for security reasons.

Posted: Fri Oct 27, 2006 6:46 am
by akimm
yea, he is right, textfile removes the extra whitespaces, although I imaigine a PHP editor would do the same trick.