bizstorm Posted August 15, 2020 Share Posted August 15, 2020 (edited) I would like to send a copy of the mail contents to sender. The mail is sent to me but not the sender. Below is my code but not sure where the error in code is . Any help would be great Thanks <?php if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) if(empty($_POST[‘g-recaptcha-response’])){ $errMsg = 'Please check the robot checkbox.'; } else if(isset($_POST[‘g-recaptcha-response’]) && !empty($_POST[‘g-recaptcha-response’])) { $secret = 'secret code'; $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']); $responseData = json_decode($verifyResponse); if($responseData->success) { $succMsg = 'Your contact request have submitted successfully.'; } else { $errMsg = 'Robot verification failed, please try again.'; } } if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "email@gmail.com"; $email_subject = "Online Contact Form"; $email_subject2 = "Please find a copy of the contact form you submitted"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". $headers2 = "From:" . $email_to; 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_from, $email_subject2, $email_message, $headers2); @mail($email_to, $email_subject, $email_message, $headers); header("Location: contact-thank-you.html"); ?> Thank you for contacting us. We will be in touch with you very soon. <?php } ?> Edited August 15, 2020 by bizstorm Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/ Share on other sites More sharing options...
bizstorm Posted August 15, 2020 Author Share Posted August 15, 2020 not sure where the mistake or error in code is Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580638 Share on other sites More sharing options...
requinix Posted August 15, 2020 Share Posted August 15, 2020 Remove the @s. That hides errors. If there are errors you need to know about them. Was the email marked as spam/junk? Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580639 Share on other sites More sharing options...
bizstorm Posted August 15, 2020 Author Share Posted August 15, 2020 No. It is not sending the copy to sender Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580640 Share on other sites More sharing options...
requinix Posted August 15, 2020 Share Posted August 15, 2020 Did you remove the @s? Next step is to modify your code so that you can know whether each mail() was successful: by checking the return value. If either call returns false then the mail will not get sent. Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580646 Share on other sites More sharing options...
bizstorm Posted August 16, 2020 Author Share Posted August 16, 2020 i did remove th @s . mail($email_to, $email_subject, $email_message, $headers); is working as the sent mail is received. mail($email_from, $email_subject2, $email_message, $headers2); this code is not executed. - Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580659 Share on other sites More sharing options...
requinix Posted August 16, 2020 Share Posted August 16, 2020 Did you do the other part of what I said in my post? Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580661 Share on other sites More sharing options...
bizstorm Posted August 16, 2020 Author Share Posted August 16, 2020 neither has failed as it proceed to thank you page, however on mail($email_from, $email_subject2, $email_message, $headers2); no mails received Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580662 Share on other sites More sharing options...
gw1500se Posted August 16, 2020 Share Posted August 16, 2020 Did you check your maillog for errors? I suggest you use PHPMailer as it is much easier, gives you better control and is also easier to debug. Quote Link to comment https://forums.phpfreaks.com/topic/311333-send-copy-of-mail-to-sender/#findComment-1580664 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.