Forum Moderators: coopster

Message Too Old, No Replies

PHP Mail Functions and MySQL

         

branmh

10:52 pm on May 20, 2007 (gmt 0)

10+ Year Member



Okay I have created a script that post user information to mysql database. Now I would like to be able to send a webpage to each user based on there place and state, but the code below sends to one user multiple pages.

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);

}
}

mcibor

7:20 am on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wouldn't loop, just send the first result, or alter your SQL:

// send the data
if (mysql_num_rows($result)) {
$qry = mysql_fetch_array($result); //no loop here!
print " fetching webpage now...." . $qry[state]. "&#166;" . $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

branmh

5:45 pm on May 21, 2007 (gmt 0)

10+ Year Member



Can you explain on how to do this, because I do need this to loop through out the mysql entries?

eelixduppy

7:27 pm on May 21, 2007 (gmt 0)



If you are going to be sending many emails, it is recommended to use PEAR::Mail [pear.php.net] or PEAR::Mail_Queue [pear.php.net]. PEAR::Mail_Mime [pear.php.net] might also be of interest to you here.

mcibor

8:50 pm on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you say, that one user get's multiple emails?

Could you provide us with your query? I think there lies the problem: you're not using GROUP BY.

Michal

PS. Also to optimize you could store the files in variables, and read from disk only when there a need for it.

branmh

4:37 am on May 22, 2007 (gmt 0)

10+ Year Member



If I run the script the first one runs okay but the others includes 2 webpages, 3 webpages.

Yes this would be use for about 15 emails of different webpages?

If you could explain about the other emailers?

mcibor

12:21 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I must say, that I may have misunderstood your last reply.

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

branmh

1:42 pm on May 22, 2007 (gmt 0)

10+ Year Member



It's not sending multiple email but in the second email you get location 1 followed by location 2. third email gets location 1, location 2, location 3.

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();

mcibor

6:33 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. So now I understand.

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 variable

while(!feof($file))
{
$message .= fgets($file) . "\n";
}
fclose($file);

branmh

3:00 am on May 24, 2007 (gmt 0)

10+ Year Member



Okay thanks now that works.

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?

mcibor

6:25 am on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You forgot to put quotes around the variable:

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