0

I want to create a contact form for the website im working on which uses PHP and Recaptcha. Everything works fine including Recaptcha and when I hit send it shows message successfully sent, but I'm not getting any mail in the account given.

I wanted to know which mail does the contact form send the mail from?. Before I was testing it in xampp, I just modified the sendmail.ini and entered my Gmail details and it used to work, but now I'm hosting this in my hosting space, but I don't know if I will have to create a domain mail and link the two PHP somehow.

    <!--Section heading-->
    <h2 class="h1-responsive font-weight-bold text-center my-4">Contact us</h2>
    <!--Section description-->
    <p class="text-center w-responsive mx-auto mb-5">Do you have any questions? Please do not hesitate to contact us directly. Our team will come back to you within
        a matter of hours to help you.</p>

    <div class="row">
        <!--Grid column-->
        <div class="col-md-9 mb-md-0 mb-5 mx-auto">
            <form action="" method="post">

                <!--Grid row-->
                <div class="row">

                    <!--Grid column-->
                    <div class="col-md-6">
                        <div class="md-form mb-0">
                            <input type="text" id="name" name="name" class="form-control"required>
                            <label for="name" class="">Your name</label>
                        </div>
                    </div>
                    <!--Grid column-->

                    <!--Grid column-->
                    <div class="col-md-6">
                        <div class="md-form mb-0">
                            <input type="text" id="email" name="email" class="form-control" required>
                            <label for="email" class="">Your email</label>
                        </div>
                    </div>
                    <!--Grid column-->

                </div>
                <!--Grid row-->

                <!--Grid row-->
                <div class="row">
                    <div class="col-md-12">
                        <div class="md-form mb-0">
                            <input type="text" id="subject" name="phone" class="form-control"required>
                            <label for="subject" class="">Phone No.</label>
                        </div>
                    </div>
                </div>
                <!--Grid row-->

                <!--Grid row-->
                <div class="row">

                    <!--Grid column-->
                    <div class="col-md-12">

                        <div class="md-form">
                            <textarea type="text" id="message" name="message" rows="2" class="form-control md-textarea" required></textarea>
                            <label for="message">Your message</label>
                        </div>

                    </div>
                </div>

                <div class="row">

                        <!--Grid column-->
                        <div class="col-md-12">

                            <div class="md-form">
                                <div class="g-recaptcha" data-sitekey=></div>
                                <input type="submit" name="submit" value="Send Message" class="submit-btn">
                            </div>

                        </div>
                </div>
                <!--Grid row-->

            </form>
            <div class="status">
                    <?php
                    if(isset($_POST['submit']))
                    {
                        $User_name = $_POST['name'];
                        $phone = $_POST['phone'];
                        $user_email = $_POST['email'];
                        $user_message = $_POST['message'];

                        $email_from = '';
                        $email_subject = "New Form Submission";
                        $email_body = "Name:$User_name.\n".
                                        "Phone No: $phone.\n".
                                        "Email Id: $user_email.\n".
                                        "User Message: $user_message.\n";

                        $to_email = "fekecih@drmail.net";
                        $headers = "From: $email_from \r\n";
                        $headers .="Reply-To: $user_email\r\n";

                        $secretKey =
                        $responseKey = $_POST['g-recaptcha-response'];
                        $userIP = $_SERVER['REMOTE_ADDR'];
                        $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";

                        $response = file_get_contents($url);
                        $response = json_decode($response);

                        if ($response->success)
                        {
                            mail($to_email,$email_subject,$email_body,$headers);
                            echo "Message sent Successfully";
                        }
                        else
                        {
                            echo "<span>Invalid Captcha, Please Try Again,</span>";
                        }
                    }
                    ?>
            </div>
        </div>

    </div>

</section>
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    Since you set $email_from = ''; then it will not come from any email address at all. That's probably the main reason it's not working. Choose an address which has the same domain as the domain where the site is hosted. It doesn't have to be a real mailbox, but setting the right domain reduces the chance of the message being flagged as spam – ADyson Sep 15 '19 at 09:39
  • 1
    P.s. be aware that not receiving a message does not mean the message was never sent. I note that you don't check the result of the mail() function though so you don't actually know if that call succeeded or not. I suggest you read this: https://stackoverflow.com/a/24644450/5947043 – ADyson Sep 15 '19 at 09:42
  • First try simple mail from your hosting and check mail sent or not... mail ("to@email.com", ""Test Subject", "Message") – Harshil Patel Sep 16 '19 at 07:55
  • yes, my webmail works, I talked to the support from my hosting provider to get access to PHP and sendmail ini file but they refused my request and gave me a php script which I have to integrate with my current code which will let me use the smpt. – Ameer Khan Ashraf Sep 17 '19 at 08:09

0 Answers0