-2

I'm trying to figure out a html/php code that enables the user to send invitations for email addresses that he enters. The user has to only type in the email address then click the button "invite", after that the invitation will be sent. I have five input fields by which I need to write a php code to take the inserted email addresses and send invitations for

here is my index.php file:

<form method="post" action="test.php">
 <input name="email" type="email" size="30" placeholder="email address of friend 1"><br> 
<input name="email2" type="email" size="30" placeholder="email address of friend 2"><br>
<input name="email3" type="email" size="30" placeholder="email address of friend 3"><br>
<input name="email4" type="email" size="30" placeholder="email address of friend 4"><br>
<input name="email5" type="email" size="30" placeholder="email address of friend 5"><br><br>

<input name="sendername" type="text" size="30" placeholder="Your Name"><br>

<input type="submit" name="invite" value="Invite"> </form>

I wrote this code in the test.php file to process the code for the first email, but it gives me the return message " message couldn't be sent" any advice ?

<?php 

if(isset($_POST['invite'])) {

    // CHANGE THE TWO LINES BELOW

 $to = $_POST['email'];;

   $subject = "This is subject";
   $message = "This is simple text message.";
   $header = "From:abc@somedomain.com\r\n";
   $retval=@mail ($to,$subject,$message,$header);

   if( $retval == true )  
   {  echo "Message sent successfully...";
   }
   else
   { echo "Message could not be sent...";
   }


}


?>
Rumanoid
  • 139
  • 1
  • 3
  • 15
  • Your headers are not set. See answer here http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php – iKey May 03 '15 at 14:49
  • 1
    Sidenote: Sure hoping that everyone knows each other. Otherwise, the spam cops will be on your a** real fast. – Funk Forty Niner May 03 '15 at 14:51
  • @iKey my code works only if I give the $to variable an email address and write it inside the code instead of getting it from the input field, like this $to= someemail@somedomain.com , but what I need is to get that email address from the text field ,, so the problem is not with the header – Saif Harbia May 03 '15 at 14:56
  • Firstly, you're suppressing possible errors in your code using `@` in `@mail`, so remove it. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner May 03 '15 at 15:01
  • @SaifHarbia Now what is your problem!!! – ops May 03 '15 at 15:03
  • You're also not doing anything with the other inputs nor are you using `isset()` or `!empty()`; there are no POST arrays assigned to what you want to use. Re-think this. What you need is a `foreach`; that's a clue. – Funk Forty Niner May 03 '15 at 15:04
  • @Fred-ii- I noticed my mistaken use of @ sign and removed it before your comment :D, I also added the reporting functions, but the result still the same – Saif Harbia May 03 '15 at 15:04
  • what you need to do is name all your email recipient inputs to and as an array `name="email[]"` then do a `foreach`. But as I said already, you better make sure everyone knows each other, because you will be hit by the spam police real fast. – Funk Forty Niner May 03 '15 at 15:08

2 Answers2

0

You need to create array as input like this:.

<input name="email[]" type="email" size="30" placeholder="email address of friend 1"><br> 
<input name="email[]" type="email" size="30" placeholder="email address of friend 2"><br>
<input name="email[]" type="email" size="30" placeholder="email address of friend 3"><br>
<input name="email[]" type="email" size="30" placeholder="email address of friend 4"><br>
<input name="email[]" type="email" size="30" placeholder="email address of friend 5"><br>

And in PHP you can access created array with foreach like this:

<?php
if(isset($_POST['invite'])) {
    // CHANGE THE TWO LINES BELOW
    $subject="This is subject";
    $message="This is simple text message.";
    $email='abc@somedomain.com';
    $header='From: '.$email."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/'.phpversion();
    foreach($_POST['email'] as $value) {
        $retval=@mail($value,$subject,$message,$header);
    }

    if($retval==true) {  echo "Message sent successfully...";
    } else { echo "Message could not be sent...";
    }

}
?>
ops
  • 2,023
  • 18
  • 21
  • Sure, that would work. However, and if I were you, I'd put a disclaimer in there, as I told the OP about how everyone needs to know each other. Otherwise, the spam cops will be dead on their tail ;-) – Funk Forty Niner May 03 '15 at 15:13
  • @yones safari your code helped me a lot to get where I want, splendid, it only misses an (if statement) for the purpose of reaching the email array :DD – Saif Harbia May 03 '15 at 15:26
-1

After indulging more into this issue and attempting to resolve it, I figured out how to do the job. Briefly I had to use foreach() supplied with an if statement to loop over the email array. my final code looks like this :

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
    if(isset($_POST['invite'])) {

        // CHANGE THE TWO LINES BELOW

        $to=$_POST['email'];
        $subject="This is subject";
        $message="This is simple text message.";
        $email='abc@somedomain.com';
        $header='From: '.$email."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/'.phpversion();

        if (is_array($to )){
        foreach($to as $value) {
         @mail($value,$subject,$message,$header);
    }

        }
           {  
                  echo "Message sent successfully...";
        } 


    }
?>