-1

I'm a PHP newbie and wondering if there is someone out there with skills who can help me with this? It's sending blank emails to my inbox once the information is submitted by a user (me in this case). Also, no subject is showing. I believe my question is different because of it's specific code with the added radio feature, which helps me understand it better and learn from my mistakes. Thanks in advance.

My PHP Code:

<?php

if(!isset($_POST['submit'])) {
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}

$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$email_from = 'testtike@yahoo.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body =   "You have received a new message from the user $name.\n" 
                . "Here is the message:\n $message".

$to = "testtike@yahoo.com"; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

//Send the email!
mail($to, $subject, $body, $from, $tel);

//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str) {

    $injections = array('
        (\n+)',
        '(\r+)',
        '(\t+)',
        '(%0A+)',
        '(%0D+)',
        '(%08+)',
        '(%09+)'
    );

    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    } else {
        return false;
    }
}

My HTML:

<label>Name</label>
<input name="name" placeholder="Type Here">

<label>Email</label>
<input name="email" type="email" placeholder="Type Here">

<label>Phone Number</label>
<input name="tel" type="phone" placeholder="Type Here">

<label>Message/Comments/Questions</label>
<textarea name="message" placeholder="Type Here"></textarea>



<label>Are you retired?</label> <br>

<div id="button">
<input type="radio" name="retired" value="yes"> Yes.
<input type="radio" name="retired" value="no"> No. 
<input type="radio" name="retired" value="na"> Does not apply.<br><br>
</div>


<label>Are you a veteran?</label> <br>
<div id="button">
<input type="radio" name="vet" value="yes"> Yes.
<input type="radio" name="vet" value="no"> No. 
<input type="radio" name="vet" value="na"> Does not apply.<br><br>

</div>


<label>Military Family Member?</label> <br>
<div id="button">
<input type="radio" name="family" value="yes"> Yes.
<input type="radio" name="family" value="no"> No. 
<input type="radio" name="family" value="na"> Does not apply.<br>
</div>

<label> Do You Have A Copy of Your DD214?</label> <br>
<div id="button">
<input type="radio" name="dd" value="yes"> Yes.
<input type="radio" name="dd" value="no"> No. 
<input type="radio" name="dd" value="na"> Does not apply.<br>
</div>

<input type="submit" name='submit' value="submit">


</form>       
MRcodes
  • 13
  • 3
  • 1
    `mail` function has incorrect values. – chris85 Aug 12 '15 at 20:14
  • 1
    Simple: Your mail parameters are incorrect. Please read through the manual before posting http://php.net/manual/en/function.mail.php - Remember, `From:` is an important part here and your code's not using it properly. – Funk Forty Niner Aug 12 '15 at 20:14
  • 1
    Thank you guys. Excuse me for the repetitive question. I feel stupid. I literally started learning php last week. I had no idea what I am doing but I am slowly understanding it more with your guys help here and will rely on the manual more and check my code better, as you guys have shared. Thank you again. – MRcodes Aug 12 '15 at 20:52

1 Answers1

1

Change your mail() call to this:

mail($to, $email_subject, $email_body, $headers);

You had incorrect variable references.

Additionally, you were passing a fifth argument to mail(). Although that's valid, I believe you may have been mistaken in your execution. The fifth argument is meant to pass additional flags to the sendmail process.

See the PHP Docs for more info.

Stuart Wagner
  • 1,997
  • 1
  • 14
  • 22
  • 1
    Got it, thank you very much. Such a stupid mistake on my part with the incorrect variable references. But I had no idea about limited parameters until now. Thank you. – MRcodes Aug 12 '15 at 20:54