0
votes

Sorry, can anyone help please?

We had some PHP code which emailed a person enquiring about our services on our website. The email used to give a standard email response back to the enquirer formatted in HTML. But sometime in June 2020 although we couldn't see that any changes were made, the emails started sending without HTML format and basically showing the HTML code.

Below is the part of the PHP code which creates the email but I can't see anything wrong with it. So if there is no issue with the code below, what could cause the code to suddenly break and change the response emails which were working in HTML to start sending as HTML code? (or is there something wrong with the code I can't see?)

Any help, pointers or ideas would be really appreciated.

    $email_from = '[email protected]';//<== update the email address
        $email_subject = "Thank You for making enquiry";
        $email_body = '<!DOCTYPE html>
                <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
                <link href="https://fonts.googleapis.com/css?family=Crimson+Text" rel="stylesheet">
                <html>
                <head>
                    <title></title>
                </head>
                    <body>
                        EMAIL CONTENT WILL GO HERE!
                    </body>
                </html>';
    
        $to = $EnquiryEmailAddress;//<== update the email address
        // Always set content-type when sending HTML email
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type:text/html;charset=ISO-8859-1' . "\r\n";
    
        $headers .= "From: $email_from \r\n";
        $headers .= "Reply-To: $email_from \r\n";
    
        mail($to,$email_subject,$email_body,$headers); 
1
What is the error you recieve? check the error.log file as well - Burhan Kashour
You can't use regular HTML in emails.en.m.wikipedia.org/wiki/HTML_email - ashu
Thanks for the info. I can't find an error.log. The email is still being sent and no error appearing, it has just lost all its HTML formatting (which previously worked) and displayed and now the emails are displaying the full HTML code as part of the email instead. - Digital_LCBT

1 Answers

0
votes

This code relies on the server outbound email is properly setup. There is a email processor built in PHP called PHPMailer. I encourage you to have a look at that package, it will help you a lot. You can read about it here: https://github.com/PHPMailer/PHPMailer

Secondly, I recommend that you UTF-8 encode your emails instead of using ISO-8859-1 (if possible).

Also, if your'e running the mail send command while a web page is rendered, the mail send command might take some time. A good approach is to batch send email when users are not waiting for a webpage to render. This can be done by (for example) store all outbound emails in a database table and have a batch processor work off that table and send the emails sequentially.

Finally, as mentioned in a comment on your question: beware that HTML in emails is very limited, so you have to be careful when constructing the email HTML.

Best of luck!