0

I'm trying to contain some HTML in an email receipt based off a form submission, however all the html in $message2 is being sent as plain text.

Code:

$to = "[email protected]"; // this is your Email address
    $from = $_POST['EMAIL']; // this is the sender's Email address
    $first_name = $_POST['FIRSTNAME'];
    $last_name = $_POST['SURNAME'];
    $subject = "subject";
    $subject2 = "subject";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['PRODUCTNAME'];
    
  
    $message2 = 
    "<html>" .
        "<head>" .
    "<title>HTML email</title>" .
        "</head>" .
        " <table cellspacing='0' cellpadding='0' border='0' align='center' width='600' style='margin: auto;' class='email-container'>
    <tr>
        <td style='padding: 20px 0; text-align: center'>
            <img src='http://placehold.it/200x50' width='200' height='50' alt='alt_text' border='0'>
        </td>
    </tr>" . 
    " <tr> " . 
        "<td style='padding: 20px 0; text-align: center'>" .
            "Hi " . $first_name . "," . "\n\n" . "You have sucessfully registered " . $_POST['PRODUCTNAME'] . " (" . $_POST['PRODUCTCODE'] . ")" . "\n\n" .
            "Your registration code is " . $unixNow . "\n\n" . "If you have any queries or require assistance, please contact [email protected]" . "\n\n" . 
            "Please retain a copy of your email confirmation. " .
        " </td> " .
    " </tr> " .
    "<tr>
        <td style='padding: 20px 0; text-align: center'>
            <img src='http://placehold.it/200x50' width='200' height='50' alt='alt_text' border='0'>
        </td>
    </tr>" . 
        "</table>" .
    "</html>";

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    
?>

Can someone explain what i'm doing wrong and how to correct my error?

7
  • 4
    $headers = "From:" . $from; overwrites your previous $headers, so no ContentType Commented Apr 12, 2021 at 9:40
  • Thanks! How do get around that? Do I just add all the statements within one '$headers =' Commented Apr 12, 2021 at 9:42
  • 2
    The same way you added Content-type:text/html;charset=UTF-8 to $headers: concatenate using .=: $headers .= "From:" . $from; Commented Apr 12, 2021 at 9:43
  • That doesn't appeared to have had any affect Commented Apr 12, 2021 at 9:53
  • 2
    "all the html in $message2" Because you're using $headers2 in message #2 - which doesn't have the MIME and ContentType set. Commented Apr 12, 2021 at 9:57

1 Answer 1

1

Your message #2 doesn't use your defined MIME/ContentType headers. $headers = "From:" . $from; would also overwrite those. Name your MIME/ContentType headers to a different name and add it to your message #2 headers:

// Always set content-type when sending HTML email
$htmlHeaders = "MIME-Version: 1.0" . "\r\n";
$htmlHeaders .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers = "From:" . $from;
$headers2 = $htmlHeaders . "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.