Using PHP's mail for Multple Posted Email Addresses - how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Using PHP's mail for Multple Posted Email Addresses - how?

Post by simonmlewis »

Code: Select all

$to = "".$_POST['email1'] . "," . $_POST['email2'] . "," . $_POST['email3'] . "," .$_POST['email4'] . "," .$_POST['email5'];
Hi.
I am building a "tell a friend" system, and want to give the user the option to tell up to 5 friends.

But... this isn't working and I am sure it's because of this part of the code.

Surely I don't have to do 5 IF statements based on whether email2 != NULL, or email3 !=NULL etc...??

It's probably an easy one to answer but I can't work it out.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: Using PHP's mail for Multple Posted Email Addresses - how?

Post by insight »

simonmlewis wrote:

Code: Select all

$to = "".$_POST['email1'] . "," . $_POST['email2'] . "," . $_POST['email3'] . "," .$_POST['email4'] . "," .$_POST['email5'];
Hi.
I am building a "tell a friend" system, and want to give the user the option to tell up to 5 friends.

But... this isn't working and I am sure it's because of this part of the code.

Surely I don't have to do 5 IF statements based on whether email2 != NULL, or email3 !=NULL etc...??

It's probably an easy one to answer but I can't work it out.
It probably is from that part of the code. I have never successfully been able to send stuff to multiple emails at once. An easy way to find out if it works or not is by replacing the ".$_POST['email']." (all five of them) with an actual email. If it works then there must be a problem with the $_POST you have written there.
globezone
Forum Newbie
Posts: 7
Joined: Wed Jun 10, 2009 4:57 am

Re: Using PHP's mail for Multple Posted Email Addresses - how?

Post by globezone »

This should help you.

Code: Select all

 
<?php
$can_continue = false;
 
for($i=0; $i<count($tell_a_friend); $i++)
{
    if(!empty($tell_a_friend[$i]) || $tell_a_friend != "")
    $can_continue = true;
}
 
if($can_continue)
{
    $_needle = array();
    
    for($j=0; $j<count($tell_a_friend); $j++)
    {
        if(!empty($tell_a_friend[$j]) || $tell_a_friend[$j] != "")
        {
            $_needle[] = $tell_a_friend[$j];
        }
    }
    
    echo "<pre>";
    var_dump($tell_a_friend);
    
    echo "<br />";
    
    var_dump($_needle);
    echo "</pre>";
}
 
?>
 
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="tell_a_friend[]" /> <br />
    <input type="text" name="tell_a_friend[]" /> <br />
    <input type="text" name="tell_a_friend[]" /> <br />
    <input type="text" name="tell_a_friend[]" /> <br />
    <input type="text" name="tell_a_friend[]" /> <br />
    <input type="submit" />
</form>
 
 
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

[RESOLVED]Re: Using PHP's mail for Multple Posted Email A...

Post by simonmlewis »

It turns out it DID work - the server was just terribly delayed.

So no need for the length code from globe (but thanks).
Basically, if there in an email in each variable, it'll pass it. If there isn't, it just sends it to those where there is an occupied variable.

Simple.

And it works better than the tellafriends you can use from free sites. I tried one yesterday and it never even sent the email!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply