Jump to content

embeding variables in email messages


wright67uk

Recommended Posts

How can i embed variables into an email message.

Eg. If i want to add the result of mysql query, would i do somthing similiar to this;

mail( "[email protected]", "Header","$message $result" ); ?

Obviously this doesn't work.  In fact i get the message from my html form, followed by 'Resource id #2'.

Im learning php at the moment.  If anyone could help me with this, or point me in the right direction this would be great.  Iv'e looked at w3schools but I cant find anything specific like this.

 

mysql_connect("db.hostedresource.com","treesurgery","password here"); 
mysql_select_db("treesurgery") or die("Unable to select database"); 
$code = $_GET['postcode'];
$message = $_GET['message'];
$result = mysql_query("SELECT email FROM treesurgeons WHERE postcode LIKE '%$code%' ORDER BY companyName LIMIT 3")
or die(mysql_error());  

echo "<h2>Business Names:</h2>";
while ($row = mysql_fetch_array( $result ))
{
echo " ".$row['email'] . "<br>";
}
echo "<br>";
echo $message;

mail( "[email protected]", "Header","$message" );

  echo "Thank you for using our mail form.";

Link to comment
https://forums.phpfreaks.com/topic/225896-embeding-variables-in-email-messages/
Share on other sites

$result doesn't carry the actual result that you're looking for, it carries the resource id (like in the error message).  What this basically means is it's a resource that other functions can tap into/use, like mysql_fetch_array.

 

So you need to use mysql_fetch_array on your $result variable (like in the snippet of code you posted) which returns a string that you can put in your message.

 

The line I added ($message .= $row['email']) will add each email address of each treesurgeon to the message.

 

mysql_connect("db.hostedresource.com","treesurgery","password here"); 
mysql_select_db("treesurgery") or die("Unable to select database"); 
$code = $_GET['postcode'];
$message = $_GET['message'];
$result = mysql_query("SELECT email FROM treesurgeons WHERE postcode LIKE '%$code%' ORDER BY companyName LIMIT 3")
or die(mysql_error());  

echo "<h2>Business Names:</h2>";
while ($row = mysql_fetch_array( $result ))
{
$message .= $row['email'];
echo " ".$row['email'] . "<br>";
}

echo "<br>";
echo $message;

mail( "[email protected]", "Header","$message" );

  echo "Thank you for using our mail form.";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.