Jump to content

loop through array - send email


change

Recommended Posts

When i run this script below it prints out correctly on screen (echo $value) and gives me the
following:

milk
eggs
bread

However, the emails that I receive for each array value is incorrect. It should send me
3 separate emails that have just one of each value in them. In other words, one email
would have milk, the next would have eggs, the next would have bread.

However, what I'm getting is one email that has milk, the next has milk and eggs, and the
last one has milk, eggs, and bread.

Please help! :)

<?php

$array = array('milk','eggs','bread');

foreach ($array as $array => $value) {

$mailbody.="$value\n\n";
mail("[email protected]", "testing 123", "$mailbody", "From: Myself<[email protected]>");
echo "<p>$value</p>";
}

?>
Link to comment
https://forums.phpfreaks.com/topic/8559-loop-through-array-send-email/
Share on other sites

[!--quoteo(post=369272:date=Apr 27 2006, 06:24 PM:name=change)--][div class=\'quotetop\']QUOTE(change @ Apr 27 2006, 06:24 PM) [snapback]369272[/snapback][/div][div class=\'quotemain\'][!--quotec--]
When i run this script below it prints out correctly on screen (echo $value) and gives me the
following:

milk
eggs
bread

However, the emails that I receive for each array value is incorrect. It should send me
3 separate emails that have just one of each value in them. In other words, one email
would have milk, the next would have eggs, the next would have bread.

However, what I'm getting is one email that has milk, the next has milk and eggs, and the
last one has milk, eggs, and bread.

Please help! :)

<?php

$array = array('milk','eggs','bread');

foreach ($array as $array => $value) {

$mailbody.="$value\n\n";
mail("[email protected]", "testing 123", "$mailbody", "From: Myself<[email protected]>");
echo "<p>$value</p>";
}

?>
[/quote]
Well, that should work, I did it like that on my mail script too.

here's part of the code, hope you can use it :)
basicly it's a form for a mail bomber but if you replace $counter with $mailbody, it should work too..

[code]
<?php

$to=$_POST['to'];
$from=$_POST['from'];
$reply=$_POST['reply'];
$headers="From: ".$from."\r\nReply-To: ".$reply;
$subject=$_POST['subject'];
$message=$_POST['textarea'];
$num=$_POST['num'];
$counter=range(1,$num);


foreach($counter as $counter)
{
        mail($to, $subject, $message, $headers)){
    echo "<title>Mail sent!</title>\r\n";
    echo "<p>Mail n° ".$counter." succesfully sent!\r\n";
}
?>
[/code]
Superpimp,

Actually, the problem isn't in the sending of the mail. It's in the loop or array. I want it to email each of the different items from the array separately.

The way it's doing it now, it's sending one email that has 1 array result, then it's sending a second email that has 2 array results, then it's sending a third email that has 3 array results.

Any ideas?
try this:
[code]
<?php

$arr = array('milk','eggs','bread');

foreach ($arr as $value) {

$mailbody="$value\n\n";
mail("[email protected]", "testing 123", "$mailbody", "From: Myself<[email protected]>");
echo "<p>$value</p>";
}

?>
[/code]

.= means it appends a value to the previous value so the behaviour you had is normal. Probably your arguments for the foreach loop were correct, but this is simpler I think.

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.