0

I am having trouble with my PHP code. I use IF-ELSE to check that everything goes ok but it keeps giving me the "You did not enter a recipient".

<?php

$to=trim($_POST['toperson']);
$from=trim($_POST['spoofrom']);
$message=trim($_POST['message']);
$subject=trim($_POST['subj']);


if (substr_count($to, '@') <= 1 ) {
    if (!isset($to)) {
        if (!isset($from)) {
            if (!isset($subject)) {
                if (!isset($message)) {
                    mail($to, $subject, $message, "From: " . $from);
                    print "Message was sent!";
                }else {print "You did not enter a message";} 
            }else {print "You did not enter a subject";}
        }else {print "You did not enter a from email";}
    }else {print "You did not enter a recipient";}
}else{print "You entered 2 or more emails.";}

?>
2
  • did you check your recipient's textbox name in the form? Maybe it is mistyped.
    – zkanoca
    Apr 6, 2013 at 12:09
  • 1
    Please learn about mail header injection. Apr 6, 2013 at 12:10

4 Answers 4

1

Try

replace your conditions if (!isset($to)) by if (isset($to)) And add the empty check

Doc: http://php.net/manual/en/function.isset.php

http://www.php.net/manual/en/function.empty.php

Like this:

if (substr_count($to, '@') <= 1 ) {
    if (isset($to) && !empty($to)) {
        if (isset($from) && !empty($from)) {
            if (isset($subject) && !empty($subject)) {
                if (isset($message) && !empty($message)) {
                    mail($to, $subject, $message, "From: " . $from);
                    print "Message was sent!";
                }else {print "You did not enter a message";} 
            }else {print "You did not enter a subject";}
        }else {print "You did not enter a from email";}
    }else {print "You did not enter a recipient";}
}else{print "You entered 2 or more emails.";}
4
  • that does not make any sense. As there is already an assigment $to = ...;, so the isset-call is ALWAYS true (if the key existed e.g. it isn't null). Check for empty...
    – bwoebi
    Apr 6, 2013 at 12:12
  • @bwoebi: Indeed. The assignment will fail with an 'undefined index' error message is the URL parameter is not present. Apr 6, 2013 at 12:14
  • Yes that's what I said: "(if the key existed e.g. it isn't null)". Btw. I don't think he wants to check if the form was manipulated, but if all the fields were filled.
    – bwoebi
    Apr 6, 2013 at 12:15
  • @bwoebi: But (s)he should want to check that, too. E.g., it's easy to inject extra headers through the from-field and send spam to others. Apr 6, 2013 at 12:17
0

Your code will send mail if your all form data are empty. if(!isset($_POST["something"]) means the conditional process will fire if your data is NOT set.

Remove exclamations.

0

You eventually mean: empty() instead of isset() as it IS always set… but not always filled?

Or check if isset($_POST['to']) and the other keys, but don't use isset on assigned variables (a check is_null / === null is better there

0

Make sure your form has proper validation using regex &| html5/css3 prior to form submission. use !empty() verse isset() lastly, I'd suggest putting

if (mail($to, $subject, $message, "From: " . $from)) { 
   print "Message was sent!";
} else { print "email failed to send!"; }

Also, I'd put the From: when setting the variable not calling it, but that's more personal preference.

$from= "From: trim($_POST['spoofrom'])";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.