0

I'm having trouble to set this email form working properly

I have this contact.html:

<form mehtod="post" action="contact.php">
<hr />

<div class="row">
    <div class="large-12 columns">
    <label>Name
        <input type="text" name="cf_name" placeholder="Name" />
    </label>
</div>
</div>

<div class="row">
    <div class="large-12 columns">
        <label>Email
    <input type="text" name="cf_email" placeholder="Email" />
    </label>
</div>
</div>

<div class="row">
<div class="large-12 columns">
    <label>Your message
    <textarea name="cf_message" placeholder="Your message"></textarea>
    </label>
</div>
</div>

<input class="button" type="reset" value="Clear">
<input class="button success expand" type="submit" value="Submit">

</form>

And this contact.php:

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

$mail_to = 'my@email.com';
$subject = 'Personal Website Message from '.$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. I will contact you shortly.');
    window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
    alert('Message failed. There is a problem with your message, if keep aving trouble         click on the Email me button above.');
    window.location = 'contact.html';
</script>
<?php
}
?>

But when I upload this, and test it, nothing is sent to my email, why ? For me everything seems to be right ...

jbernardo
  • 155
  • 1
  • 3
  • 9
  • Who hosts your website? A lot of free web hosts don't allow php mail but throw no error messages. – Dummy Code Mar 07 '14 at 16:00
  • use swift mailer or php mailer for better error handling .. If none works then send mail using SMTP – srs Aug 26 '14 at 09:42

2 Answers2

0

Your mail client may not be allowing sending emails without authorization. Unfortunately PHP's mail function does not support username + password authorization, so you have to look for third party extensions.

bobek
  • 8,003
  • 8
  • 39
  • 75
  • This is strange, I've downloaded this php code from the internet and with the .html code that came with it it worked fine, but after I associated it with my html code it stopped working, and yes after I click on the submit button the javascript code runs so it should mean that the email was sent ... – jbernardo Mar 07 '14 at 16:01
  • Look at the "from" property. Why would you expect this to work? You can't send email from any email address that you want. You have to change the "from" to an email client that will actually let you do it. As an example, you can use Google's email address as a host: http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – bobek Mar 07 '14 at 16:04
  • I am using mail() function from php, where is it supposed to have a "from" property ? – jbernardo Mar 07 '14 at 16:09
  • It's in PHP's configuration file: php.ini – bobek Mar 07 '14 at 16:11
  • I'm new to php, should I have a php.ini file with specifications ? – jbernardo Mar 07 '14 at 16:13
  • It's in your server, if you have PHP installed - I am new too so no worries :) http://stackoverflow.com/questions/8684609/dude-wheres-my-php-ini – bobek Mar 07 '14 at 16:13
  • But I have my site hosted at hostgator, am i supposed to infall php there ? (if that is even possible) – jbernardo Mar 07 '14 at 19:27
0

Please check the spelling of your 'method' attribute in the form element.

The PHP code you supplied specifies $_POST as the variable that contains data relevant to the form.

Your misspelling causes the server use the default 'get' method to post the data (for reference, see here)

Correcting the opening tag to the following should fix the issue:

<form method="post" action="contact.php">
Jamus
  • 865
  • 4
  • 11
  • 28