【问题标题】:Having trouble getting a PHP mail() to work [duplicate]无法让 PHP mail() 工作[重复]
【发布时间】:2014-10-17 08:41:37
【问题描述】:

我已经托管了网站,并创建了表单和 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
}
?>

谁能明白为什么这不起作用?谢谢。

【问题讨论】:

  • 提供您发送的输入信息。管理员电子邮件格式错误 $admin_email = 'myemail.hotmail.co.uk';
  • 是的,抱歉,这是一项糟糕的工作,我删除了我的实际电子邮件,并用一个替代邮件代替了帖子,在我的代码中它的格式正确。

标签: php forms email submit


【解决方案1】:

将邮件函数的返回值赋值给一个变量,并检查邮件是否真的被发送。例如:

$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)。

【讨论】:

  • 更新:刚刚开始工作,mail() 的代码是一样的,我不得不尝试一些设置,但现在的问题是它把所有信息都从邮件中( ) 进入电子邮件的“发件人”部分。
猜你喜欢
  • 2012-05-06
  • 1970-01-01
  • 2012-06-15
  • 2012-03-23
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多