Forum Moderators: coopster
What is wrong with this code or is there something better to use to do this?
// send the data
if (mysql_num_rows($result)) {
while ($qry = mysql_fetch_array($result)) {
print " fetching webpage now...." . $qry[state]. "¦" . $qry[place] . "<br>\n";$path = "http://domain.com/printfriendly/localnews/$qry[state]/$qry[place]/";
$fp = fopen($path, 'r');
do //we loop until there is no data left
{
$data = fread($fp, 8192);
if (strlen($data) == 0) break;
$content .= $data;
} while (true);
$message .= $content . "\n";
$headers = "From: \"MyName\"<noreply@domain.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-15\n";
$headers .= "Content-Disposition: inline; \n\n";
@mail($qry[email], $subject, $message, $headers);
print " send message to....". $qry[email] ."<br>\n";
sleep(1);
}
}
// send the data
if (mysql_num_rows($result)) {
$qry = mysql_fetch_array($result); //no loop here!
print " fetching webpage now...." . $qry[state]. "¦" . $qry[place] . "<br>\n";$path = "http://domain.com/printfriendly/localnews/$qry[state]/$qry[place]/";
$fp = fopen($path, 'r');
do //we loop until there is no data left
{
$data = fread($fp, 8192);
if (strlen($data) == 0) break;
$content .= $data;
} while (true);
$message .= $content . "\n";$headers = "From: \"MyName\"<noreply@domain.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-15\n";
$headers .= "Content-Disposition: inline; \n\n";@mail($qry[email], $subject, $message, $headers);
print " send message to....". $qry[email] ."<br>\n";
sleep(1);
}
Also there are better ways to loop through a file, but that's up to you.
Hope this helps
Michal
You mean, when you run this script (the whole) once, then it works fine, but on the next, second run, users get multiple emails?
Yes this would be use for about 15 emails of different webpages? - I don't understand what you mean
I understand that the last question was to eelix.
Could you still please post the sql query you are using? It will help us solve your problem.
Regards
Michal
I hope this above is helpful.
Here is the Send file, hope this is what you need?
$subject = "Your Local News";
// connect to the mysql server
$dbh=mysql_connect ($server, $db_user, $db_pass)
or die ('I cannot connect to the database because: ' . mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
// read data from database
$result = mysql_query("select * from $table order by id desc limit $rows", $dbh)
or die ("Could not read data because ".mysql_error());
// send the data
if (mysql_num_rows($result)) {
foreach ($qry = mysql_fetch_array($result))
print " fetching webpage now...." . $qry[state]. "¦" . $qry[place] . "<br>\n";
$file = fopen("http://domain.com/printfriendly/localnews/$qry[state]/$qry[place]/", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
$message .= fgets($file) . "\n";
}
fclose($file);
$headers = "From: \"MyName\"<noreply@domain.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-15\n";
$headers .= "Content-Disposition: inline; \n\n";
@mail($qry[email], $subject, $message, $headers);
print " send message to....". $qry[email] ."<br>\n";
sleep(1);
}
}
mysql_close();
The solution is quite easy - you don't initialize variable $message and that's the problem.
Solution is:
$file = fopen("http://domain.com/printfriendly/localnews/$qry[state]/$qry[place]/", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
$message = ''; //-initialize variablewhile(!feof($file))
{
$message .= fgets($file) . "\n";
}
fclose($file);
Now I'm trying to create a unsubscribe feature but it's not working right.
With this code:
$query = "DELETE FROM $table WHERE email=$_GET[email]";
$result = mysql_query($query, $dbh)
or die("Could not delete data because ".mysql_error());
Return's this:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'email@domain.com' at line 1
What is wrong with this code, because all the internet searches I find has the same code?
this code should work fine:
$email = mysql_real_escape_string($_GET[email]);//at least make secure against sql injections$query = "DELETE FROM $table WHERE email='$email'";
$result = mysql_query($query, $dbh)
or die("Could not delete data because ".mysql_error());
Hope this helps
Michal