Forum Moderators: coopster
Ok, here's the deal. I have written a MySQL database and I'm in the last stages of writing the PHP frontend for it. I need to be able to pull an email address from the database and then send an email to that user by using the PHP mail function.
My database is running perfectly, it can and does return the email when queried, I just can't figure out how make data that is retrieved into a variable.
Here is my code (in it's entirety) for reference:
<?php
$con = mysql_connect('localhost','*********','************');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('**********');
// Var Notes:
// var = quote number from htm
// var1 = Serach Criteria from htm
// var3 = textarea from php
// Declare vars from searchdb.htm
$variable_from_htm = @$_POST['var'];
$variable_from_htm1 = @$_POST['var1'];
$variable_from_php = @$_POST['body'];
if ($variable_from_htm=='lname')
$query = "select * from customer where lname = '$variable_from_htm1' ";
elseif ($variable_from_htm=='recordnum')
$query = "select * from customer where recordnum = '$variable_from_htm1' ";
elseif ($variable_from_htm=='phone')
$query = "SELECT * FROM customer WHERE phone = '$variable_from_htm1' " ;
else
echo 'Incorrect Syntax: No Criteria Given';
$result = mysql_query($query) or die("Can't execute insert query: " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo '<br />';
echo "Customer's Email Address: ";
echo '<font color="#FF7600">',$row['email'], '</font>';
}
$mailaddy = '$row['email']';
$to = "$mailaddy"; // Seperate With Comma's
$subject = "Test";
$message = "$variable_from_php";
$result = mail($to,"$subject ",$message, "From:PHPMail-Script");
if ($result){
echo ('Mail Sent');
} else { echo('Error Sending Mail'); }
mysql_close($con);
?>
Any and all help is greatly appreciated!
Thank you in advance.
$result = mysql_query($query) or die("Can't execute insert query: " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo '<br />';
echo "Customer's Email Address: ";
echo '<font color="#FF7600">',$row['email'], '</font>';
$mailaddy .= $row['email']." , "; //Change Number 1 - In the loop and correct syntax
}
$mailaddy = rtrim($mailaddy,","); //Change Number 2 - Trimming the extra comma
$to = "$mailaddy"; // Seperate With Comma's
$subject = "Test";
$message = "$variable_from_php";
$result = mail($to,"$subject ",$message, "From:PHPMail-Script");
if ($result){
echo ('Mail Sent');
} else { echo('Error Sending Mail'); }
Habtom
[edited by: Habtom at 7:50 am (utc) on Oct. 4, 2007]
However, I did notice one little problem while setting up another page with a serious amount of variables pulled from the database to be included in the body of the email.
Code:
$lastname .= $row['lname']." , ";
Will produce this output:
Lastname ,
New Code:
$firstname .= $row['fname']."";
Will produce this output:
Firstname
Just in case anybody needs this little tidbit of knowledge that HabTom graciously provided.
rtrim($mailaddy,",");
rtrim [php.net] is used to trim any character or spaces at the right end of a string.
Habtom
I used your code as an example and listed the variables I wanted to retrieve from the database one at a time then did rtrim on them one at a time and ending up with ,'s all over my email! Actually, it was kind of a trip to read...Must be what it's like to try and code while on acid, eh? LOL.
Anyway, I really do appreciate your help and I did get it running thanx to you.