-1

I've been having an issue with my PHP mail() script. I'm attempting to build a website for my class to use to get homework, know the dates of upcoming tests/coursework, etc.

This script is to send data from a form to my email address. My problem is that: I had attempted to include a Field in my form to increase security, It basically said to insert "65FL7YB" to ensure that the user wasn't a bot, and in my PHP, I wrote the following:

if($_POST['valid'] = "65FL7YB") {
            mail($to , $subject , $message , $header);

            echo "Your Email Has Been Sent.";

        } else {
            echo "Your email has not been sent. Please ensure that you correctly entered the Validation Code.";
        }

But when I proceded to test the script the following occurred: When I typed in the Validation Code correctly, as expected, on the page was printed "Your Email Has Been Sent." But when I typed in an incorrect Validation Code still the same "Your Email Has Been Sent." came up instead of "Your email has not been sent. Please ensure that you correctly entered the Validation Code."

Please help me, I've spent hours trying to work this out (with others who know how to code) but we have not been able the identity the problem.

For extra reference this is the full code of the page:

<?php
        $name = $_POST['name'];
        $date = $_POST['date'];
        $subject = $_POST['subject'];
        $homework = $_POST['homework'];

        $to = "Removed for my Privacy";
        $subject = "Homework Submission";
        $message = "The following has been submitted by $name. /n" . "The subject(s) is/are $subject. /n" . "Today's Homework is: /n $homework /n" . "$date";
        $header = $name;

        if($_POST['valid'] = "65FL7YB") {
            mail($to , $subject , $message , $header);

            echo "Your Email Has Been Sent.";

        } else {
            echo "Your email has not been sent. Please ensure that you correctly entered the Validation Code.";
        }
    ?>
Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

7

Edit: (and do read this in its entirety)

After noticing what you posted in a comment, this:

<input name="name" type="text" placeholder="Insert Your Name or Email" size="97">

You shouldn't use that, you should use a seperate input for an Email, otherwise if there is no From: in the header, the Email will not be sent.

Plus, this line:

<form action="submit-homework-form-email.php" method="post" enctype="text/plain"> 
  • Remove enctype="text/plain" (more on this subject after Footnotes)

to read as:

<form action="submit-homework-form-email.php" method="post"> 

Now, assuming your form contains an input field for someone's Email address,

I added $email = $_POST['email']; plus I added the $valid = $_POST['valid']; to test, otherwise, Email will not be received.

Using this $header = $name; as an Email header() is invalid.

This was also invalid. You were doing an assignment instead of checking if it was "equal to".

if($_POST['valid'] = "65FL7YB") {

instead of:

if($_POST['valid'] == "65FL7YB") {
  • = is an assignment
  • == is equal to

Another thing is /n that needed to be changed to \n

I added the following below, in my tested and working code:

$header = "From: ".$name." <".$email.">\r\n";
$header .= "Reply-To: ".$email."\r\n";

Another thing, you're repeating $subject with $subject = "Homework Submission"; and $subject = $_POST['subject']; I commented out $subject = "Homework Submission";

You can modify it from hereonin.

Reformatted PHP

<?php

    $email = $_POST['email']; // added for testing purposes
    $valid = $_POST['valid']; // added for testing purposes

    $name = $_POST['name'];
    $date = $_POST['date'];
    $subject = $_POST['subject'];
    $homework = $_POST['homework'];

    $to = "Removed for my Privacy";
    // $subject = "Homework Submission";
    $message = "The following has been submitted by $name. \n" . "The subject(s) is/are $subject. \n" . "Today's Homework is: \n $homework \n" . "$date";

    $header = "From: ".$name." <".$email.">\r\n";
    $header .= "Reply-To: ".$email."\r\n";

        if($_POST['valid'] == "65FL7YB") {
            mail($to , $subject , $message , $header);

            echo "Your Email Has Been Sent.";

        } else {
            echo "Your email has not been sent. Please ensure that you correctly entered the Validation Code.";
        }
?>

Footnotes:

You should also be checking if any of the form elements are SET or are EMPTY which you are not doing.

If someone does not fill out the subject field, chances are that Email will not be received or will end up in SPAM.

It's best to use something like this:

if(empty($_POST['subject'])){ die("You must enter a subject."); }

or

if(empty($_POST['subject'])){ echo "You must enter a subject."; exit; }

Or use $subject = "Homework Submission"; instead of $subject = $_POST['subject']; that way you don't need to use a conditional statement for the subject.


About enctype="text/plain"

enctype defines how the data should be formatted before sending. The two correct formats are application/x-www-form-urlencoded (which essentially sends things as key=value&anotherkey=anothervalue, with http headers) and multipart/form-data (which splits each key up into a distinct section of data). text/plain means nothing should be done - its behaviour is essentially undefined between browsers, and was only ever used for automated email forms in the days before spam.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
3

You are using the assignment operator = instead of the comparison operator ==.

Change:

if($_POST['valid'] = "65FL7YB") {

To:

if($_POST['valid'] == "65FL7YB") {
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
R R
  • 2,999
  • 2
  • 24
  • 42
1

Try this:

Changes your if statement in php code.

It should be like this ....

if($_POST['valid'] == "65FL7YB")

Remember = is assignment operator and == is comparison operator. In if statement you have to use compare operator.

Thanks!

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
  • I tried this but now the function only executes the else statement, whether the Validation code was entered correct, incorrectly, or not at all. – TheTriniFlyer Dec 31 '13 at 04:49
  • Pl check both values ($_POST['valid'] && 65FL7YB) before check in if statement. If both are equal than if statement will execute. – Anand Solanki Dec 31 '13 at 05:07