Reputation: 3
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.";}
?>
Upvotes: 0
Views: 308
Reputation: 112
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'])";
Upvotes: 0
Reputation: 11117
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.";}
Upvotes: 1
Reputation: 23787
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
Upvotes: 0
Reputation: 9928
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.
Upvotes: 0