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("me@myself.com", "testing 123", "$mailbody", "From: Myself<me@myself.com>");
echo "<p>$value</p>";
}

?>
Link to comment
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("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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.