无法使PHP mail()工作

时间:2014-10-17 08:41:37

标签: php forms email submit

我已经托管了网站,并且我已经创建了表单和PHP,但我无法获得实际发送电子邮件的表单。我非常确定我正确地列出了所有变量。表单确实提交,在用户提交内容后,警报弹出正常并且表单未显示但我从未收到该电子邮件。这是我的代码:

<?php
if (isset($_REQUEST['email']) && isset($_REQUEST['fullname'])) {

//Email information
$admin_email = 'myemail.hotmail.co.uk';
$fullname = $_REQUEST['fullname'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$followup = $_REQUEST['followup'];
$comment = $_REQUEST['comment'];

//send email
mail($admin_email, $fullname, $comment, 'From: ' . $email . 'Phone: ' . $phone . 'Follow up? ' . $followup);

//Email response
$contactThanks = "Thank you for contacting us!";
echo "<script type='text/javascript'>alert('$contactThanks');</script>";
}

//if 'email' variable is not filled out, display the form
else {
?>

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
    <table class='contactForm'>
        <tr>
            <td><label for='fullname'>*Full Name:</label></td>
            <td><input type='text' name='fullname' id='fullname' /></td>
        </tr>
        <tr>
            <td><label for='email'>*Email:</label></td>
            <td><input type='text' name='email' id='email' /></td>
        </tr>
        <tr>
            <td><label for='phone'>Phone:</label></td>
            <td><input type='tel' name='phone' id='phone' /></td>
        </tr>
        <tr>
            <td><label for='followup'>Follow Up:</label></td>
            <td><input type='text' name='followup' id='followup' /></td>
        </tr>
        <tr>
            <td><label for='comments'>Comments:</label></td>
            <td><textarea type='text' name='comments' id='comments'></textarea></td>
        </tr>
        <tr>
            <td id='inputButtons' colspan='2'>
                <input type='submit' value='Submit' id='submit'/>
                <input type='reset' value='Reset' id='reset'/>
            </td>
        </tr>
    </table>
</form>
<?php
}
?>

任何人都可以看到为什么这不起作用?感谢。

1 个答案:

答案 0 :(得分:0)

将邮件功能的返回值分配给变量,并检查邮件是否实际发送。例如:

$sent = mail($admin_email, $fullname, $comment, 'From: ' . $email . 'Phone: ' . $phone . 'Follow up? ' . $followup);

if ($sent) {
    // thanks, mail sent
} else {
    // error, mail failed to send
}

我怀疑您的服务器上没有安装邮件服务器(例如sendmail)。