2

So have a single form which only gets the email of the person and sends it to the email address I assigned. But I guess I wrote the code in a wrong way since I am getting errors. I have seen other stackoverflow problems but I am not sure why this isnt working. This is the form in html.

<form style="margin-bottom:50px;" name="contactform" action="contact-form-handler.php" class="news-letter "method="post">
  <div class="subscribe-hide">
    <input type="text" name="email" class="form-control"  placeholder="Email Address"  >
    <button  type="submit"  class="btn"><i class="fa fa-envelope"></i></button>
  </div><!-- /.subscribe-hide -->
</form><!-- /.news-letter -->

and here is the PHP code in a different file.

    <?php
$errors = '';
$myemail = 'masnadhossain@live.com';
   empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}

$email_address = $_POST['email'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission";
$email_body = "You have received a new message. ".
" Here are the details:".
"Email: $email_address\n ";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}

?>

The error I get is server error 500

Masnad Nihit
  • 1,986
  • 2
  • 21
  • 40

2 Answers2

1

Your code have syntax error pleas check it.

Line no 3 should be:

if (empty($_POST['email']))
{
    $errors .= "\n Error: all fields are required";
}
Wiram Rathod
  • 1,895
  • 1
  • 19
  • 41
0

1.) The first problem you have is that you are sending the email address from a form input via post but you did not utilize it.

2.) To and From variables are having the same email address masnadhossain@live.com which is creating conflicts

3.) The Preg_Match for checking Email address validity is errorneous. I have re-written your code below.

If you are posting the users email(eg gmail,yahoomail etc) from a form input, this code will get you going.

In case you want to intialize the email within the code just change

$email_address = strip_tags($_POST['email']);

to may be

$email_address = 'user1@gmail.com';

Finally, the From variable must be the email address pointing to your website address. in this case I think it is masnadhossain@live.com

This code is working. please mark it as correct answer if it solve your problem....

<?php


//Users email address coming from form input eg. gmail,yahoomail etc.

$email_address = strip_tags($_POST['email']);

//validate the email address

$email_val= filter_var($email_address, FILTER_VALIDATE_EMAIL);
if (!$email_val){
echo "<font color=red><b>Invalid Email Address</b></font>";
exit();
}



$to=$email_val;
$subject = "Contact form submission";
$message = "Here is the message;

// set the from variable to any email pointing to your website

$from = "masnadhossain@live.com";
$headers = "From:" . $from;
$sent=mail($to,$subject,$message,$headers);
 if($sent)  {

print "<br><font color=green><b>Your mail was sent Successfully</b></font>";
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');

 } else  {
print "<br><font color=orange><b>We encountered an error sending your mail.</b></font>";

 }
?> 
nackolysis
  • 217
  • 4
  • 13