简单的订阅者电子邮件表单 - PHP / HTML

时间:2015-06-25 11:10:32

标签: php html email

我正在尝试创建一个简单的订阅者电子邮件表单,其中一个用户插入他的电子邮件,我和客户端应该收到一封电子邮件。

给我发电子邮件说有新订阅。 发送电子邮件给客户说我们会尽快与他联系。

我对php知之甚少,我请求帮助解决这段代码出了什么问题,以及如何让它更好地制作我想要的东西。

HTML:

<div class="mail2">
  <!-- Subscription Form -->
  <form action="form/form.php" method="post">
    <h1>Try Now!</h1>
    <input name="email" class="email" type="text" placeholder="Enter your email address ...">
    <a href="#">Get started for free</a>
    <a class="top" href="#top">Top</a>
  </form>
</div>

PHP:

    <?php
$to = "office@site.com";
$from = "no-reply@site.com";

$headers = "From: " . $from . "\r\n";

$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];


if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{ 
    if (mail($to, $subject, $body, $headers, "-f " . $from))
    {
        echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
    }
    else
    {
       echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';   
    }
}
else
{
   echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';   
}

2 个答案:

答案 0 :(得分:0)

编辑你的HTML代码..

<div class="mail2">
  <!-- Subscription Form -->
  <form action="form/form.php" method="post">
    <h1>Try Now!</h1>
    <input name="email" class="email" type="text" placeholder="Enter your email address ...">
    <input type="submit" value="Get started for free">
    <a class="top" href="#top">Top</a>
  </form>
</div>

答案 1 :(得分:0)

我首先要说你的HTML有问题。随着入门现在。您实际上并未提交表单,而只是链接回您所在的页面。

首先更改HTML:

<div class="mail2">
  <!-- Subscription Form -->
  <form action="form/form.php" method="post">
    <h1>Try Now!</h1>
    <input name="email" class="email" type="text" placeholder="Enter your email address ...">
    <a href="#">Get started for free</a>
    <a class="top" href="#top">Top</a>
  </form>
</div>

其次,这是一个更整洁的PHP版本,有一些改进。

$to = "office@site.com";
$from = "no-reply@site.com";

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $from;
$headers[] = "Reply-To: " . $from;
$headers[] = "X-Mailer: PHP/".phpversion();

$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];

if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
   if (mail($to, $subject, $body, implode("\r\n", $headers)))
   {
      echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
   }
   else
   {
      echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
   }
}
else
{
   echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}

如果其余部分不起作用,请确保在服务器上正确安装并设置了sendmail。

只是要指出,如果邮件检查返回false,则可能不是特定的电子邮件,因为输出错误消息显示该错误。