JohnGreen Posted November 3, 2017 Share Posted November 3, 2017 (edited) Goodday everyone Need some help hope someone can help. I got a script for a contact form to send email but its not working -if you fill it in it runs through but its not sending emails. I only want to use PHP mail The form only have the following fields to be filled in Name Email Subject Message Here are the code i need some help please <?php //====================================================================== // Variables //====================================================================== //E-mail address. Enter your email define("__TO__", "support@emaildomain.com"); //Success message define('__SUCCESS_MESSAGE__', "Your message has been sent. We will reply soon. Thank you!"); //Error message define('__ERROR_MESSAGE__', "Your message hasn't been sent. Please try again."); //Messege when one or more fields are empty define('__MESSAGE_EMPTY_FIELDS__', "Please fill out all fields"); //====================================================================== // Do not change //====================================================================== //E-mail validation function check_email($email){ if(!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){ return false; } else { return true; } } //Send mail function send_mail($to,$subject,$message,$headers){ if(@mail($to,$subject,$message,$headers)){ echo json_encode(array('info' => 'success', 'msg' => __SUCCESS_MESSAGE__)); } else { echo json_encode(array('info' => 'error', 'msg' => __ERROR_MESSAGE__)); } } //Get data form and send mail if(isset($_POST['name']) and isset($_POST['mail']) and isset($_POST['messageForm'])){ $name = $_POST['name']; $mail = $_POST['emaild']; $subjectForm = $_POST['subject']; $messageForm = $_POST['message']; if($name == '') { echo json_encode(array('info' => 'error', 'msg' => "Please enter your name.")); exit(); } else if($mail == '' or check_email($mail) == false){ echo json_encode(array('info' => 'error', 'msg' => "Please enter valid e-mail.")); exit(); } else if($messageForm == ''){ echo json_encode(array('info' => 'error', 'msg' => "Please enter your message.")); exit(); } else { $to = __TO__; $subject = $subjectForm . ' ' . $name; $message = ' <html> <head> <title>Mail from '. $name .'</title> </head> <body> <table style="width: 500px; font-family: arial; font-size: 14px;" border="1"> <tr style="height: 32px;"> <th align="right" style="width:150px; padding-right:5px;">Name:</th> <td align="left" style="padding-left:5px; line-height: 20px;">'. $name .'</td> </tr> <tr style="height: 32px;"> <th align="right" style="width:150px; padding-right:5px;">E-mail:</th> <td align="left" style="padding-left:5px; line-height: 20px;">'. $mail .'</td> </tr> <tr style="height: 32px;"> <th align="right" style="width:150px; padding-right:5px;">Subject:</th> <td align="left" style="padding-left:5px; line-height: 20px;">'. $subjectForm .'</td> </tr> <tr style="height: 32px;"> <th align="right" style="width:150px; padding-right:5px;">Message:</th> <td align="left" style="padding-left:5px; line-height: 20px;">'. $messageForm .'</td> </tr> </table> </body> </html> '; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; $headers .= 'From: ' . $mail . "\r\n"; send_mail($to,$subject,$message,$headers); } } else { echo json_encode(array('info' => 'error', 'msg' => __MESSAGE_EMPTY_FIELDS__)); } ?> I would appreciate help as it runs but nothing gets send but the test below sends email perfectly <?php ini_set( 'display_errors', 1 ); error_reporting( E_ALL ); $from = "support@emaildomain.com"; $to = "support@emaildomain.com"; $subject = "PHP Mail Test script"; $message = "This is a test to check the PHP Mail functionality"; $headers = "From:" . $from; mail($to,$subject,$message, $headers); echo "Test email sent"; ?> Thank you very much John Edited November 3, 2017 by JohnGreen Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 3, 2017 Share Posted November 3, 2017 Since you have code that works, you could add a piece at a time from the non working code testing along the way and build it till it breaks. Just one way you could debug this yourself. I would not recommend this method if you had a lot of code. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 3, 2017 Share Posted November 3, 2017 emails you send through/from your web hosting are NOT (getting tired of pointing out this obvious piece of information) being sent from the person's email address who filled in your form. they are being sent through/from your web hosting and the From: email address needs to correspond to your web hosting. do NOT use the email address that was entered in the form as the From: address. if you use the same From: email address that works in your second piece of code, in the first piece of code, it should work. you should also apply htmlentities() to all the dynamic pieces of data you are putting into the subject and message in order to prevent any nefarious content in those values from being rendered in the mail client being used to read the emails. Quote Link to comment Share on other sites More sharing options...
JohnGreen Posted November 3, 2017 Author Share Posted November 3, 2017 Thanks benanamen and Mac-gyver.....Macgyver I hear about its being sent from ISP but its not clear to me which I must use.....should the to at the top be changed to from ??? I am trying to make the top code work but something is not cosher there. I have change the headers to the same in the botom....it doesnt want to send now #confused!!!!!!! Quote Link to comment Share on other sites More sharing options...
JohnGreen Posted November 3, 2017 Author Share Posted November 3, 2017 (edited) There is differences My headers at the botom I have changed to $headers .= 'From: ' . $from; ------like the botom code send_mail($to,$subject,$message,$headers); mail($to,$subject,$message,$headers); This is also different ???? Edited November 3, 2017 by JohnGreen Quote Link to comment Share on other sites More sharing options...
dalecosp Posted November 3, 2017 Share Posted November 3, 2017 (edited) There is differences My headers at the botom I have changed to $headers .= 'From: ' . $from; ------like the botom code send_mail($to,$subject,$message,$headers); mail($to,$subject,$message,$headers); This is also different ???? I'm not sure what you mean, "this is also different?" mail() is a native PHP function, and send_mail() is simply a wrapper around the mail() call that tests it for success and sends __SUCCESS_MESSAGE__ or __ERROR_MESSAGE__ out as JSON. You should be able to tell which it's responding with. If you're using mail() instead of send_mail(), you should just assign a variable to it and check the value of this variable. $success = mail($to, $subject, $message, $headers); if (!$success) echo "Mail did not send!!"; Keep in mind that for multiple headers, you must end each with "\r\n". mac_gyver is very correct to speak to you about DELIVERABILITY, which is totally different than the success or failure of a call to mail(). It seems to me that you need your sysadmin to take a look at the server's mail-logs. Edited November 3, 2017 by dalecosp Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.