使用php无法正常发布联系表单到电子邮件

时间:2014-08-19 05:50:50

标签: php forms email

我已经创建了即将推出的'我的新网站的登陆页面。在网站设计和开发完成之前,我只想收集感兴趣的各方电子邮件地址 - 这应该直接提交给我的电子邮件。我写了下面的代码,但是在测试过程中我没有收到任何发送到我地址的电子邮件:

HTML:

form method="post" name="myemailform" action="emailform.php">
<div id="subscribe">
    <input type="text" placeholder="enter your email address..." name="emailAddress" id="emailAddress">
    <input type="submit" value="Submit">
    <div class="clear"></div>
  </div>
</form>

PHP:

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to hit submit";
}
$visitor_email = $_POST['emailAddress'];
$email_from = $_POST['emailAddress'];

// Validate FIRST
if(empty($visitor_email))
{
    echo "Email address is required";
    exit;
}

$email_from = 'myemail@gmail.com';
$email_subject = "New Form";
$email_body = "You have received a new notifcation from $visitor_email.\n".

$to = "myemail@gmail.com";
$headers = "From: $email_from \r\n"

// SEND EMAIL
mail($to,$email_subject,$email_body,$headers);
?>

这是我第一次尝试服务器端脚本。你能在我的代码中看到任何不正确的内容吗?

4 个答案:

答案 0 :(得分:2)

您的条件声明

if(!isset($_POST['submit']))

正在寻找名为的提交按钮:

<input type="submit" value="Submit">

应该是

<input type="submit" value="Submit" name="submit">

如果这是您的实际代码,您似乎错过了<的{​​{1}}。

您还在form method="post"

中错过了分号

应为$headers = "From: $email_from \r\n"

另外,

$headers = "From: $email_from \r\n";

删除末尾的点并用分号替换

$email_body = "You have received a new notifcation from $visitor_email.\n".

并使用错误报告

$email_body = "You have received a new notifcation from $visitor_email.\n";

放在您的打开error_reporting(E_ALL); ini_set('display_errors', 1); 标记之前,如果在代码中找到错误,则会发出错误信号。


旁注:

您不需要<?php

答案 1 :(得分:1)

您是从本地计算机还是从托管服务器运行测试? 可以在此处找到发送电子邮件的要求:http://php.net/manual/en/mail.requirements.php

答案 2 :(得分:1)

你有权访问php.ini吗?您需要在服务器中安装有效的电子邮件系统或访问php.ini以配置SMTP参数。你在运行什么操作系统?

答案 3 :(得分:-1)

您需要在计算机上安装SMTP服务器才能发送邮件。