Shivam
Shivam

Reputation: 2248

Form not sending email

I am having trouble sending the information from my PHP form to the email address. I am fairly new to PHP. Code is below:

if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$myEmail = "[email protected]";

if (empty($name) || empty($subject) || empty($message)) {
  $error = 'Please make sure to double check the fields for errors.';
} elseif (!filter_var($email1, FILTER_VALIDATE_EMAIL)) {
  $error = 'Email is incorrect';
} else {
  $headers .= "From: $email\r\n";
  $headers .= "Reply-To: $myEmail\r\n";
  $headers .= "Return-Path: $myEmail\r\n";
  $headers .= "CC: $email\r\n";
  $headers .= "BCC: $myEmail\r\n";

  if ( mail($to,$subject,$message,$headers) ) {
    $to = $myEmail;
    $subject = $subject;
    $message = $message;
    $from = $email;
    $headers = "From:" . $from;
    echo "Mail Sent.";
  } else {
    echo 'failure to send email';
  }
}
}

HTML:

<form id="contactForm" class="form-horizontal" action="<?php echo get_option('home'); ?>/email/"  method="POST">
    <input id="name" name="name" placeholder="Full Name" type="text">
    <input id="subject" name="subject" placeholder="Subject" type="text">
    <input id="email" name="email" placeholder="Email Address" type="email">
    <textarea placeholder="Your Message" id="message" name="message" rows="10"></textarea>
    <input type="submit" value="SEND" class="btn btn-primary btn-block">
</form>

NOTE: I am using WP CMS.

Upvotes: 0

Views: 336

Answers (6)

Hunter MacDermut
Hunter MacDermut

Reputation: 27

There's a . (period) missing from the first $headers variable declaration. Might help.

Upvotes: 0

user2727841
user2727841

Reputation: 725

$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;
if(@mail($to, $subject, $message, $headers)) {
   echo "Mail Sent";
} else {
   echo "Fail";
}

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Change your code.It was sending mail by 2times

$to = $myEmail;
$subject = $subject;
$message = $message;
$from = $email;
$headers = "From:" . $from;

if ( mail($to,$subject,$message,$headers) ) {

    echo "Mail Sent.";
} else {
    echo 'failure to send email';
}

And your form method is like POST

<form id="contactForm" class="form-horizontal" action="contact.php" method="post">

And main thing your file name is either contact.php or contact.tpl NOT contact.tpl.php

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

You have wrong parameter $to in mail()

Try

....
....
//// use $email here not $to which is not initialised yet
if ( mail($email,$subject,$message,$headers) ) {

is place of

 if ( mail($to,$subject,$message,$headers) ) {

Upvotes: 0

Leo T Abraham
Leo T Abraham

Reputation: 2437

Form method POST is missing in the form tag.

<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="post">

Upvotes: 0

DevZer0
DevZer0

Reputation: 13525

Your form is missing the method attribute. edit the code so that your form has method POST.

<form id="contactForm" class="form-horizontal" action="contact.tpl.php" method="POST">

secondly remove one of your mail function calls. if not your email will be sent twice

Upvotes: 1

Related Questions