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@domain.co.uk", "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@domain.co.uk", "Header","$message" );

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

Link to comment
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@domain.co.uk", "Header","$message" );

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.