Jump to content

Php Mail() Not Sending Mail


jalpro

Recommended Posts

Hey!

 

I have a php mail() function that just refuses to send!

 

I was wondering if someone here could check my code or suggest anything that might be preventing this from working.

 

HTML / jQUERY / AJAX:

<div id="contactformcontainer">
  <form method="post" id="contactform">
   <table>
	<tr>
	 <td><label>Name</label></td>
	 <td><input class="formbox" type="text" name="contactname" id="contactname"/></td>
	</tr>
	<tr>
	 <td><label>Email</label></td>
	 <td><input class="formbox" type="email" name="contactemail" id="contactemail"/></td>
	</tr>
	<tr>
	 <td><label>Subject</label></td>
	 <td><input class="formbox" type="text" name="contactsubject" id="contactsubject"/></td>
	</tr>
	<tr>
	 <td><label>Message</label></td>
	 <td><textarea class="formbox" name="contactmessage" id="contactmessage"></textarea></td>
	</tr>
	<tr>
	 <td><label>Anti-spam</label></td>
	 <td>
	  <script>
	   $(document).ready( function() {
		var randNumber1 = Math.floor(Math.random()*10)
		var randNumber2 = Math.floor(Math.random()*10)
		sumNumbers = randNumber1 + randNumber2;
		$('#randomNumbers').html(randNumber1 + ' + ' + randNumber2 + ' = ');
	   });
	  </script>
	  <span id="randomNumbers"></span>
	  <input class="formbox" type="text" name="contactantispam" id="contactantispam"/>
	 </td>
	</tr>
	<tr>
	 <td></td>
	 <td><input name="contactsubmit" id="contactsubmit" value="Send"/></td>
	</tr>
   </table>
  </form>
  <div id="success"><p>Thank you. Your message was successfully delivered.</p></div>
  <div id="empty"><p>Please ensure all fields are filled out correctly.</p></div>
 </div>

 <script>
 $('#contactsubmit').click(function() {
  var emailformat = /^([A-Za-z0-9_\+\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

  if( $('#contactname').attr('value') !== '' && emailformat.test( $('#contactemail').attr('value') ) && $('#contactsubject').attr('value') !== '' && $('#contactmessage').attr('value') !== '' && $('#contactantispam').attr('value') == sumNumbers ) {
   var contactname = $('#contactname').attr('value');
   var contactemail = $('#contactemail').attr('value');
   var contactsubject = $('#contactsubject').attr('value');
   var contactmessage = $('#contactmessage').attr('value');
  
   var datastring = "contactname=" + contactname + "&contactemail=" + contactemail + "&contactsubject=" + contactsubject + "&contactmessage=" + contactmessage;
   //alert(datastring);
   $('#empty').hide();
   $('#contactsubmit').hide();
  
   $.ajax({
	type: "POST",
	url: "php/contactform.php",
	data: datastring,
	success: function(){
	 alert("success!");
	 $('#success').fadeIn();
	}
   });
   return false;
  } else {
   alert("empty");
   $('#empty').fadeIn();
  }
 });
 </script>

 

PHP:

<?php
$to = "james@jalproductions.co.uk";
$contactname = $_REQUEST["contactname"];
$contactemail = $_REQUEST["contactemail"];
$subject = $_REQUEST["contactsubject"];
$contactmessage = $_REQUEST["contactmessage"];
$headers = "From: $contactemail\r\n";
$headers .= "Reply-to: $contactemail";

$body = "Name: $contactname\nEmail: $contactemail\nMessage:\n$contactmessage";

mail($to, $subject, $body, $headers);
echo "$to";
echo "$subject";
echo "$body";
echo "$headers";
?>

 

Using alerts and echos, it seems that the data is being sent to the php code perfectly fine. The echos in the php code print out exactly what was entered into the form.

I tried removing the jQUERY/AJAX and adding an action="" to the form tag so that it just runs the php on its own, but this still doesn't work!

 

Help!

Thanks! :)

 

EDIT: I'm not using this code on a HelioHost hosted site.

Link to comment
Share on other sites

You did mention that you had verified that the data was being sent to the PHP script correctly; this means everything on the client side is fine. Are you sure the PHP mail() function is configured correctly?

 

Try this test script:

 

<?php

mail('youremailhere', 'TEST EMAIL', 'TEST EMAIL');

 

Of course, be sure to replace "youremailhere" with your email. Then, verify that you actually get the email.

 

 

If you do, try the function with the headers added and see if you still get the email.

Link to comment
Share on other sites

Made the php file. Put in my email (within the apostrophes). Uploaded. Directed my browser to the page.

Doesn't work. No email.

Tried a different email on a different host. Didn't work either.

Both emails I tried aren't hosted by the same host where the files are hosted btw. But that shouldn't matter... right?

I'll try it with an email from that host, and check tomorrow if it went through.

Link to comment
Share on other sites

Core

PHP Version: 5.3.5

mail.add_x_header: On On

mail.force_extra_parameters: no value no value

mail.log: no value no value

sendmail_from: no value no value

sendmail_path: no value no value

 

Standard

Internal Sendmail Support for Windows: enabled

 

 

Not sure what I'm looking for? Just Ctrl+F'd "mail".

Does the above mean anything to you?

Or do you need something else?

 

Thanks! :)

Link to comment
Share on other sites

sendmail_path: no value no value

 

The absence of the sendmail_path indicates that the PHP mail() function is not enabled on that server. Basically, you have two options at this point:

  • Ask the server administrator to configure the sendmail_path to the correct value (on our server it's "/usr/sbin/sendmail -t -i").
  • Send the mail with SMTP; you have several options here:
     
  • -->Write a class to interface with an SMTP server yourself.
     
  • -->Use a class or extension that is already made.

Link to comment
Share on other sites

Thanks!

I messaged the host and they're changing my server from Windows to Linux.

Hopefully this will work!

 

For future reference, does php mail() only work on Linux servers? Or is it just the way that this host has set up their Windows server?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...