Take the 2-minute tour ×

I am a bit confused about how to use foreach. I read some internet things on it and I kind of understand how it works, but I don't fully understand it. I think I could use foreach to create a PHP mass emailer that sends blank carbon copy to email addresses and adresses the customer by name in the subject (Dear, Michael Here is your email). I've figured out how to retrieve the names and emails from my database into variables and I know how to email, but I don't know how to send multiple emails at once and to associate the name and email address.

        //Variables for connecting to your database.
        //These variable values come from your hosting account.
        $hostname = "MichaelBerna.db.10339998.hostedresource.com";
        $username = "MichaelBerna";
        $dbname = "MichaelBerna";

     //These variable values need to be changed by you before deploying
        $password = "********";
        $usertable = "subscribers";
        $yourfield = "name";
        $yourfield1 = "email";

        //Connecting to your database
        $link = mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later.");

        //Fetching from your database table.
        $query = "SELECT * FROM $usertable";
        $result = mysql_query($query);

        if ($result) 
            while($row = mysql_fetch_array($result)) 
                $name = $row["$yourfield"];
                $email = $row["$yourfield1"];
                echo "Name: $name<br>";
                echo "Email: $email<br>";


Here is my email code:


    require_once '../PHPMailer_5.2.2/class.phpmailer.php';

    $name = $_POST['name'] ;

    $email = $_POST['email'] ;

    //$file = $_POST['file'] ; // I'm going to later add a file later to be attached in email from database

    $body = "Hey $name thank you for continuing to be a valued customer! This month's story is included in this email asa an attachment.";

    $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch



      $mail->AddAddress($email, $name);

      $mail->SetFrom('admins_email@yahoo.com', 'Site Admin');

      $mail->AddReplyTo('admins_email@yahoo.com', 'Site Admin');

      $mail->Subject = "Dear $name Your monthly subscription has arrived!";

      $mail->Body = $body;

      if ($_FILES['file']['size']) 


      $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);// attachment



      echo "Email Sent Successfully</p>\n";   


    catch (phpmailerException $e) 

      echo $e->errorMessage(); //Pretty error messages from PHPMailer

    catch (Exception $e) 

      echo $e->getMessage(); //Boring error messages from anything else!



Basically, I need a way to combine these two scripts and link them together and that's what I'm unsure of how to do.

share|improve this question
You don't understand foreach? It's just looping, a basic computer programming concept. –  Barmar Oct 11 '13 at 0:44
just use your email code in side the while loop you already have –  Dagon Oct 11 '13 at 0:49
If you're sending personalised emails you're not sending several emails at once: you're sending them one at a time. Most hosts will limit the number you can send per hour or per day, or both. I'd suggest you look at persistent connections to improve throughput, but if you're struggling with foreach I doubt you're ready for that. –  Mike W Oct 11 '13 at 0:49
add comment

1 Answer

up vote 3 down vote accepted

Put the mailing code in a function, e.g. send_mail(), so that it can be called from different places. Then change your database query loop to:

while ($row = mysql_fetch_assoc($result)) {
    send_mail($row['name'], $row['email'), "Text of the email");
share|improve this answer
Thanks so much! That makes sense and simplifies it. –  user2201182 Oct 16 '13 at 4:28
add comment

Your Answer


