1

I am having problems getting my contact.php form to work. Before I was not getting the message to my email at all, now, the email does get sent, always show me:

"Message failed. Please, send an email to treneree@gmail.com" this is all it shows:

Form:

<form action="contact.php" method="post" class="tm-contact-form">
    <div class="form-group">
        <input name=”cf_name” type="text" id="contact_name" class="form-control" placeholder="NAME..." />
    </div>
    <div class="form-group">
        <input name=”cf_email” type="text" id="contact_email" class="form-control" placeholder="EMAIL..." />
    </div>
    <div class="form-group">
        <textarea name=”cf_message” id="contact_message" class="form-control" rows="8" placeholder="WRITE A MESSAGE..."></textarea>
    </div>
    <button type="submit" class="btn text-uppercase tm-dark-bg tm-orange-text tm-send-btn">Send</button>
</form>

and php:

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'trener.waw@gmail.com' ;
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to treneree@gmail.com');
        window.location = 'index.html';
    </script>
<?php
}
?>

Anyone?

MarthyM
  • 1,839
  • 2
  • 21
  • 23
siemamordo
  • 11
  • 1

2 Answers2

0

Please add these headers,

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
Mansoor H
  • 594
  • 1
  • 8
  • 25
0

Problem is with your form having special character in your form input name

just replace name=”cf_name” with name="cf_name" in all form name

<form action="" method="post" class="tm-contact-form">
    <div class="form-group">
        <input name="cf_name" type="text" id="contact_name" class="form-control" placeholder="NAME..." />
    </div>
    <div class="form-group">
        <input name="cf_email: type="text" id="contact_email" class="form-control" placeholder="EMAIL..." />
    </div>
    <div class="form-group">
        <textarea name="cf_message" id="contact_message" class="form-control" rows="8" placeholder="WRITE A MESSAGE..."></textarea>
    </div>
    <button type="submit" class="btn text-uppercase tm-dark-bg tm-orange-text tm-send-btn">Send</button>
</form>

If you use name like name=”cf_name”

your post data look like

Array ( [â€cf_nameâ€] =>value  [â€cf_emailâ€] =>value  [â€cf_messageâ€] => value )
Saty
  • 22,443
  • 7
  • 33
  • 51