send-mail php function

Status
Not open for further replies.

earthc

New Member
Messages
6
Reaction score
0
Points
0
A form i have on my website uses validation and it comes up in console relaying error meaning the message cant send. I dont know why this is happening as it should be working..

I have the exact same script/code hosted on another server and it works fine. i dont understand why its not working for me.. i need this to be working by 2pm today! :\
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Please provide a link?
Please provide the error message?
Please provide the language it is written in?
 

earthc

New Member
Messages
6
Reaction score
0
Points
0
Please provide a link?Please provide the error message?Please provide the language it is written in?
http://earthcastle.x10.bz/contact.htmlit is returning the error which shows "oops, something went wrong - message not sent"written in php:and here is the form javascript code:// JavaScript Document$.fn.formValidation = function(){ //set variables for all the various form elements; var name = $(this).find("#name"); var email = $(this).find("#email"); var subject = $(this).find("#subject"); var message = $(this).find("#message"); var spam = $(this).find("#spam"); var formFields = $(this).find("input:text, textarea"); var status = $(this).find("#status"); var resetBtn = $(this).find("#reset"); /*submit handler for contact form*/ $(this).submit(function(e) { //prevents default form action e.preventDefault(); formFields.removeClass("error-focus"); //check required fields are not empty and that the email address is valid if(name.val()==""){ errorMessage("Please Enter Your Name"); name.focus().addClass("error-focus"); }else if(email.val()==""){ errorMessage("Please Enter Your Email Address"); email.focus().addClass("error-focus"); }else if(!isValidEmail(email.val())){ errorMessage("Please Enter a Correct Email Address"); email.focus().addClass("error-focus"); }else if(subject.val()==""){ errorMessage("Please Enter a Subject"); subject.focus().addClass("error-focus"); }else if(message.val()==""){ errorMessage("Please Enter Your Message"); message.focus().addClass("error-focus"); }else if(!spam.val()==""){ errorMessage("Spam Attack!!"); }else{ //if all fields are valid then send data to the server for processing successMessage("Email being sent... please wait"); //serialize all the form field values as a string var formData = $(this).serialize(); //send serialized data string to the send mail php via POST method $.ajax({ type: "POST", url: "send-mail.php", data: formData, success: function(sent){ if(sent=="error"){ errorMessage("Opps, something went wrong - message not sent"); } else if(sent=="success"){ successMessage("Thanks "+name.val()+", your message has been successfully sent"); //clear form fields formFields.val(""); }//end if else }//end success function });//end ajax function }//end else });//end submit //click handler for reset button - hides the status message resetBtn.click(function(){ status.slideUp("fast"); formFields.removeClass("error-focus"); }); //functions function errorMessage(message){ status.html(message).attr("class", "error").slideDown("fast"); } function successMessage(message){ status.html(message).attr("class", "success").slideDown("fast"); } function isValidEmail(email) { var emailRx = /^[\w\.-]+@[\w\.-]+\.\w+$/; return emailRx.test(email); } };
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Well, that gives us the front-end code (except for jQuery), but the front-end code doesn't send mail -- it just makes a POST request to a script that sends mail. It's that script (send-mail.php) that we need to know more about.

There are restrictions on outgoing mail in the system even when it's working properly: you aren't allowed to spoof the From header (it must be an address belonging to your hosting account), and text/plain is the only allowed format (no MIME+HTML). Most of the restrictions are in place to prevent spamming and phishing in an ongoing effort to keep the host off of the spamhaus and blocking lists (always a problem with free and low-cost hosting, especially when using subdomains -- one bad account can get the whole domain or IP block on the block lists).

It would help to know what the error is. You are relying on a message canned in your JavaScript, which is just saying "something went wrong" because the server didn't return a "200 OK" message to the AJAX request. It doesn't tell you what the error was; you'll need to get that from the server (either the error log or an error handler in the PHP script).
 

earthc

New Member
Messages
6
Reaction score
0
Points
0
Well, that gives us the front-end code (except for jQuery), but the front-end code doesn't send mail -- it just makes a POST request to a script that sends mail. It's that script (send-mail.php) that we need to know more about.

There are restrictions on outgoing mail in the system even when it's working properly: you aren't allowed to spoof the From header (it must be an address belonging to your hosting account), and text/plain is the only allowed format (no MIME+HTML). Most of the restrictions are in place to prevent spamming and phishing in an ongoing effort to keep the host off of the spamhaus and blocking lists (always a problem with free and low-cost hosting, especially when using subdomains -- one bad account can get the whole domain or IP block on the block lists).

It would help to know what the error is. You are relying on a message canned in your JavaScript, which is just saying "something went wrong" because the server didn't return a "200 OK" message to the AJAX request. It doesn't tell you what the error was; you'll need to get that from the server (either the error log or an error handler in the PHP script).

if you read the whole comment which unfortunately didn't want to include the spaces.. it includes the jQuery code after the php code..

I dont know what youre talking about when you say spoof from the header etc. Either way for some reason it had gone though when i used a hotmail address yesterday and today it worked perfectly fine. I think it was something to do with the server because the server also went down yesterday for a while.. it also seems alot of others are having problems with this issue.
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
What is the PHP code?

Also, please wrap your code in [code] and [/code].
There is no need to remove spaces if you do this :)
 

earthc

New Member
Messages
6
Reaction score
0
Points
0
What is the PHP code?

Also, please wrap your code in [code] and [/code].
There is no need to remove spaces if you do this :)

thanks for the tip ill keep that in mind for next time haha.

Code:
<?php 

//echo("success");	
//echo("error");
	
define('RECIPIENT','earthcastle@live.com');
	
//set email body and headers
$body = $_POST['message']."\n\n";
$body.= $_POST['name'].'<'.$_POST['email'].'>';
$headers = 'From: '.$_POST['name'].'<'.$_POST['email'].'>'."\r\n";

//send email
if(mail(RECIPIENT, $_POST['subject'], $body, $headers)){
		
	echo "success";
	
} else {
	
	echo "error";
	
}//end if else
	
?>

Code:
// JavaScript Document
$.fn.formValidation = function(){
	
	//set variables for all the various form elements;
	var name = $(this).find("#name");
	var email = $(this).find("#email");
	var subject = $(this).find("#subject");
	var message = $(this).find("#message");
	var spam = $(this).find("#spam");
	var formFields = $(this).find("input:text, textarea");
	var status = $(this).find("#status");
	var resetBtn = $(this).find("#reset"); 
	
	/*submit handler for contact form*/
	$(this).submit(function(e) {	
			
			//prevents default form action
		 	e.preventDefault();
			
			formFields.removeClass("error-focus");
			
			//check required fields are not empty and that the email address is valid
			if(name.val()==""){
				
				errorMessage("Please Enter Your Name");
				name.focus().addClass("error-focus");
				
			}else if(email.val()==""){
				
				errorMessage("Please Enter Your Email Address");
				email.focus().addClass("error-focus");
				
			}else if(!isValidEmail(email.val())){
				
				errorMessage("Please Enter a Correct Email Address");
				email.focus().addClass("error-focus");
				
			}else if(subject.val()==""){
				
				errorMessage("Please Enter a Subject");
				subject.focus().addClass("error-focus");
				
			}else if(message.val()==""){
				
				errorMessage("Please Enter Your Message");
				message.focus().addClass("error-focus");
				
			}else if(!spam.val()==""){
				
				errorMessage("Spam Attack!!");

			}else{
				
				//if all fields are valid then send data to the server for processing
				successMessage("Email being sent... please wait");
				
				//serialize all the form field values as a string
				var formData = $(this).serialize();
				
				//send serialized data string to the send mail php via POST method
				$.ajax({
					 
					 type: "POST",
					 url: "send-mail.php",
					 data: formData,
					 
					 success: function(sent){
						 
						 if(sent=="error"){ 
						 
							 	errorMessage("Opps, something went wrong - message not sent");
							 
						  } else if(sent=="success"){
								
								successMessage("Thanks "+name.val()+", your message has been successfully sent");
								
								//clear form fields
								formFields.val("");
								
						  }//end if else
							
					 }//end success function
					 
				});//end ajax function
				
			}//end else

   });//end submit
	
	//click handler for reset button - hides the status message
	resetBtn.click(function(){
			status.slideUp("fast");
			formFields.removeClass("error-focus");														
	});
	
	//functions
	function errorMessage(message){
		status.html(message).attr("class", "error").slideDown("fast");
	}
	
	function successMessage(message){
		status.html(message).attr("class", "success").slideDown("fast");
	}
	
	function isValidEmail(email) {
		var emailRx = /^[\w\.-]+@[\w\.-]+\.\w+$/;
		return  emailRx.test(email);
	}
													 
};
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Note that you are spoofing the From header in the email.
PHP:
$headers = 'From: '.$_POST['name'].'<'.$_POST['email'].'>'."\r\n";

//send email
if(mail(RECIPIENT, $_POST['subject'], $body, $headers)){
.
.
.

You are only allowed to send from a mail account that belongs to your hosting account; anything else will be discarded. Again, this is to prevent spam and email phishing originating from x10's servers.

And when I said you didn't include jQuery, I meant the library itself. Not that I wanted you to include it -- I was just trying to point out that showing us the front-end code was not going to do a whole lot of good, since it doesn't actually send mail. It just makes a request to the send-mail.php script, which I did ask you to post previously.
 
Status
Not open for further replies.
Top