tags:

views:

32

answers:

2

I want to send an email to multiple recipients using PHP mail() function. The email message is simply a reminder that a membership is due to expire so email addresses will come from MySql database query. There would anywhere from 2-10 at any given time. I found the following code but it generate errors. The problem is not with my query as it generates an accurate recordset. This is the code I have: Hopefully someone can help. By the way, I am very much a novice so need easy straight forward explanation. Thanks in advance:

<?php 

$recipients = ("SELECT email FROM tblMembers WHERE search criteria=criteria"); 
$email_list = $db->query($recipients); 
foreach($email_list as $row) { 
$to = $row['email']; 
$subject = "Membership Renewal";
$headers = "From: Membership Coordinator <[email protected]>\r\n";
$message = "THIS IS AN AUTOMATED EMAIL.  PLEASE DO NOT REPLY""\n""etc, etc, etc";
  if ( mail($to,$subject,$headers,$message) ) {
   echo "Email was sent successfully";
   } else {
   echo "Email delivery has failed!";
   }
} 
?> 
A: 

As far as I know, then $headers comes after $message, so you should just change the order in mail() and be more aware in future.

Tom
A: 

Change

$message = "THIS IS AN AUTOMATED EMAIL.  PLEASE DO NOT REPLY""\n""etc, etc, etc";

to

$message = "THIS IS AN AUTOMATED EMAIL.  PLEASE DO NOT REPLY\netc, etc, etc";

There is the syntax error, because " will end the string. You would need a . to concatenate the next string.

But you could also leave the two " out at this point, becase in a double quoted string, PHP will replace \n by a newline character.

JochenJung
Now I have a new error. The code is in a for that displays the list of renewals in a dynamic form with repeat region. Before the page would not open at all. The error message appeared on a white page. Now the page is opening but I have a new error at the bottom of the page that says"Fatal error: Call to a member function query() on a non-object in /data/9/1/42/26/1205515/user/1284694/htdocs/Members/Renewals_Due.php on line 79And line 79 is:$email_list = $db->query($recipients);
Kathy
Are you within a function when you call $db->query() ?Then you need to add "global $db;" before the $db->query() call.And check if you have loaded the $db class.
JochenJung
OK. I have checked all of this. Now the error says that the foreach argument is invalid. As in code above, I have: foreach($email_list as $row)
Kathy
I don't know your $db class, but I suppose, you have to use it like this:$email_list = $db->query($recipients);foreach($row = $db->fetch_array($email_list)) {[...]
JochenJung
Nope, not the answer. Thanks for trying to help but I guess I am in over my head here so I am just going to forget about it for now. I have code that sends a single email to an address captured in a session variable. Works great. I have another page where I want to send an email to multiple addresses based on recordset results. Sounds like it should be fairly straightforward but despite many, many hours reading and researching, I am no further ahead, so I have to forget it for a while. Thanks again.
Kathy