jalpro Posted November 29, 2011 Posted November 29, 2011 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.
Byron Posted November 29, 2011 Posted November 29, 2011 Your php part is working fine. Did you check your spam folder?
jalpro Posted November 29, 2011 Author Posted November 29, 2011 Sorry, I should have mentioned that this code isn't being used for the site in my sig.Nothing in my spam folder though.
Byron Posted November 29, 2011 Posted November 29, 2011 But it is being used for a site on heliohost, right? Sorry I can't help your with JQuery.
jalpro Posted November 29, 2011 Author Posted November 29, 2011 No it's not.Was hoping I could get some help here though.Is that allowed?
Byron Posted November 29, 2011 Posted November 29, 2011 Yeah I guess that's ok just as long as the admin. know that it's not on our servers.
jalpro Posted November 29, 2011 Author Posted November 29, 2011 Okay.So you say the PHP looks fine.Can anyone verify the HTML?Thanks Oh and the HTML W3C validates except for 4 duplicate IDs that I have yet to fix.But they shouldn't be causing the problem, should they?
Guest xaav Posted November 30, 2011 Posted November 30, 2011 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.
jalpro Posted November 30, 2011 Author Posted November 30, 2011 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.
Krydos Posted November 30, 2011 Posted November 30, 2011 It sounds to me like whatever host you have this code on doesn't have php mail() enabled. Maybe take a look at phpinfo()?
jalpro Posted November 30, 2011 Author Posted November 30, 2011 CorePHP Version: 5.3.5mail.add_x_header: On Onmail.force_extra_parameters: no value no valuemail.log: no value no valuesendmail_from: no value no valuesendmail_path: no value no value StandardInternal 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!
Krydos Posted November 30, 2011 Posted November 30, 2011 Well, the differences between what you posted and johnny/stevie info that is known to work is the path to sendmail. Can you get mail() function to work at all with the simplest possible code like xaav posted?
Guest xaav Posted November 30, 2011 Posted November 30, 2011 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.
jalpro Posted November 30, 2011 Author Posted November 30, 2011 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?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now