Send contact form invalid email

Following are the files i have to get my contactus form working. However, i am not sure what went wrong with my codes such that it keep giving invalid form and message was not send, thus, try again. Just to inform that I have yet to set my mail functionality set up. I only getting my form works and if it is valid, it will bring user to contactthanks.php. Is it due invalid email address? Hope someone can help me notify this error.

contactus.php

	<form name="contactForm" action="contactthanks.php" method="post"
				id="contactForm" onsubmit="return chkContact(this)">
				<div class='input1'>
					<div class="help">
						<input id="uname" name="name" type="text" placeholder="Name "
							value="<?php echo $user_profile["uname"]; ?>">
					</div>

					<div id="email-label" class="help">
						<input id="uemail" name="email" type="text"
							placeholder="Email Address " class="input-xlarge"
							value="<?php echo $user_profile["uemail"]; ?>">
					</div>

					<div id="subj-label" class="help">
						<input id="utitle" name="title" type="text"
							placeholder="Subject " class="input-xlarge"
							value="<?php echo $user_profile["utitle"]; ?>">
					</div>
					<div id="message" class="help">
						<textarea id="message" name="message" rows="13" cols="33"
							placeholder="Message " class="input-xlarge"></textarea>
					</div>

					<button id="btnSubmit" type="submit" value="Send"
						onclick="return chkContact()">Send</button>
					<div id="msgSubmit" class="h3 text-center hidden"></div>
				</div>
			</form>
                      <script>
function chkContact(form) {
	if(form.uname.value == "" && form.uemail.value == "" && form.utitle.value == "" && form.message.value == "" ) {
		  alert("Please fill in the required fields: \n\u26AC Name \n\u26AC Email \n\u26AC Subject \n\u26AC Message");
		     form.uname.focus();
		     return false;
	}
	 if(form.uname.value == "") {
	     alert("Error: Name cannot be blank!");
	     form.uname.focus();
	     return false;
	   }
	   
	 if(form.uemail.value == "") {
	     alert("Error: Email Address cannot be blank!");
	     form.uemail.focus();
	     return false;
	   }
	   re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	   if(!re.test(form.uemail.value)) {
	     alert("Error: Invalid email address!");
	     form.uemail.focus();
	     return false;
	   }
	   if(form.utitle.value == "") {
		     alert("Error: Subject cannot be blank!");
		     form.utitle.focus();
		     return false;
	   }
	   if(form.message.value == "") {
		     alert("Error: Message cannot be blank!");
		     form.message.focus();
		     return false;
	   }
	var uname = document.getElementById('uname').value;
	var uemail = document.getElementById('uemail').value;
	var utitle = document.getElementById('utitle').value;
	var message = document.getElementById('message').value;
	var dataString='uname='+ uname+'&uemail='+uemail+'&utitle='+utitle+'&message='+message;

	$.ajax({
		type:"post",
		url: "<?php echo $base_url; ?>ajax-helper/post-email.php",
		data:dataString,
		cache:false,
		success: function(data){
		if (/^\s*SUCCESS\s*$/.test(data)) {
		window.location = '<?php echo $base_url; ?>index?success=true';
		   }
		else{
		     alert(data);
		  }
		}
	});
		return false;
}
</script>

post-email.php

<?php
$uname = Trim ( stripslashes ( $_POST ['name'] ) );
$uemail = Trim ( stripslashes ( $_POST ['email'] ) );
$utitle = Trim ( stripslashes ( $_POST ['title'] ) );
$message = Trim ( stripslashes ( $_POST ['message'] ) );

$EmailTo = "nurainiyakob95@live.com";
$Subject = "New Message Received";

// prepare email body text
$Body = "<br>";
$Body .= "Name: ";
$Body .= $uname;
$Body .= "\n<br>";
$Body .= "Email: ";
$Body .= $uemail;
$Body .= "\n<br>";
$Body .= "Subject: ";
$Body .= $utitle;
$Body .= "\n<br>";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n<br>";

// send email
// $url = 'http://52.76.105.171/apis/mailerServiceJson/tos_emailer.php';
// $fields = array (
// 'BASE_URL' => $base_url,
// 'BRAND_NAME' => $PROJECT_NAME,
// 'RECEIVER_EMAIL' => $EmailTo,
// 'SENDER_EMAIL' => $email,
// 'RECEIVER_NAME' => '...',
// 'SENDER_NAME' => $name,
// 'SUBJECT' => $Subject,
// 'CONTENT' => $Body
// );

$success = mail ( $EmailTo, $Subject, $Body, "From: <$uemail>" );

// $fields_string = json_encode ( $fields );
// $ch = curl_init ();
// curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );

// curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
// curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );

// curl_setopt ( $ch, CURLOPT_URL, $url );
// curl_setopt ( $ch, CURLOPT_POST, count ( $fields ) );
// curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields_string );
// curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
// curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
// 'Content-Type: application/json',
// 'Content-Length: ' . strlen ( $fields_string )
// ) );
// $result = curl_exec ( $ch );
// curl_close ( $ch );

if ($success) {
	echo "Message sent!";
} else {
	echo "Message not sent! Try again.";
	// header('location: contactus.php');
}
?>

contactthanks.php

   	<div class="six columns">
				<f7>Your message has been sent!</f7>
<!-- 				<br> -->
				<br> <a href="contactus.php" style="float: left;">&#9668; Back to
					Contact Form |</a> <a href="home.php" style="float: right;"> Back
					to Homepage &#9658;</a>
			</div>

You’re still trying to send the email from whatever email address the form-filler entered, as per the discussion on the other topic. As an experiment, try setting the “from” address to something you know is valid. Is the mail server configured properly in your development environment, or are you testing this on a live server and if so, is it configured there?

i set the email address to my own email address which is valid, valid in a way that the email address is working, however, the mail server is not configured yet in the development environment.

it is still under process of getting it configured on live server. So is it due to that the contact form always send invalid?

Not likely. The process is something like
PHP mail() → email server → email client (inbox)

If the PHP was saying “sent OK” but you weren’t getting any emails then the problem would more likely be either with the email server or ,maybe the email client rejecting it.

The PHP mail() only returns true if it was sent to the email server, it has no way to know anything beyond that.

So if mail() is failing, then the problem lies somewhere in the PHP code.

email headers use newlines as delimiters. Try

$success = mail ( $EmailTo, $Subject, $Body, "From: <$uemail>\r\n" );

Wouldn’t that be enough to also make the mail() function fail @Mittineague? Assuming the error comes in the development environment that is. I guess it depends on whether the comment means that there is a mail server set up but it’s not configured to know how to send messages any further than itself, or whether there is no pointer to a mail server in development.

1 Like

Hmmm, maybe so, It was my understanding that mail() only returned true if it was sent, but checking the documentation it says it returns true if it was accepted.
http://php.net/manual/en/function.mail.php

Return Values

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

I tried with this too but to no avail. The contact form still shows message not send. :pensive:

If that’s the problem, I assume this error is due to th development not configured yet. This is because I don’t see any wrong in my PHP code. :cry:

My form is now able to proceed to contactthanks.php. However, i’m unsure of what $EmailTo and $Subject refer to as i tried to command it out from post-email.php my form still able to proceed to contactthanks.php. But i think this is due to action="contactthanks.php" which i add to my <form>

Unless I’m missing something in your question, these are the email address you’re sending the email to and the email subject.

alright noted! It would be great if you can read up my post as i’m still stuck at making my contactus form works like normal form as i haven’t set the mail functionality configured yet.

But the mail() function won’t work if you haven’t configured mail functionality.

If you want to send mail without configuring mail functionality you could use phpmailer instead of the PHP mail() function.

ouhhh now it makes sense! Phpmailer? How should i call phpmailer in my post-email.php?

Well, it’s a choice. Either you configure your server to send emails or you use phpmailer (or something similar). If you want to use phpmailer, use the link I provided to download it. The package includes instructions for using it. The PHP code is more involved using phpmailer so you’ll have to decide which is easier/best for you - set up your server to send emails or install phpmailer.

The main difference is that phpmailer uses a mail server to send emails rather than the web server.

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