Jump to content

Recommended Posts

What I am trying to do is run a loop from a database ... grabbing all the emails within the table and sending out the same email to each one .... below is my code .. it works fine .. however it pulls the info from an array and I need it to pull it from a database ...

 

table name: myemails

id: $id

email:  $email

 

can anyone help me ?

 

<?php

  set_time_limit(0); // this will keep the script from stoping if it takes longer then 30 seconds, must have this here
  $emailaddress = array('test1@test.com', 'test2@test.com', 'test3@test.com', 'test4@test.com', 'test5@test.com', 'test6@test.com');
  $emailsubject = "This is a sample subject!";
  $emailbody = file_get_contents("email.html");
  $fromaddress = "name@domain.tld";
  $i = count($emailaddress);
  $z = 0;

  // here we check how many  email address's we have, if its is 0, then we dont start the email function
  if ($i != 0)
    {// start if

      // Lets loop until we reach the count from email address arrar
      while ($i != $z)
        {// start while

          // here we send the email to the varables from above, using the email array incrament
          mail($emailaddress[$z], $emailsubject, $emailbody, "From: " .$fromaddress. "\nX-Mailer: PHP 4.x");

          // lets echo out that the email was sent
          echo $z + 1 . " out of " . $i . " emails sent. (" . $emailaddress[$z] . ")<br>";

          // incrament the array one, so we get a new email address from the array
	  ++$z;

        }// end while   

      }//end if

   else
     {//start else

       // we echo out that no emails where found in the array and end the script
       echo "Warning: No emails in array.";

     }// end else

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/139689-php-email-script-help/
Share on other sites

mysql_connect, mysql_select_db, mysql_query, mysql_fetch_array

 

You will need to use all of those. You first need a table in the database and the connection info for the database, then create the connection with mysql_connect, connect to the database with the select_db then query the db and put that result into mysql_fetch_array which you will loop through each row using the values in your mail function.

mysql_query() (*don't forger "or die(mysql_error())" after*)

$array = mysql_fetch_assoc();

do{

code stuff here for mail

}while($array = mysql_fetch_assoc(same as up above));

 

does that get you the gist along with premiso?

(btw, either mysql_fetch_assoc() or mysql_fetch_array() could be used)

Your code should look something like this:

 

<?php

set_time_limit(0);

$host = 'hostname';
$user = 'username';
$pass = 'password';
$db   = 'database';

// connect to mysql host
$link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());

// select database
$db_selected = mysql_select_db($db, $link) or die ("Can't use $db : " . mysql_error());
  
$subject = "This is a sample subject!";
$body = file_get_contents("email.html");
$from = "name@domain.tld";

// query database for email addresses
$query = "
SELECT 
	email
FROM 
	myemails
";
$result = mysql_query($query) or die('Invalid query: ' . mysql_error());

// send emails
$total = mysql_num_rows($result);
$x = 1;
while ($row = mysql_fetch_object($result) {
mail($row->email, $subject, $body, "From: $from\nX-Mailer: PHP 4.x");
echo "$x out of $total emails sent. (" . $row->email . ")<br />";
$x++;
}
?>

Heres the code that will you acheive wht you are looking for .

 

<?php set_time_limit(0); // this will keep the script from stoping if it takes longer then 30 seconds, must have this here

$dbhost = 'localhost'; //DB host
$dbuser = 'root';      //DB user with read permission
$dbpass = 'XXXX';   //DB password
$conn = mysql_connect($dbhost,$dbuser,$dbpass) or die("Error Connecting To DB");
$dbname='test';         //db name
mysql_select_db($dbname);



/*assumed test database has one column(email) which is populated with email addresses. 
(modify per ur req) */


//get the email ids of users;

  $userEmails = "select distinct email from test order by email;";
  $success = mysql_query($userEmails);
  $Total = mysql_num_rows($success);

  if($Total == 0){
  echo "Db contains no email addresses.";
  exit;
  }


//fetching the email address one after the other to send the mail

while($info = mysql_fetch_array($success)){

$email = $info['email']; //we have the first users email address;
$message = "Hello ".$email;
$subject = "Hello ";

$headers = 'From: test@test.com' . "\r\n" .
    'Reply-To: test@test.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

	$sent = mail($email, $subject, $message, $headers);

	 if($sent){
	 echo "Email sent to ".$email."<br>";
	 }else{
	 echo "Error: Unable to mail ".$email."<br>";
	 }


}


?>

 

Rgds,

Kris

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.