Jump to content

[SOLVED] [Problem] sending a email with php to many emailadressen, using a database


funzmu

Recommended Posts

 

 

                                                        //Showing all emails in database

<?php

mysql_connect("localhost","sa","pass");

mysql_select_db("DB");

 

$qr = mysql_query("select mail_addr FROM Memb_Info");

 

$nrows = mysql_num_rows($qr);

for ($i=0; $i < $nrows; $i++) {

$row = mysql_fetch_array($qr);

echo $row['mail_addr']."<br><br>";

}                                                     

 

 

                                                              //Sending email

$to = $row['mail_addr'];               

$subject = "hi";

$body = "message";

$headers = "From: [email protected]\r\n" .

    "X-Mailer: php";

if (mail($to, $subject, $body, $headers)) {

  echo("<p>Message sent!</p>");

} else {

  echo("<p>Message delivery failed...</p>");

}                                                                   

?>

 

 

When I use this script, it only sends a email to the last person in the database. who can help me?

 

 

<?php
mysql_connect("localhost","sa","pass");
mysql_select_db("DB");

$qr = mysql_query("select mail_addr FROM Memb_Info");

while ($row = mysql_fetch_array($qr)) {
   $tolist .= $row['mail_addr'] . ", ";
}                                                       

//Sending email
$subject = "hi";
$body = "message";
$headers = "From: [email protected]\r\n" .
    "X-Mailer: php";
if (mail($tolist, $subject, $body, $headers)) {
  echo("<p>Message sent!</p>");
} else {
  echo("<p>Message delivery failed...</p>");
}                                                                     
?>

 

Sorry, I don't understand, I am just a beginning n00b.

I try this:

 

<?php

mysql_connect("localhost","sa","pass");

mysql_select_db("DB");

 

$qr = mysql_query("select mail_addr FROM Memb_Info");

 

while ($row = mysql_fetch_array($qr) ) {

  for ($row['mail_addr']  = 1 + mail_addr)

  $tolist .= $row['mail_addr'] . ", ";

 

                                                 

//Sending email

$subject = "hi";

$body = "message";

$headers = "From: [email protected]\r\n" .

    "X-Mailer: php";

if (mail($tolist, $subject, $body, $headers)) {

  echo("<p>Message sent!</p>");

} else {

  echo("<p>Message delivery failed...</p>");

}                                                                   

?>

right...as mentioned, if you want to break it down, put it inside a loop. There are many ways to do this, with different things to consider (lookup php bulk emailing), but one example:

 

<?php
mysql_connect("localhost","sa","pass");
mysql_select_db("DB");

$qr = mysql_query("select mail_addr FROM Memb_Info");

while ($row = mysql_fetch_array($qr) ) {
   for ($row['mail_addr']  = 1 + mail_addr)
   $tolist .= $row['mail_addr'] . ", ";
}   

//Sending email
$subject = "hi";
$body = "message";
$headers = "From: [email protected]\r\n" . "X-Mailer: php";

foreach ($tolist as $currentemail) { // loop to cycle through email addresses one at a time
   if ($currentemail, $subject, $body, $headers)) {
      echo("<p>Message sent!</p>");
   } else {
      echo("<p>Message delivery failed...</p>");
   }
} // end foreach
?>

   

<?php

mysql_connect("localhost","sa","pwd");

mysql_select_db("db");

 

$qr = mysql_query("select Test_Info FROM Memb_Info");

 

while ($row = mysql_fetch_array($qr)) {

  $tolist .= $row['Test_Info'] . ", ";

}                                                     

 

$subject = "hi";

$body = "nice";

$headers = "From: [email protected]\r\n" .

    "X-Mailer: php";

 

foreach ($tolist as $currentemail) { // loop to cycle through email addresses one at a time

  if ($currentemail, $subject, $body, $headers)) {

      echo("<p>Message sent!</p>");

  } else {

      echo("<p>Message delivery failed...</p>");

  }

} // end foreach

?>

 

 

 

Parse error: syntax error, unexpected ',' in /home/joran/domains/funzgames.com/public_html/mail/test.php on line 17

 

an error in this  if ($currentemail, $subject, $body, $headers)) {

 

I need some good tutorials ^^ I don't see the , error  ???

 

Oops. I set it up originally to just make a comma separated list. You need to change this:

 

while ($row = mysql_fetch_array($qr)) {
   // this one make one long string of your emails, separated by commas
   $tolist .= $row['Test_Info'] . ", ";
}

 

to this:

 

while ($row = mysql_fetch_array($qr)) {
   // this one makes an array of your emails
   $tolist[] = $row['Test_Info']; // this 
}

 

Allright :) it's working now... Great job, you helped me alot :):):)

 

Here is the complete working script for people who need it also:

 

<?php

mysql_connect("localhost","sa","pwd"); //your sqlname, userlogin, password

mysql_select_db("db");                      //Database that need to be connected

 

$qr = mysql_query("select monkeys FROM 1234"); //Select monkeys from table name 1234

 

while ($row = mysql_fetch_array($qr)) {

  $tolist[] = $row['monkey'];                        //fill in monkeys agian

}                                                     

 

$subject = "Hello";

$body = "Enjoy";

$headers = "From: [email protected]\r\n" .

    "X-Mailer: php";

 

 

foreach ($tolist as $currentemail) { // loop to cycle through email addresses one at a time

  if (mail($currentemail, $subject, $body, $headers)) {

      echo("<p>Message sent!</p>");

  } else {

      echo("<p>Message delivery failed...</p>");

  }

} // end foreach

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.