Email message with variables


#1

Hi, i was wondering if anyone could give me some help…

I’ve created a script that emails a list of contacts in a database, so far it’s working perfectly, i’m just left scratching my head trying to put a few variables in the message body… I’ll attach my code below so if you could give me any advice that would be great.

I’ve got one variable in the message at the moment, but would basically like to set up a $message variable that pulls the variables from the first query and puts them into a user friendly email message.

I’m not asking for full code, maybe just a short example??

Also, how would i go about changing the status of the row that is in $req_id = $_GET[‘id’]; once the email has been sent out?

Thanks very much…

[php]

<?php session_start(); // Are we logged in? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) { // If we're not logged in, piss off to the login page header('Location: login.php'); exit; } // Get the request id from the previous selection $req_id = $_GET['id']; // Set mail prefs $subject = Part Request'; $headers = 'From: Whoever n'; // Get the data for that request from the database $dbid = mysql_connect ('localhost', 'username', 'password'); mysql_select_db('database',$dbid) or die ("Cannot find database"); $query = "SELECT * FROM request_data WHERE req_id = $req_id"; $result = mysql_query($query,$dbid) or die("SELECT error:".mysql_error()); while($row = mysql_fetch_array($result)) { $request_id = $row['req_id']; $manufacturer = $row['manufacturer']; $model = $row['model']; $capacity = $row['capacity']; $year = $row['year']; $part_one = $row['part_one']; $part_two = $row['part_two']; $part_three = $row['part_three']; $part_four = $row['part_four']; $part_five = $row['part_five']; $full_name = $row['full_name']; $telephone = $row['telephone']; $alt_telephone = $row['alt_telephone']; $email = $row['email']; $area = $row['area']; $comments = $row['comments']; } mysql_close($con); // Get the email addresses, depending on active status $dbid = mysql_connect ('localhost', 'username, 'password'); mysql_select_db('database',$dbid) or die ("Cannot find database"); $query = "SELECT requests FROM breakers WHERE active='yes'"; $result = mysql_query($query,$dbid) or die("SELECT error:".mysql_error()); while($row = mysql_fetch_array($result)) { set_time_limit(0); $request_subs = $row['requests']; mail("$request_subs", $subject, $part_one, $headers); echo 'test'; } mysql_close($con); ?>

[/php]


#2

to get all vars into the $message variable do something like:
$message=$manufacturer." ".$model.“ncapacaty:t”.$capacity.“nyear:t” […]

I haven’t understood the part with the $req_id. Do u have a status flag in ur MySQL db?

P.S.: watch your quotes:
$subject = !!!Part Request’;
$dbid = mysql_connect (‘localhost’, 'username!!!, ‘password’);


#3

Well, the $req_id is just getting the id of the row that is selected on the previous page, using this id the select query grabs the correct row from the database that we want to email out.

RE the status flag, basically there is a column in my table that basically sets all requests i recieve as new. As soon as they’re sent i’d like that changed to old so it’s only displayed on the correct page of my system.


#4

Could i ask what the following is doing?

.“ncapacaty:t”.

More specificaly, what is thet part?

Thanks


#5

“n” is a new line
“t” is a tabulator

it will align the values behind it


#6

mysql_query(‘UPDATE request_data SET status=“old” WHERE req_id=’.intval($req_id));


#7

Again, you’ve been an excellent help… one more thing before this part of my site is finished…

I’ve added some code to update the row of the table that we sent out… the thing is, it is breaking when it gets to this part of the script…

Any ideas/suggestions? I’ve attached the code…

Thanks

[php]

<? session_start(); // Are we logged in? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) { // If we're not logged in, piss off to the login page header('Location: login.php'); exit; } // Get the request id from the previous selection $req_id = $_GET['id']; // Set mail prefs $subject = 'Subject'; $headers = 'From: Men'; // Get the data for that request from the database $dbid = mysql_connect ('localhost', 'username', 'password'); mysql_select_db('database',$dbid) or die ("Cannot find database"); $query = "SELECT * FROM request_data WHERE req_id = $req_id"; $result = mysql_query($query,$dbid) or die("INSERT error:".mysql_error()); while($row = mysql_fetch_array($result)) { $request_id = $row['req_id']; $manufacturer = $row['manufacturer']; $model = $row['model']; $capacity = $row['capacity']; $year = $row['year']; $part_one = $row['part_one']; $part_two = $row['part_two']; $part_three = $row['part_three']; $part_four = $row['part_four']; $part_five = $row['part_five']; $full_name = $row['full_name']; $telephone = $row['telephone']; $alt_telephone = $row['alt_telephone']; $email = $row['email']; $area = $row['area']; $comments = $row['comments']; } mysql_close($con); $message = "Hi, here's a part request from http://www.url.co.uk.nnIf there are any problems with the data in the request please contact me on email@address.co.uknwith the REQUEST IDnnThanksnAndrew TaylornSite Adminn" . "nSubmitters Details:" . "nName:t"."$full_name" . "nTelephone:t"."$telephone" . "nAlt Telephone:t"."$alt_telephone" . "nEmail:t"."$email" . "nArea:t"."$area" . "nComments:t"."$commentsn" . "nBike Details:" . "nManufacturer:t"."$manufacturer" . "nModel:t"."$model" . "nCapacity:t"."$capacity" . "nYear:t"."$yearn" . "nPart Details:" . "nPart One:t" . "$part_one" . "nPart Two:t" . "$part_two" . "nPart Three:t" . "$part_three" . "nPart Four:t" . "$part_four" . "nPart Five:t" . "$part_five"; // Get the breakers email addresses, depending on active status $dbid = mysql_connect ('localhost', 'username', 'password'); mysql_select_db('database',$dbid) or die ("Cannot find database"); $query = "SELECT requests FROM breakers WHERE active='yes'"; $result = mysql_query($query,$dbid) or die("SELECT error:".mysql_error()); while($row = mysql_fetch_array($result)) { set_time_limit(0); $request_subs = $row['requests']; mail("$request_subs", $subject, $message, $headers); } mysql_close($con); $set_sub_old = 'old'; $dbid = mysql_connect ('localhost', 'username', 'password'); mysql_select_db('database',$dbid) or die ("Cannot find database"); $query = "UPDATE request_data SET state='$set_sub_old' WHERE req_id='$req_id'"; $result = mysql_query($query,$dbid) or die("Update error:".mysql_error()); // If all is cool, this will redirect to a thank you page. echo 'sent and submission updated'; exit; } ?>

[/php]


#8

What is the error or what is it doing or not doig?


#9

Sorry, it just isn’t printing the echo at the end of the code. I was originally using a header redirect but when that wasn’t working used a echo instead.

There are no errors coming up. It just isn’t updating the row and isn’t sending the email which i find a bit strange…


#10

Have you tried to view source after you have loaded the page through the browser? Sometimes there is stuff in there that can give you a clue as to what is going wrong.

Also, not exactly sure, but this also could be causing a problem

$message = "Hi, here’s…

The ’ could be messing with php’s parser.

Might want to look into that as well.


#11

The thing is, before i added the bit of code to update the row in the database it was functioning fine which would lead me to believe the ’ isn’t causing the problem.

I’ve looked at the page source but there is only the following:


#12

I guess if I were you, I would start putting in some echo’s and see where the script is failing…

echo "Made it to section 1.
";
some code
echo "Made it to section 2.
";
some code

Something like that would help.


#13

None of those are displaying, i really have no idea what is wrong with it… If it was a syntax error it would tell me so rather than leaving me with a blank screen wouldn’t it?

Thanks


#14

It’s something to do with the code i added to the bottom. As soon as i remove it and add an echo or redirect to the part that does the mailing it works…

[php]

$set_sub_old = 'old';
	
$dbid = mysql_connect ('localhost', 'username, 'password');
          mysql_select_db('database',$dbid) 
          or die ("Cannot find database");

  $query = "UPDATE request_data SET state='$set_sub_old' WHERE req_id='$req_id'";	  
     $result = mysql_query($query,$dbid) 
     	     or die("Update error:".mysql_error());
 	 
  // If all is cool, this will redirect to a thank you page.
    echo 'sent and submission updated';
    exit;
 }

?>
[/php]


#15

Doh, what an idiot, it was the rogue } that was killing it. :D