PHP not sending email

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.

A simple answer was already given to you. If you find this hard to do, then programming isn’t for you. Programming consists of a lot of problem solving, self teaching, and self awareness. If you do not have any of those, you won’t get very far.

People find that the syntax for any programming language is hard. I say it’s not. Knowledge doesn’t come to you, you have to learn it yourself. This is where self teaching comes from. If you can’t find answers yourself before asking a question, you’ll always end up back at the start.

Now. Use PHPMailer as the default mail() function is not reliable and has been an issue on some live hosts. Here is an important reminder why you should not use the default mail() function.

i am learning and to say the current php mail i used i learnt myself getting it wasnt as when i started the only thing i knew was the <?php and ?> tags nothing else i played with it before but this time ive been learning im not expecting answers for everything i just got carried away just wanted to actually know why my php wasnt working i dont know phpmailer thats why i hesitate over it i dislike using plugins or other software just me and notepadd++

what do you mean by default mail() is there another way of doing it?

PHPMailer isn’t a plugin. It’s a 3rd party library that is an alternative for sending emails. If you haven’t read what I have posted before, please go back and read it thoroughly. What I mean by “default mail() function” is that the mail() function comes with PHP by default. That is all. This function isn’t reliable because it fails at times. Just like you, I was once a noob too. I used to send emails using the default mail() function too. Shortly after using it, I started to see that my emails weren’t being sent. I kept searching online for answers, but I could not find the exact problem I was having. Long after, I found out myself that the mail() function fails at times even with a proper mail server. So I gave up on it and started using PHPMailer. Haven’t turned back since.

1 Like

alright thx for letting me know :slight_smile: uhhm i started downloading phpmailer yesterday amd i didnt get a readme got V6 of phpmailer and i dont know how to install it i extracted into Xampp/htdocs and im trying to figure out what to do coz i create a index.php to test the phpmailer and it doesnt do anything it shows all my coding on the page and i do have apche switched on any help or point me to a website that can help me instal phpmailer ive been looking and all i get is how to create my first phpmailer nothing about how to install it or if there is more than one package to instal nothing…

There are some articles here on PHPmailer.


1 Like

Thx for the URL i have now got it to work as it seems im still able to use the original php verification i have all i need to do is replace mail() to the phpmailer info

now i want make that phpmailer file like an external link so it can execute it from a seperate file therefore making it 'global without having to write it over and over and also keep it out of the ‘root’ folder.

so i just want to know is there a way to link to external files outside of root like in xampp link files outside of htdocs.

basicly only fill this part in the php.mail i used earlier and the rest before that into a seperate php file.

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

otherwords something like this:

<?php 

/* Define Veriables */
	$name = test_input($_POST["name"]);
	$srn = test_input($_POST["surname"]);
	$email = test_input($_POST["send"]);
	$hidden = $_POST["website"]);
	$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 = "";
		}
	}

/* check if hidden field is filled in */
	if(empty($hidden){
		$hidden = 1;
		$errhidden = "</br>Somehow you answered a hidden input field";
	else{
		$hidden = 0;
		$errhidden = "";
	}
/* 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 + $hidden > 4){
		
	$name = test_input($_POST["name"]);
	$srn = test_input($_POST["surname"]);
	$email = test_input($_POST["send"]);
	$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.";
	
	
/* link external php here */
$ext = /* Code or file path */
/* */

	$mail->Subject = "From:" ','.$Name ',' .$srn ',' "(" ','.$email ',' ")";
	$mail->Body    = $msg;
	$mail->AltBody = "From: ***";

if(!$mail->send()) {
		
		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 $errhidden;
		echo "</b>";
		echo "</br>";
		echo "</br>";
		echo '<a href="http://localhost/contact.html" style="color:#fff;font-size:15px;">Back to form</a>';
	
	}
}
?>

Ok so i fixed my issue in this PHP file but now just to be extra carefull how do i link outside of my root folder for instance its situated in htdocs of xampp how do i select a file outside of htdocs as it is more secure

use of PHP would be great ive looked on the internet for answers but all say i need to edit php.ini to use alias and i also heard its not completely safe there and i dont want to have to do the same on the server as they may not even be using xamp or wampp.

honestly I dont know what the server uses for php support but is there a way to select files outside of htdocs but still in the server eg in xampp and not in the files inside of htdocs

You can select files for inclusion that are below the root folder. Use ../ to go back a folder level.

include_once "../includes/myInclude.php" ;

i do that and it always keeps telling me directory doesnt exist

like instance i have my directories like this:
c:/xampp/htdocs/bla.php
and other file:
c:/xampp/Other/bla.php

the htdocs is the web root anything in there gets used at localhost therefor anything outside of htdocs doesnt work i want to access it from outside the htdocs for security reasons.

Using …/Other/bla.php calls:
c:/xampp/htdocs/Other/bla.php

and gives error file not found.

basicly after i added the php external file into the php file it worked but because it was inside of htdocs therefor can still be accessed if so i decided to create an ini file or php file outside the htdocs file to use as veriables to execute my files without revealing important info.
and thats what im trying i tried parsed_ini but it didnt work file directory is still the issue as it gives me file not found

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.