0

I'm trying to mail everybody in my subscriber database. I'm trying to go through each PersonID but if people unsubscribe, rows will be missing from my database. It could go 1,2,5,9,10,11,15 etc.

How can I best accommodate this?

$pullEmails = mysql_query("SELECT COUNT(*) FROM Subscribe;");
while($pullEmails > 0))
{
    $getEmail = mysql_query("SELECT Email from Subscribe where PersonID = " . $pullEmails . ";");
    $subject = "Test mail";
    $message = "test email to all the subscribers";
    $from = "admin@codefundamentals.com";
    $headers = "From:" . $from;
    mail($getEmail,$subject,$message,$headers);
    $pullEmails = $pullEmails - 1;
}

EDIT - I have this

$sql = mysql_query("select 'Email' from Subscribers");
$recipients = array();
while($row = mysql_fetch_array($sql)) {
    $recipients[] = $row['Email'];
}

$to = 'admin@codefundamentals.com';
$subject = "E-mail subject";
$body = "E-mail body test. sorry everyone";
$headers = 'From: admin@codefundamentals.com' . "\r\n" ;
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";

mail($to, $subject, $body, $headers);

It's returning back

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home1/codefund/public_html/admin/submit.php on line 33 script finished

But I can't figure out why! Googling it gives me mysql_fetch_array() but I've incorporated that?

Luís Cruz
  • 14,264
  • 16
  • 68
  • 94
Ryan Reese
  • 19
  • 3
  • 1
    Why don't you just get all rows (e-mail addresses...) in one query and loop over the result? This doesn't make a lot of sense: You are not actually fetching the count and querying in a loop is almost always a bad idea. Note that you are also not getting the addresses, you always need to fetch a row to get the actual results from a database query. – jeroen Aug 11 '14 at 00:04
  • I'm not that good at PHP/SQL so apologies if this is primitive and stupid – Ryan Reese Aug 11 '14 at 00:09
  • 1
    as mentioned above you just use `$getEmail = mysql_query("SELECT Email from Subscribe");` then loop the results. be careful if this is a shared host they almost always impose mail sending limits. –  Aug 11 '14 at 00:11
  • You should start with a look at the php manual; any of the `mysql`, `mysqli` or `PDO` sections would have given you the right idea straight away. – jeroen Aug 11 '14 at 00:11
  • Could I just send one email and have all the "to"'s differentiated with a comma? – Ryan Reese Aug 11 '14 at 00:14
  • not recommended as each person would see every ones email address, but that approach using BCC is an idea. Just as long as you dont think this will get around sending limits –  Aug 11 '14 at 00:15
  • Your mail system might not be set up to accept that many addresses so I guess it would depend on the amount. – jeroen Aug 11 '14 at 00:16
  • *"Could I just send one email and have all the "to"'s differentiated with a comma?"* - You shouldn't need to do that, nor should you. Loop over your results doing a `foreach($row['Email'] as $to)` then use `$to` as the mail recipient. That way, nobody will see other addresses. – Funk Forty Niner Aug 11 '14 at 00:17
  • I'll try and google the code to get this working - I'm new to PHP and unsure what all of that is. Thanks for the ideas. – Ryan Reese Aug 11 '14 at 00:22
  • there are quite a few tutorials on sending email to a list in a db –  Aug 11 '14 at 00:40

1 Answers1

0

That error is telling you that there is an error with the query or the database connection. That is, your $sql is not a valid resource.

Your SQL

select 'Email' from Subscribers

will return a row with only a column with the content Email. You should change that to

select Email from Subscribers

Also, make sure that the column name is Email and the table name is Subscribers. Copy that query and run it inside PhpMyAdmin / Mysql Workbench / HeidiSQL, etc.

Besides that, make sure that your SQL connection is OK. You might this tutorial from w3schools usefull.

As a side note, you should consider the use of mysqli or PDO.

Luís Cruz
  • 14,264
  • 16
  • 68
  • 94
  • Few things wrong on my end - wrong table name - also previously in the script I was using a different database so I had to switch over. And finaly yes your SQL worked. Many htanks. – Ryan Reese Aug 11 '14 at 01:40