Jump to content

Sending email wtih mysql qurey result on body


phpme1221

Recommended Posts

Hey 1221,

I tested this code and it sent out to emails in database with specific info in the body of the email. I'm sorry I can't test your exact given info because i'm limited to only a certain outgoing mail servers. However, the code exact same. So here it is:

<?php
mysql_connect('localhost', 'root', '******') or die('could not connect: ' . mysql_error());
mysql_select_db("database") or die('cannot select the database');
?>

<html>
<head>
<title>create student table</title>
<body>

<?php
$to = "';
$subject = 'email testing';
$message = "';
$message .= "<table border = 1>";
$message .= "<tr><th>Name</th><th>Email</th></tr>";	
$query = "select Name, Email from info"; // adjust to your code
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
$message .= "<tr><td>";
$message .= $row['Name'];
$message .= "</td><td>";
$to .= $row['Email'];
$message .= $row['Email'] . "<br />";

$message .= "</table>";
//end email here
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: <[email protected]>' . "\r\n";//i'm using my real address here to test
$headers .= 'Cc: [email protected]' . "\r\n";
mail($to,$subject,$message,$headers);
}

?>
</body>
</html>

It works perfectly for me... make sure the addresses in the database are valid and enable the mailing port in php.ini

If that's doesn't work for you, screw it lol

try using CONCAT in your select

 

 

$sql="SELECT CONCAT(PrimaryContact,email,hostname) FROM sm_table WHERE hostname='$hostname' ORDER BY hostname ASC ";

 

should output something like

 

zzz [email protected] joe smith

 

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.