change Posted April 27, 2006 Share Posted April 27, 2006 When i run this script below it prints out correctly on screen (echo $value) and gives me thefollowing:milkeggsbreadHowever, the emails that I receive for each array value is incorrect. It should send me3 separate emails that have just one of each value in them. In other words, one emailwould 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 thelast one has milk, eggs, and bread.Please help! :)<?php$array = array('milk','eggs','bread');foreach ($array as $array => $value) { $mailbody.="$value\n\n"; mail("me@myself.com", "testing 123", "$mailbody", "From: Myself<me@myself.com>"); echo "<p>$value</p>";}?> Quote Link to comment Share on other sites More sharing options...
superpimp Posted April 27, 2006 Share Posted April 27, 2006 [!--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 thefollowing:milkeggsbreadHowever, the emails that I receive for each array value is incorrect. It should send me3 separate emails that have just one of each value in them. In other words, one emailwould 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 thelast one has milk, eggs, and bread.Please help! :)<?php$array = array('milk','eggs','bread');foreach ($array as $array => $value) { $mailbody.="$value\n\n"; mail("me@myself.com", "testing 123", "$mailbody", "From: Myself<me@myself.com>"); 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] Quote Link to comment Share on other sites More sharing options...
change Posted April 27, 2006 Author Share Posted April 27, 2006 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? Quote Link to comment Share on other sites More sharing options...
koencalliauw Posted April 27, 2006 Share Posted April 27, 2006 try this:[code]<?php$arr = array('milk','eggs','bread');foreach ($arr as $value) {$mailbody="$value\n\n";mail("me@myself.com", "testing 123", "$mailbody", "From: Myself<me@myself.com>");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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.