AmyLL AmyLL - 1 month ago 9
PHP Question

Send Mail [PHP]

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.";}

?>

Answer

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.";}
Comments