0

I'm having trouble sending out a simple email out with this. It works on MAMP. However it keeps displaying undefined variable, but it was still able to send.

<?php
    if (isset($_POST["submit"])) {
        $email = $_POST['email'];
        $from = 'test@test.com'; 
        $to = 'receipt@recieved.com'; 
        $subject = 'New Message';
        $body = "New Email: $email";


        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        if (!$errEmail) {
            if (mail ($to, $subject, $body, $from)) {
                $result='<div class="alert alert-success">Thank You</div>';
            } else {
                $result='<div class="alert alert-danger">Sorry there was an error sending your email. Please try again later</div>';
            }
        }
    }
?>

The email sends out when I'm on MAMP using php 5.6.10 but my host is using php 5.3.3. Is the issue wrong with the php version or is there something wrong with the code?

Here's what the html form looks like, it has just one field and a submit button. I'm using bootstrap3 to style the forms :)

<form class="form" role="form" method="post" action="index.php">
    <div class="form-group">
        <label for="email" class="control-label"></label>
        <div>
            <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
        </div>
    </div>
        <div class="form-group">
        <div class="text-center">
            <input id="submit" name="submit" type="submit" value="Submit" class="btn btn-primary">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-1">
            <?php echo $result; ?>  
        </div>
    </div>
</form>

Thanks! :)

user3335966
  • 2,673
  • 4
  • 30
  • 33
fupuchu
  • 307
  • 1
  • 2
  • 11

1 Answers1

0

Try this

if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    if ($errEmail == "") {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your email. Please try again later</div>';
        }
    }
    else {
        $errEmail = 'Something wrong!';
    }
}
MuthaFury
  • 805
  • 1
  • 7
  • 22