How do I get this to work PHP form sender

221 views Asked by At

Can anyone see what is wrong with my PHP form sender code:??

My code is listed below - the form always says no matter what, incorrect email address. Thus, it is broken. Much appreciate your help!

$to = "[email protected]";
$from = "[email protected]";

$headers = "From: " . $from . "\r\n";

$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];


if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 

    if (mail($to, $subject, $body, $headers, "-f " . $from)) {
        echo 'Alright ! You will be notified on <b>  ' . $_POST['email'] . '</b> :)';
    }
    else {
        echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';  
    }
}
else {
    echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';  
}
2

There are 2 answers

0
Giacomo1968 On

What is the incorrect e-mail & what are you trying to do?

Look at this:

if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 

    if (mail($to, $subject, $body, $headers, "-f " . $from)) {

While the if is checking for $_POST['email'], the mail command is using $to as the variable. Huh? Maybe your code should be:

if (filter_var($to, FILTER_VALIDATE_EMAIL)) { 

    if (mail($to, $subject, $body, $headers, "-f " . $from)) {
        echo 'Alright ! You will be notified on <b>  ' . $to . '</b> :)';
    }
    else {
        echo 'There was a problem with your e-mail (' . $to . ')';  
    }
}
else {
    echo 'There was a problem with your e-mail (' . $to . ')';  
}

But then again, why are you doing this via a form if you are not going to use the $_POST form variables?

0
Joshua Fabillar On

I think the problem is the $_POST['email']. echo it out so you could see if the $_POST['email'] is in correct format for email, because the filter_var checks that..