0

I am using the following script as a contact form on my website:

    <?php
    require('recaptcha-master/src/autoload.php');
    $from = 'Contact Form <info@mydomain.com>';
    $sendTo = 'Contact Form <contact@mydomain.com>';
    $subject = 'New Contact';
    $fields = array('name' => 'name', 'company' => 'company', 'email' => 'email', 'phone' => 'phone'); // array variable name => Text to appear in the email
    $okMessage = 'Thank you, we will be in touch soon';
    $errorMessage = 'There was an error while submitting the form. Please try again later';
    $recaptchaSecret = 'xxxxxxxxxxx';
    try
    {
        if (!empty($_POST)) {    
            if (!isset($_POST['g-recaptcha-response'])) {
                throw new \Exception('ReCaptcha is not set.');
            }
            $recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());   
            $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
            if (!$response->isSuccess()) {
                throw new \Exception('ReCaptcha was not validated.');
            }
            $emailText = "You have new message from contact form\n=============================\n";
            foreach ($_POST as $key => $value) {

                if (isset($fields[$key])) {
                    $emailText .= "$fields[$key]: $value\n";
                }
            }
            $headers = array('Content-Type: text/plain; charset="UTF-8";',
                'From: ' . $from,
                'Reply-To: ' . $from,
                'Return-Path: ' . $from,
            );
            mail($sendTo, $subject, $emailText, implode("\n", $headers));
            $responseArray = array('type' => 'success', 'message' => $okMessage);
        }
    }
    catch (\Exception $e)
    {
        $responseArray = array('type' => 'danger', 'message' => $errorMessage);
    }

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $encoded = json_encode($responseArray);
        header('Content-Type: application/json');
        echo $encoded;
    }
    else {
        echo $responseArray['message'];
    }

This works very well for me, however I would like to add an automatic thank you to be sent to the provided email address of the person that submits the form. Is there any method to have this accomplished on this script? Some expert advise would be really helpful.

Thank you very much!

AnFi
  • 10,493
  • 3
  • 23
  • 47
  • Sure! PHP's [mail() function](http://php.net/manual/en/function.mail.php) or the popular mail library [PHPMailer](https://github.com/PHPMailer/PHPMailer) – tshimkus Mar 13 '19 at 07:33
  • Possible duplicate of [How to send an email using PHP?](https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) – tshimkus Mar 13 '19 at 07:34

0 Answers0