March 12, 2012

two foreach loops combined for email sending

Question by Bowser Entertainment

I am trying to take a two text areas with multiple emails, one with the “from” emails, and one with the “to” emails. Combine the emails line by line and send emails out accordingly.

Ex:

“To” list:

        mike@gmail.com

        nick@hotmail.com

        adam@yahoo.com

“From” list:

        ashley@gmail.com

        brittney@yahoo.com

        racheal@hotmail.com

I want a email sent to:

   mike@gmail.com from ashley@gmail.com

   nick@hotmail.com from brittney@yahoo.com

   adam@yahoo.com from racheal@hotmail.com

Any help would be greatly appreciated. Thanks in advanced.

Below is the script I got so far, It sends to multiple emails from one email.

 <?php
 if (isset($_POST['submit']))
 {

    // Execute this code if the submit button is pressed.
    $raw_email_account = $_POST['email_from'];
    $email = $_POST['email_to'];
    $sent = "";
    $invalid = "";

    //Separating each line to be read by foreach
    $list = explode("n",$email);

    //Rendering the separeted data from each line
    foreach($list AS $data) {
                //Separating each line to be read by foreach
            $item = explode(":",$data);
            $mail_body = '<html><body>email here</body></html>';
                $subject = "subject here";
                $headers  = "From:".$raw_email_account."rn";
                $headers .= "Content-type: text/htmlrn";
                $to = $item[0];

                $mail_result = mail($to, $subject, $mail_body, $headers);

            if ($mail_result) {
               $valid++;
            } else {
                   // write into an error log if the mail function fails
                   $invalid++;
            }
    }

}
?>
<html>
<head>
</head>
<body>
<form action="email_sender.php" method="POST">
<div align="center">From Email Accounts: <textarea name="email_from" cols="100"    rows="60"></textarea></div><br />
<div align="center">To Email Accounts: <textarea name="email_to" cols="100" rows="60">        </textarea></div><br />
<div align="center"><input type="submit" name="submit"></div>
<br>
Valids: <?php echo $valid;?>
<br>
Invalids: <?php echo $invalid;?>
</body>
</html>

Answer by Vitalmax

Add logic that provides same count of $email and $raw_email_account arrays before foreach loop.

$list = explode("n",$email);
$list2 = explode("n", $raw_email_account);

foreach($list AS $key=>$data) {
            ...
            $headers  = "From:".$list2[$key]."rn";
            ...
}

Answer by Starx

If the array is by default, the indexes will coincide. So you can do something like this.

$toList = array(..);
$fromList = array(...);

foreach($toList as $key => $value) {
    $toAddress = $value;
    $fromAddress = $fromList[$key]; 
    //..
    //.. Go on with you mail function
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!