Forum Moderators: coopster
I have to email about 100-150 users whose emails I receive from a database. So, I have the following code to do it:
<?php
require ('../admin/connect.php');
$subject=$_POST['subject'];
$body=$_POST['body'];
$fromaddress='From: info@domain.com';
$findout=mysql_query("SELECT name,lastname,email,lang from folks WHERE lang='English'");
if ($findout) {
while($row = mysql_fetch_array($findout))
{
$lastname= ($row["lastname"]);
$toaddress= ($row["email"]);}
$subject=$_POST['subject'];
if (mail ($toaddress, $subject, $body, $fromaddress)) { echo 'Everything went ok!<br>';
}
else {
echo 'Houston, we got a problem!<br>';
}
}
?>
So, if I run my script (the one with the form which I fill out with the subject and body fields), and when I click 'Mail Them', the script returns "Everything went ok!". After that, I check my two test email address from a database, and I receive the email on first account, but the other email address I put in a database (for testing purposes also) does not receive the email. Any ideas why?
Your problem is your while loop.
while($row = mysql_fetch_array($findout))
{
$lastname= ($row["lastname"]);
$toaddress= ($row["email"]);}
This just loops through overwriting the $toaddress variable each time until you get to the last one. So it only ever has one value. Hope that makes sense.
Your mail function also needs to be in the loop:
if ($findout) {
while($row = mysql_fetch_array($findout))
{
$lastname= ($row["lastname"]);
$toaddress= ($row["email"]);
$subject=$_POST['subject'];
if (mail ($toaddress, $subject, $body, $fromaddress)) { echo 'Everything went ok!<br>';}
unset($toaddress);
}
else {
echo 'Houston, we got a problem!<br>';
}
}
Hope that helps.
dc
Parse error: parse error, unexpected T_ELSE in /home/website/public_html/testing/mailit.php on line 41 It's the else statement right above the
echo 'Houston, we got a problem!<br>';
line. I don't get it? Oh, and btw, I think I don't have to declare a
$subject=$_POST['subject'];
twice? Once right after the require() and than again in the while loop? I am a newbie, as you can see :)
Try:
if ($findout) {
while($row = mysql_fetch_array($findout))
{
$lastname= ($row["lastname"]);
$toaddress= ($row["email"]);
$subject=$_POST['subject'];
if (mail ($toaddress, $subject, $body, $fromaddress)) { echo 'Everything went ok!<br>';
}
else {
echo 'Houston, we got a problem!<br>';
}
unset($toaddress);
}
}
If the subject variable is the same for all messages, you only need declare it once. It will be available for the duration of the loop. So just declare it outside of the loop instead of inside.
dc
Yes, that's true, the unset was written at the wrong place. It's ok now, thank you. My server is limited at 1000 emails per hour, so now I am beginning to think how could I manage to mail users if my database grow to a 1000+ email accounts?
Moderator jatar_k once wrote me that he was releasing 50 emails every 5 or 10 minutes on cron and that it's not very complicated (as he says he was using some kind of flags) to see which members already received the email and to select the next 50 and send them, so I am curios how would the code go? Any pointers would be great..