HTML Form and Sendmail.php sending blank emails

Hi all,

I am very new to html and ran into problems with my html and php code for form sending. It is just a simple form, but I have been stuck here for hours and finally decided to ask some experts for advice so I can learn more.

Below is the php code (sendemail.php):



<?php
	header('Content-type: application/json');
	$status = array(
		'type'=>'success',
		'message'=>'Email sent!'
);


$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$contact = @trim(stripslashes($_POST['contact']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));

$email_from = $email;
$email_to = 'myemail@gmail.com';

$body = 'Name: ' . $name . "\
\
" . 'Email: ' . $email . "\
\
" . 'Contact: ' . $contact . "\
\
" . 'Subject: ' . $subject . "\
\
" . 'Message: ' . $message;

$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');


echo json_encode($status);
die;



and below is the form code:



<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">

<label>Name</label>
<input type="text" class="name" required="required" placeholder="Your name">

<label>Email Address</label>
<input type="text" class="email" required="required" placeholder="Your email address">

<label>Contact</label>
<input type="text" class="contact" required="required" placeholder="Your contact number">

<label>Subject</label>
<input type="text" class="subject" required="required" placeholder="The subject of your inquiry">

</div>

<div class="span7">

<label>Message</label>
<textarea name="message" id="message" required="required" class="message" rows="8" placeholder="Your inquiry"></textarea>

</div>

</div>

<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>

<p></p>

</form>


Thanks in advance and sorry for the inconvenience caused!

Remove the @ sign before your mail() function call so that you aren’t suppressing errors (they will then be output to the browser).

Also try moving your header() call to just before your echo() at the bottom.

Hi, thanks for the help, unfortunately it still did not work. I have removed the @ and shifted the header() to before echo(). This is what i got on my email (with no subject title as well):

Name:

Email:

Contact:

Subject:

Message:

Any more advises?

All those nasty error suppressors (the leading @'s) aren’t helping you debug any.

$_POST variables come from a form’s (whose method = POST) inputs based on their name values.
I don’t see any “name=” in your form code except for message.

And it would be a good idea to be more strict with the user supplied input.

Hi, I was thinking of getting it working first before inserting restrictions so I can learn step by step. Thanks a bunch!

I have removed all @ at the php and also replaced “class=” with “name=” at the form, unfortunately it is still sending in blank emails. Below is the updated codes:

sendemail.php:


<?php
header('Content-type: application/json');
	$status = array(
		'type'=>'success',
		'message'=>'Email sent!'
);


$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$email_from = $email;
$email_to = 'myemail@email.com';

$body = 'Name: ' . $name . "\
\
" . 'Email: ' . $email . "\
\
" . 'Contact: ' . $contact . "\
\
" . 'Subject: ' . $subject . "\
\
" . 'Message: ' . $message;

$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');


echo json_encode($status);
die;

form code:


<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">

<label>Name</label>
<input type="text" name="name" required="required" placeholder="Your name">

<label>Email Address</label>
<input type="text" name="email" required="required" placeholder="Your email address">

<label>Contact</label>
<input type="text" name="contact" required="required" placeholder="Your contact number">

<label>Subject</label>
<input type="text" name="subject" required="required" placeholder="The subject of your inquiry">

</div>

<div class="span7">

<label>Message</label>
<textarea name="message" id="message" required="required"  rows="8" placeholder="Your inquiry"></textarea>

</div>

</div>

<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>

<p></p>

</form>


Thanks in advance and sorry for any inconvenience caused!

Add:


print_r($_POST); exit;

At the top of sendemail.php to see what values/variables are being passed via POST. Then we can investigate further.