justin7410 Posted December 7, 2015 Share Posted December 7, 2015 hey guys, i am trying to auto generate an email. i have a function that calls for the following use of : <?php $emailSub = 'Test Subject'; $emailTo = 'myemail@gmail.com'; $emailMsg = 'You have a new email that must be read below:<br> '; mail($emailTo,$emailSub,$emailMsg); ?> i dont seem to get any email. any suggestions as to why ? Quote Link to comment https://forums.phpfreaks.com/topic/299660-mail-function-not-working/ Share on other sites More sharing options...
Solution requinix Posted December 7, 2015 Solution Share Posted December 7, 2015 There is so much more to doing emailing properly, and so many ways it can go wrong. Do yourself a favor and get yourself a copy of PHPMailer. (There are other things but it's the most popular.) There are tons of examples and plenty of documentation on how to use it, and it will take care of everything for you. 2 Quote Link to comment https://forums.phpfreaks.com/topic/299660-mail-function-not-working/#findComment-1527620 Share on other sites More sharing options...
justin7410 Posted December 9, 2015 Author Share Posted December 9, 2015 Thanks for the feedback Requinix. I used a mail function library and installed this into my site. ( i added the following code into a file called mailtest.php ) ran it and success, i got my test email sent to me and everything was all good. I then tried to add this into my existing ( working ) php contact form that uses jquery and ajax to run a no refresh UI form. the form worked and all the functions ran perfectly. So i assumed just adding the code that worked inside one file would work inside my working conditional of the "completed" form. Does not seem to work . Not sure why the email wont go out anymore. php file that the ajax / jquery is taking from. <?php sleep(3); $name = trim($_POST['name']); $email = trim($_POST['email']); $subject = trim($_POST['subject']); $message = trim($_POST['message']); $honeypot = $_POST['honeypot']; $humancheck = $_POST['humancheck']; if ($honeypot == 'http://' && empty($humancheck)){ /// RUN SPAMBOT CHECK AND IF PASS CHECK TO MAKE SURE ALL FIELDS ARE OK $errorMsg = ''; $reg_exp = "/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/"; if(!preg_match($reg_exp, $email)){ $errorMsg .= "<p>A valid email is required</p>"; } if(empty($name)){ $errorMsg .= '<p>Please provide your name</p>'; } if(!isset($message)){ $errorMsg .= '<p>Please provide a message</p>'; print_r($_POST); } if(!empty($errorMsg)){ // IF NO ERRORS AND PASSES ARE ALL CHECKED , CONFIRM AND SEND EMAIL $return['error'] = true; $return['msg'] = 'Sorry the request was successful but your form was not correctly filled. ' . $errorMsg; echo json_encode($return); exit(); }else{ // SEND EMAIL TO CLIENT // ADD FOLDER FOR MAIL LIBRARY require 'PHPMailer-master/PHPMailerAutoload.php'; // BEGIN TO DEFINE EMAIL VARIABLES $mail = new PHPMailer; //$mail->SMTPDebug = 3; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.mandrillapp.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'MYusername'; // SMTP username $mail->Password = 'hidden_pass'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to $mail->From = "myemail@domain.com"; $mail->FromName = "DO-NOT REPLY"; $mail->AddAddress($email); $mail->Subject = "First PHPMailer Message"; $mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer."; $mail->WordWrap = 50; if($mail->Send()) { $return['error'] = false; $return['msg'] = '<h4>' . $name . ', thank you for your email.</h4> Your email from: ' . $email.' has been sent and one of our team members will contact you shortly..'; echo json_encode($return); } else { $return['error'] = true; $return['msg'] = 'THE EMAIL WAS NOT SENT .. PLEASE TRY AGAIN'; echo json_encode($return); } } } else { $return['error'] = true; $return['msg'] = 'Oops...there was a problem with your submission. Please try again' ; echo json_encode($return); } ?> Again, the mail() function works when i load it outside the conditional. but then in checking the conditional, it works within the PHP script and executes properly (displaying, that the user is submitting and validating from correctly. $return['msg'] = '<h4>' . $name . ', thank you for your email.</h4> Your email from: ' . $email.' has been sent and one of our team members will contact you shortly..'; They just dont seem to work together ?? any suggestions ? Quote Link to comment https://forums.phpfreaks.com/topic/299660-mail-function-not-working/#findComment-1527714 Share on other sites More sharing options...
requinix Posted December 9, 2015 Share Posted December 9, 2015 So you're seeing that "thank you for your email" message but not receiving the email? Did you change anything after taking the code from the original test script (where you got the email using PHPMailer) and copy/pasting it into the script that handles the contact form? And I assume the values for Username and Password and From are all correct in your real code and you edited those just for posting it here? Quote Link to comment https://forums.phpfreaks.com/topic/299660-mail-function-not-working/#findComment-1527715 Share on other sites More sharing options...
justin7410 Posted December 9, 2015 Author Share Posted December 9, 2015 Yes everything was correct. It now seems to work flawlessly. seems there was a hiccup in the mail server, and all the requests / response got batched and sent together. so i think the code ( which it should ) works fine. i think this was more of an email server issue. Quote Link to comment https://forums.phpfreaks.com/topic/299660-mail-function-not-working/#findComment-1527716 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.