0

I am trying to create a simples subscriber email form , where one user inserts his email, and me and the client should receive a e-mail.

Email to me saying that got a new subscription. Email to client saying that we will contact him shortly.

I know very little about php, i ask for help about what is wrong on this code and how to make it better in order to make what i want.

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 Answers2

0

Edit your html code..

<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>
Sailesh Kotha
  • 1,324
  • 1
  • 16
  • 27
0

I would have to say first your HTML has an issue. With the Get started now. You're not actually submitting the form, you're just linking back to the page you're on.

So first change the 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>

Secondly, here's a neater version of the PHP with some improvements.

$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'] . ')';
}

If the rest doesn't work, make sure that sendmail is installed and setup correctly on the server.

Just to point out that if the mail check returns false it might not specifically be the e-mail that's at fault as your output error message suggests.

Greg
  • 113
  • 1
  • 13
  • Hello, i get no errors now. About the email i think im doing something wrong, what do i need to setup ? I didnt created anything relationed with email. – Duarte Andrade Jun 25 '15 at 13:58
  • Is this on your local computer or a remote server? webhosting? – Greg Jun 26 '15 at 14:57