Why isnt this contactform working?

I aint recieving any email when im trying it out. This is my first time trying to make a functional contactform, but nothing seems to be working, nor the jquery validation or the php. The php outputs “your message has been sent” but there are no emails in my inbox.

the form:

<form method="post" id="myform" action="contact.php">
	<input type="text" name="name" value="Ditt namn" />
	<input type="text" name="email" value="Din epostadress" />
	<textarea name="message"></textarea>
	<button type="submit" name="send">Skicka meddelande</button>
</form>

Validation:

	<script type="text/javascript">
	$(document).ready(function () {

	    $('#myform').validate({ // initialize the plugin
	        rules: {
	            name: {
	                required: true,
	                minlength: 5
	            },
	            email: {
	                required: true,
	                email: true
	            },
	           	message: {
	                required: true,
	                minlength: 20
	            }
	        }
	    });

	});
	</script>

the php script:

<?php

// Details
$message = "$message";

// Mail of sender
$mail_from = "$email";

// From
$header = "from: $name <$mail_from>";

// Enter your email address
$to ='jesper.landberg@outlook.com';
$send_contact=mail($to,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "Your message has been sent";
}
else {
echo "ERROR";
}
?>

Hi ReGGaeBOSS,

There are several reasons why you might not be getting the email. Are you having this problem when running the code on a web server, or on your local machine?

If you’re running on your local machine then it’s possible that PHP isn’t configured correctly to send mail. If you’re running on hosted web space, it could be that the email is getting sent but is being flagged as spam by the receiving email service.

Ive tried it booth offline and on web server. None works. If it is beeing flagged as spam do you have any idea how to fix this? Or maby u know some better script?

cheers

Could you share the link to the page on your web server? I think it would be easier to debug your online version (as that’s where you actually need it to work).

Thanks for the link.

First off, I notice you’re getting a couple of JS errors on the page… jquery-ui is complaining because you’re trying to load it before you’ve loaded jquery (just reverse the order of the <script> tags on the page). Also, you’re not loading a validation plugin anywhere on the page, which is why the form validation is not working.

do you tried a different email adderess
use sender’s email id to be same as from your server eg:noreplay@yourserver.com
and just post the email of sender in message
try it for justhost i had similar problem

Thanks totally forgot about the plugins.

Btw i just noticed that i do get the emails as empty junkmails.

So now it seems its just the php script that is causing trouble?

You’ve also got some problems with your PHP script:


// Details
$message = "$message";

// Mail of sender
$mail_from = "$email";

You’re just assigning strings to those variables, rather than getting the form data that was submitted.

Try something like this:


<?php

$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
$mail_from = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_SPECIAL_CHARS);

// From
$to = 'jesper.landberg@outlook.com';
$subject = 'Contact from website';
$header = "From: $name <$mail_from>";

$send_contact = mail($to, $subject, $message, $header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
    echo "Your message has been sent";
}
else {
    echo "ERROR";
}

I just noticed that i do get the emails as junkmail … empty junkmail.

cheers this seems to be working, still getting the emails in the junkmail tho… do you know how to fix that?

You can try adding the actual email address that your webserver sends the email from (not the one you set in your PHP script) to your contacts in Outlook.com

You can find the real From address by highlighting the received email and going to the Actions menu, and selecting View Message Source. You’ll see all the email headers, and somewhere among them will be the address that the email actually came from… it may be from the same domain as your website.