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: <me@whatever.com>' . "\r\n";//i'm using my real address here to test
$headers .= 'Cc: myboss@example.com' . "\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

Link to comment
Share on other sites

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 example@yahoo.com joe smith

 

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.