PHP邮件在MAMP中运行但在实际站点上没有(Centos6)

时间:2016-06-17 03:08:41

标签: php html email

我发送一封简单的电子邮件时遇到了麻烦。它适用于MAMP。然而,它一直显示未定义的变量,但它仍然能够发送。

<?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>';
            }
        }
    }
?>

当我在MAMP上使用php 5.6.10发送电子邮件时,我的主机正在使用php 5.3.3。 php版本的问题是错误的还是代码有问题?

这里是html表单的样子,它只有一个字段和一个提交按钮。我使用bootstrap3来设置表单的样式:)

<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>

谢谢! :)

1 个答案:

答案 0 :(得分:0)

试试这个

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!';
    }
}