PHP not sending email

I have PHP installed using Xampp and ive been working on an email form using php and constructed error messages and all the error messages work and claims mail has been sent but i dont see anything in spam filter or my email.

HTML Form:

<form method="POST" action="***">
<h2> Email Us </h2>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
<label for="surname">Surname</label>
<input type="text" id="surname" name="surname" /></br>
<label for="email">Email</label>
<input type="email" id="email" name="email" /></br>
<label for="message">Message</label>
<textarea id="message" name="message"></textarea></br>
<h4>Please, complete Captcha to submit</h4>
<div id="validate">
<!-- Insert Captcha here!! -->
</div>
<input type="submit" id="formS" name="submit" value="send"/>
</form>

Do not worry too much for the links i remove them for security purpose :slight_smile: The links work and id also like to know is it safe to display error messages in the php form? not in the html document.

PHP Email Form:

<?php 

/* Define Veriables */
	$name = test_input($_POST["name"]);
	$srn = test_input($_POST["surname"]);
	$email = test_input($_POST["email"]);
	$msg = test_input($_POST["message"]);
	
/* Filter Remove extra spacing */
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


/* When Submit is called */
if($_SERVER["REQUEST_METHOD"] == "POST"){
/* filter Name Empty  and characters */
	if(empty($name)){
		$name = 0;
		$errname = "* Name Field has not been filled in";
	}else{
		if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
			$name = 0;
			$errname = "* Name Field contains elements other than plain text";
		}else{
			$name = 1;
			$errname = "";
		}	
	}

/* filter Surname Empty  and characters */
	if(empty($srn)){
		$srn = 0;
		$errsrn = "";
	}else{
		if (!preg_match("/^[a-zA-Z ]*$/",$srn)) {
			$srn = 0;
			$errsrn = "</br>* Surname Field contains elements other than plain text";
		}else{
			$srn = 1;
			$errsrn = "";
		}
	}
	
/* filter email Empty  and characters */
	if(empty($email)){
		$email = 0;
		$erremail = "</br>* Your Email Field has not been filled in";
		
	}else{
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
			$email = 0;
			$erremail = "</br>* Email Field Does not contain @ symbol and or suffixis";
		}else{
			$email = 1;
			$erremail = "";
		}
	}
/* filter message Empty  and characters */
	if(empty($msg)){
		$msg = 0;
		$errmsg = "</br>* Error: message not displayed what are you trying to tell us?";
		
	}else{
		$msg = 1;
		$errmsg = "";
	}
	
	if($name + $srn + $email + $msg > 3){
		
	$name = test_input($_POST["name"]);
	$srn = test_input($_POST["surname"]);
	$email = test_input($_POST["email"]);
	$msg = test_input($_POST["message"]);
	$succmsg = "Your message has been successfully delivered!</br></br> Feel Free to check out our social media </br></br>or</br></br> select an option at the navigation to take you to our website.";
	$to = "nathangriesel@gmail.com";
	$subject = "From: ".$email ."(" .$name ." " .$srn .")";
	$header = $headers = "From: $email\
";
	
	
	mail($to,$subject,$msg,$headers);
		
		echo "Dear";
		echo " ";
		echo $name;
		echo " ";
		echo $srn;
		echo "</br>";
		echo "</br>";
		echo "</br>";
		echo $succmsg;
		echo "</br>";
		echo "</br>";

	}else{
		
		$name = "User";
		$srn = "";
		$email = "From: Mr Laptop.co.za";
		$msg = "It Appears Your Email was unsuccessful, This could be due to incomplete Field(s).";
	
		echo "Dear";
		echo " ";
		echo $name;
		echo " ";
		echo $srn;
		echo "</br>";
		echo "</br>";
		echo $msg;
		echo "</br>";
		echo "</br>";
		echo '<b style="color:red;font-size:14px;">';
		echo $errname;
		echo $errsrn;
		echo $erremail;
		echo $errmsg;
		echo "</b>";
		echo "</br>";
		echo "</br>";
		echo '<a href="contact.html" style="color:#fff;font-size:15px;">Back to form</a>';
	
	}
}
?>

So the above codes work and validation goes as plan i tried it on Safari, Chrome, Opera,IE8 and firefox all latest version except IE its version 8.

and the </br> tags echoed in php get ignored in IE8 and safari any reasons for that wld also be great please.

If you want the PHP mail() function to work using XAMPP you will need to configure the Mercury mail server. Better still, ditch mail() and use PHPMailer or SwiftMailer instead.

Do you send the opening <html> and other tags somewhere else in the PHP code that you haven’t shown?

In any case, I thought the proper syntax was either <br> or <br /> for a break? I’m not up on modern HTML though, maybe I just proved that.

I can’t see why it wouldn’t be “safe”, but it’s not all that nice as an end product. Either check to see that there are errors and, if there are, re-draw the contact form with the fields filled in as the user provided them (perhaps not the email field) and draw the error messages in the form; or use Ajax to validate the fields as the user types them, and don’t allow the form to be submitted until it’s filled out correctly. Start with the way you’re doing it, then enhance it to just redraw the form with errors, then add the Ajax when you’re more confident.

Users hate spending ages filling out forms only for them to be rejected with “there was an error” and made to fill the form out again. Even when you add more detail about what the problem was, with your current method they can’t see what they typed any more, so can’t figure out exactly what they did wrong. If it’s an enquiry form, will they bother filling it in again?

the code is echoed onto the .php file but before the page is echoed it is styled so that it looks like my main website so header and footer and main page is all there it doesnt show error:line bla bla it shows the veriables echoed after a message is displayer in other words it will be displayed like so

Dear User ($name) " " (Surname)

It Appears Your Email was unsuccessful, This could be due to incomplete Field(s).

  • Name Field has not been filled in
  • Your Email Field has not been filled in
  • Error: message not displayed what are you trying to tell us?

Back to form

This way it will display fields that were not filled.

but is there a way when they click to go back it will re-input the stuff they have already typed out?

The $surname or $srn input isnt important thats why it wont be displaying an error but if it is filled in with info other than text like and html code or something than it will display a code so the design looks like an ordianry website page all in all it still looks good only problem here is i need to verify email address exists and find a way to re input the content that was already filled by the user.

oh and plain <br> also gets ignored by safari and IE8 i will try <br /> see if it works

how do i edit the mercury settings i already edited php.ini and sendmail.ini and inserted that other code they said to use on stackexchange i dont know if i need to allow it in my gmail settings or something?

and i dont like relying on external links like libraries or other apps im only using an notepad++ editor and pure knowledge only html css and php and javascript and mysql i dont know the other languages well enough yet alone php and mysql and javascript basicly all backend coding i can stylesheets to run the way i want but am not familiar with coding that i cant see like global veriables used by browsers and etc

I’m afraid I gave up trying, especially when I realise that mail() is not very reliable and doesn’t work on all hosts. Apart from the reliability of PHPMailer and Swift, they work on localhost without the hassle of installing a mail server, and they work on all hosting accounts.

As @droopsnoot pointed out, your syntax is wrong. It should be <br> in HTML5 and <br /> for earlier versions. But I have to ask, why are you concerned about IE8? It is a dead browser, it has ceased to be…

The way I would do it is to stick the form variables into session variables, make the contact form a PHP script instead of plain HTML, and have it look to see whether the session variables are present and, if they are, insert the values into the form when it is drawn.

You can also stick the error messages in there, and draw them in the form to highlight the problems, I’d probably do that instead of displaying the error message. Just make sure it’s clear the form has not been submitted.

because unfortuantely the desktops sold here are using IE8 as opposed to the software package so if a desktop gets sold most of the customers will not hesitate to use a browser which have currently available to open the website and in the end viewing compatabality issues therefore the first impression wld be gone in reulting to maybe 10% or i dont know statistics but windows xp users also still use that old browser so if they tried to view my work it will look like a mess nvm the current website that is being used also lookes like a mess on IE8 so im just making sure to capture the target audiance regurdless if they upgrade their browsers since MOST people do not know how to do that,

you will be surprised not everyone out there is computer literate.

but that would require me to recreate my entire php file to fit into the html document that wld be a mess

isnt there a way to store data that is already in there and place them back like say i use that

Back to form

right than when i click it take the $_POST('name') veriables and place them back into the html input tags would that work?

anyway at the least all i want to do is figure out why the email wont get recieved as thats the issue the “bells and whistles” will have to wait i still need to apply the captcha into the php file too by adding another if() in so that it verifies the captcha is correct and make the operrand + another veriable and make it greate than 4 not 3 i have an idea of creating a random integer and displaying the integer in the captcha than checking if the input field matches the random integer i dont think it wld work though its just and idea

Maybe someone else will have a better solution, there are often different ways to do the same job.

I’m not seeing how it’s that much of a job, though of course I haven’t seen your contact form code. To me, all you need to do is rename it to a PHP file, add some PHP to check if the session variables exist and, if they do, insert them while rendering the html form. Oh, and obviously set the session variables before you go back to the form after you’ve validated things, and clear them out at an appropriate time, and maybe add an expiry. Maybe cookies might make it easier, if you can retrieve them from some JavaScript in your contact page, and if you can be sure your users have JavaScript enabled.

I can’t think of way you can pass the form variables back to the form without writing some code to do it. Sometimes (but not always) if the user hits the “back” button, the form might still be filled in. But it’s very difficult to control browser behaviour like that.

But you’re right, that’s away from the original problem of not being able to send emails. So you either need to configure Xampp to do it, or use something else as others have already said. As you said you’re using Gmail, you might also need to use a valid “from” address rather than trying to send the email using whatever address the user typed into the form.

the valid form address would mean what exactly? the from address shows who the address belongs to so that a reply can be done otherwise i wont know who sent it if i use the website address as from: than i wont know who filled in the form if its an address adressed to someone i can know who sent it by replying.

so what exactly is ‘valid’ email address?

i used my emailed dressed sending to myself to test it and still nothing so in the end from and to is the same isnt that technically valid im not seeing it in my spam messages and what should i edit in xampp?

By “valid”, I mean one that your Gmail account is happy to send. If it allows you to send an email with a “from-address” of anything without checking it, then it’s doing something called “open relay”, which is not a good thing.

The proper way to do it is to send it using your valid Gmail address, include the enquirers address in the body of the email message and if you really need to get back to them by just clicking on “reply” instead of writing a new email, set the “reply-to” address to the address they typed into the form.

so which From: do i edit the $header or the $subject?

I don’t know what to edit in Xamp, I’ve never used it. If you’re sending it using your own valid Gmail address, then it might not be the problem.

You can put anything you want in the message subject, it’s only when you try to use someone else’s address as the “from address” that it might cause a problem.

if i have to enable something into gmail than i might aswell use phpmailer but wld i have to change my code in anyway? or is there other computer programming languages i can use other than php?

about the captcha can i ask it true that create a hidden field for spam bots to fill in can work or is that just all bull?

like create an extra input field and hide it so technically only spam bots can read it?

therefore if it is filled in than display error message i dont think it wld do any harm to the users only if it doesnt work than most likely get loads of spam :frowning:

Invisible captcha can work and is better for UX. It is more effective to use a number of bot detection methods.

This kind of “honeypot” input is one method. Another is to use a timer which records the time a user lands on the form page and compares that against the time of form submission. If it happens too fast, it could be a bot.

The thing is to try it. If you still get spam, then try a more robust captcha.


I think this topic is starting to confuse different issues.
  • One being the mail not sending.
  • Your <br> elelments
  • The error messages
  • And your captcha

Though I’m not sure if it will split neatly into different topics at this stage.

Haha it may seem confusing but they all under the same problem my php file although different issues…

just always have issues with my content not familiar with back end script lol.

but i thought of an idea which i dont really now how to use is that my forms are already hidden and has to be opened with a button click like ‘email us now’ type of forms so i should use javascript to verify if the form input field was submited and check if the display for the form is display:block if not u cant fill in a form if its hidden so straight off i should have a javascript that takes away the submit field lol

and about the php mail() i honestly dont know trying it in localhost doesnt seem to work maybe the ssl protocal will be secure when implemented online… otherwise i’ll have to use mailto: input until i can find a better use for mailing like different computer languages other than php…

Javascript is no good for any kind of security as it can so easily be bypassed or disabled altogether, many bots don’t have it enabled. It’s fine for on-page validation to catch honest mistakes prior to submission, but needs to be backed up by back-end validation for security.