Contact form not working

the mail() function

Moderators: egami, macek, gesf

Post Reply
geenidee
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 09, 2017 9:19 am

Sat Dec 09, 2017 9:37 am

Hi all,

I used to have a working contact form but all of a sudden it's not working anymore. Been looking everywhere for an answer but don't see what's going wrong. I keep getting an error message when submitting a message: "Error: Invalid E-Mail Address". When I remove all error and security checks it works but there's no subject or message in the mail. Anyone know what might be causing the errors? PHP runs on 7.1.

Thanks a lot for your help!

Code: Select all

<?php
	// VALUES FROM THE FORM
	$name		= $_POST['name'];
	$email		= $_POST['email'];
	$message	= $_POST['msg'];

	// ERROR & SECURITY CHECKS
	if ( ( !$email ) ||
		 ( strlen($_POST['email']) > 200 ) ||
	     ( !preg_match("#^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$#", $email) )
       ) 
	{ 
		print "Error: Invalid E-Mail Address"; 
		exit; 
	} 
	if ( ( !$name ) ||
		 ( strlen($name) > 100 ) ||
		 ( preg_match("/[:=@\<\>]/", $name) ) 
	   )
	{ 
		print "Error: Invalid Name"; 
		exit; 
	} 
	if ( preg_match("#cc:#i", $message, $matches) )
	{ 
		print "Error: Found Invalid Header Field"; 
		exit; 
	} 
	if ( !$message )
	{
		print "Error: No Message"; 
		exit; 
	} 
	if (eregi("\r",$email) || eregi("\n",$email)){ 
		print "Error: Invalid E-Mail Address"; 
		exit; 
	} 
	if (FALSE) { 
		print "Error: You cannot send to an email address on the same domain."; 
		exit; 
	} 


	// CREATE THE EMAIL
	$headers	= "Content-Type: text/plain; charset=iso-8859-1\n";
	$headers	.= "From: $name <$email>\n";
	$recipient	= "MYEMAIL";
	$subject		= "MYMAILHEADER";
	$message	= wordwrap($message, 1024);

	// SEND THE EMAIL TO YOU
	mail($recipient, $subject, $message, $headers);

	// REDIRECT TO THE THANKS PAGE
	header("location: MYWEBSITETHANKS");
?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 544
Joined: Mon Feb 22, 2016 5:52 pm

Sat Dec 09, 2017 10:20 am

eregi was removed in PHP 7 having been deprecated since version 5.3.

geenidee
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 09, 2017 9:19 am

Sat Dec 09, 2017 10:30 am

Thanks for the info! I just removed the eregi part in total and that still didn't work. Any suggestions on how to fix? (I'm a php newby..)

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 544
Joined: Mon Feb 22, 2016 5:52 pm

Sat Dec 09, 2017 10:39 am

Put these two lines at the start of your script:

Code: Select all

error_reporting(E_ALL);
display_errors = on;
then read what is displayed.

geenidee
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 09, 2017 9:19 am

Sun Dec 10, 2017 10:54 am

Thanks for the help! The result is a completely blanc page..

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 544
Joined: Mon Feb 22, 2016 5:52 pm

Sun Dec 10, 2017 11:36 am

Was the blank page with the error checking?

I've been trying to avoid saying the following, but it might be your best bet rather than trying to fix what you have:
It may be worth you looking at using PHPMailer (there are some youtube videos which may help) or SwiftMailer. And it wouldn't hurt to re-write your form handle your code is not very secure.

geenidee
New php-forum User
New php-forum User
Posts: 4
Joined: Sat Dec 09, 2017 9:19 am

Sun Dec 10, 2017 11:52 am

The page was blank with error checking yes.

Thanks for the suggestions! I'll have a look at them!

Post Reply