1

I have a working email script. Now I want to add the senders email in CC, so they get a copy of the email as a result. So I added the $headers part to it and in the email on the receiving end it shows as being CC'd, but the CC'd email does not actually get the email. Any thoughts?

    <?php
        if($_POST) {
            $yourEmail = "myemail@business.com";

            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $email = $_POST['email'];
            $phone = $_POST['phone'];
            $message = $_POST['message'];
            $headers = 'From:' . $email . "\r\n" . 'Cc:' . $email . "\r\n";

            $responseRefresh = "Redirecting in 5 seconds.<br>";
            $responseFillData = "Please fill all the data.<br>";
            $responseValidEmail = "Please enter valid email.<br>";
            $responseCompleted = "Form successfully submitted.<br>";
            $responseFailed = "Form failed to be submitted.<br>";

            $response = "" . $responseRefresh;

            $hasError = false;
            if($fname == "" || $lname == "" || $email == "" || $phone == "") {
                $hasError = true;
                $response = $responseFillData . $response;
            }

            if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i", $email)) {
                $hasError = true;
                $response = $responseValidEmail . $response;
            }

            if($hasError) {
                header("refresh:5;url=index.html#contact");
                echo $response;
                return false;
            }

            $text = "Voornaam: $fname \n
                Familienaam: $lname \n
                E-mailadres: $email \n
                Telefoonnummer: $phone \n
                Bericht: \n
                $message";

            if(mail($yourEmail, $fname . " " . $lname . " sent a message via de DDW website", $text, $headers)) {
                $response = $responseCompleted . $response;
            }
            else {
                $response = $responseFailed . $response;
            }

            header("refresh:5;url=index.html#contact");
            echo $response;
        }
    ?>

Next to that, I also want to format the response message. I tried adding some html elements to the message, but that does not seem to work. How can I add my image in the 'success' response?

Example:

$responseCompleted = "Form successfully submitted. <img src="http://www.ddw-sanitair.be/img/vink.png"><br>";
Johan
  • 21
  • 1
  • 4
  • Mail won't be delivered to an inbox for a multitude of reasons - even if the `mail` function returns true. Use `\r\n` instead of `\n`. Also, try to use a unique CC address just to test. – waterloomatt Oct 24 '19 at 16:08
  • I got it to work (see updated post above), however the CC'd email does not actually get the email ?! – Johan Oct 24 '19 at 17:29

0 Answers0