Jump to content

Contact form doesn't send details


Unrecognized

Recommended Posts

Hello everyone,

 

I'm having some trouble with fixing my contact form, it doesn't send any details to my email address.

The form I made is coded in HTML and I use PHP to send the details away.

 

I contacted the hosting company of my website and they said the following:

"Mail with another sender than the domain from which it got sended is unfortunately not possible anymore, the mail which will be send from dioncreations.com has to be a @dioncreations.com address. We did this because spammers abused the contact forms multiple times which gave our mail servers some difficulties."

 

So I'm trying to fix my PHP send script so it will send the details to my email address.

 

PHP script:

<?php
	header('Content-type: application/json');
	$status = array(
		'type' => 'success',
		'message' => 'Email sent!'
	);
	
	$name = @trim(stripslashes($_POST['name']));
	$email = @trim(stripslashes($_POST['email']));
	$subject = @trim(stripslashes($_POST['subject']));
	$message = @trim(stripslashes($_POST['message']));
	
	$email_from = 'info@dioncreations.com';
	$email_to = 'info@dioncreations.com';
	
	$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
	
	$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
	
	echo json_encode($status);
	die;
?>

HTML script

<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="mailer.php" role="form">
	<div class="row">
		<div class="col-sm-6">
			<div class="form-group">
				<input type="text" class="form-control" id="name" name="name" placeholder="Name" required="required">
			</div>
		</div>
		<div class="col-sm-6">
			<div class="form-group">
				<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required="required">
			</div>
		</div>
	</div>
	<div class="row">
		<div class="col-sm-12">
			<div class="form-group">
				<textarea class="form-control" rows="8" id="message" name="message" placeholder="Message" required="required"></textarea>
			</div>
			<div class="form-group">
				<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
			</div>
		</div>
	</div>
</form>

Someone told me there are variables missing, but what kind of variables he wouldn't say.

 

Can someone help me out, so I can continue with my website?

 

I thank you kindly! Your help is much appreciated.

Edited by Unrecognized
Link to comment
Share on other sites

Are you receiving the e-mails or not?

 

The code seems fine to me. You should update $status based on the value of $success so that you can tell if the mail was properly sent or not.

Link to comment
Share on other sites

Are you receiving the e-mails or not?

 

The code seems fine to me. You should update $status based on the value of $success so that you can tell if the mail was properly sent or not.

 

I do receive the emails now but they are empty and I've no clue how to fix it. The only thing I'll get is:

 

Name:

Email:

Subject:

Message:

 

And the subject is also empty.

Link to comment
Share on other sites

You should remove the @ from these lines of code:

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

If any errors are occurring there the @ operator will stop you from seeing them.

 

I suspect your POST values are empty. Try printing out the values to see what you're getting. If you use var_dump() on the values you'll see more information about the content of the variables.

Link to comment
Share on other sites

You should remove the @ from these lines of code:

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

If any errors are occurring there the @ operator will stop you from seeing them.

 

I suspect your POST values are empty. Try printing out the values to see what you're getting. If you use var_dump() on the values you'll see more information about the content of the variables.

 

I hope I understood you right and I tried the following:

	$name = trim(stripslashes($_POST['name']));
	$email = trim(stripslashes($_POST['email']));
	$subject = trim(stripslashes($_POST['subject']));
	$message = trim(stripslashes($_POST['message']));
	echo var_dump($name, $email, $subject, $message);
Link to comment
Share on other sites

The echo statement isn't necessary, but it will still work the same. When you submit the form information should be displayed on the page, look at it carefully to see if it matches what you expected to see.

 

You could just var_dump($_POST) as well.

Link to comment
Share on other sites

The echo statement isn't necessary, but it will still work the same. When you submit the form information should be displayed on the page, look at it carefully to see if it matches what you expected to see.

 

You could just var_dump($_POST) as well.

 

Once again, I'm not sure if I'm doing it right. I tried the following:

<?php
	header('Content-type: application/json');
	$status = array(
		'type' => 'success',
		'message' => 'Email sent!'
	);
	
	$name = trim(stripslashes($_POST['name']));
	$email = trim(stripslashes($_POST['email']));
	$subject = trim(stripslashes($_POST['subject']));
	$message = trim(stripslashes($_POST['message']));
	echo var_dump($_POST);
	
	$email_from = 'info@dioncreations.com';
	$email_to = 'info@dioncreations.com';
	
	$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
	
	$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
	
	echo json_encode($status);
	die;
?>

I tried it without the echo and the form didn't work anymore so I added the echo again.

When I went to the index.html and tried sending the form it didn't gave me a "Email sent!" message anymore but I had an email still without any content only the name, subject, email and message.

So I tried going to my mailer.php file instead of going to my index.html and had the same email in my inbox but the PHP file was giving me this message:

array(0) {}{"type":"success","message":"Email sent!"}
Link to comment
Share on other sites

The information would be displayed in mailer.php of course, which is the file that the browser goes to upon submitting the form.

 

The $_POST array is empty, that's the problem.

 

Is there any Javascript involved in this system? Why are you returning the data in JSON format?

 

If all you had was that HTML and that PHP code then what you have now would work without a problem.

Link to comment
Share on other sites

I'm using a template which had the JSON thing already in this file, so I didn't remove it because I didn't understand it fully.

 

There is actually a couple JavaScript files added to my website, but only 1 were I could find something about the contact form.

This is the part I found in the JavaScript which is about the contact form:

//Ajax contact
	var form = $('.contact-form');
	form.submit(function () {
		$this = $(this);
		$.post($(this).attr('action'), function(data) {
			$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
		},'json');
		return false;
	});
Link to comment
Share on other sites

It's sending a POST request but not sending any of the form's fields.

 

Do you know Javascript?

 

I tried learning JavaScript, but I didn't had the same amount of interest in JavaScript as in HTML and CSS. That's the sad part, maybe this is a good thing so I'll go and learn JavaScript.

Link to comment
Share on other sites

$email = @trim(stripslashes($_POST['email'])); will always be empty, there is no input with name="email", it will throw error email index does not exist, probably causing it to fail from that point onwards. just add

 

$_POST['email'] ="bugs@bunny.com";

 

above

 

$name = trim(stripslashes($_POST['name']));

 

if it succeed to send all fields after that! then that is the problem. you require input for email.

Link to comment
Share on other sites

I somehow fixed it by removed this little JavaScript code:

//Ajax contact
	var form = $('.contact-form');
	form.submit(function () {
		$this = $(this);
		$.post($(this).attr('action'), function(data) {
			$this.prev().text(data.message).fadeIn().delay(3000).fadeOut();
		},'json');
		return false;
	});

I added a email input into my form and uploaded it to my webhost. Tried the form out and it worked perfect, it actually sends the details to my email address right now.

 

Thank you all for the patience, kindness and help!

Link to comment
Share on other sites

Yes, that Javascript is intercepting the form submission. If it was programmed properly then it would allow the e-mail to be sent without reloading the page. Removing it will make the form work but it will load mailer.php into the browser.

Link to comment
Share on other sites

Yes, that Javascript is intercepting the form submission. If it was programmed properly then it would allow the e-mail to be sent without reloading the page. Removing it will make the form work but it will load mailer.php into the browser.

 

Indeed, I'm looking for a way to stop that. Not sure how I'm going to do that yet as I'm not very good in JavaScript.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...