loading...

. . . . . .

let’s make something together

Give us a call or drop by anytime, we endeavour to answer all enquiries within 24 hours on business days.

Find us

PO Box 16122 Collins Street West
Victoria 8007 Australia

Email us

info@domain.com
example@domain.com

Phone support

Phone: + (066) 0760 0260
+ (057) 0760 0560

, ,

Form To Emails get sent out as raw HTML on PHP 8 (Fixed)

  • By admin
  • August 12, 2022
  • 867 Views

HTML emails are typically sent using a combination of PHP code and HTML markup. To ensure that the HTML code is properly interpreted by the email client, the HTML code needs to be formatted correctly and the email headers need to be set properly. Here are some steps you can take to send HTML emails in PHP:

  1. Start by creating a new PHP file and including the necessary email libraries or functions, such as the PHPMailer library or the built-in mail() function.
  2. Use the appropriate functions to set the email headers, including the From, To, Subject, and Content-Type headers. The Content-Type header should be set to “text/html” to indicate that the email contains HTML content.
  3. Write the HTML code for the email, including any images or links that you want to include. Be sure to test the HTML code in a web browser to ensure that it renders correctly.
  4. Use the email library or function to send the email. Make sure to include the HTML code as the message body.

Here is an example code snippet that demonstrates how to send an HTML email using the PHPMailer library:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your-email@gmail.com';
    $mail->Password = 'your-email-password';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    //Recipients
    $mail->setFrom('your-email@gmail.com', 'Your Name');
    $mail->addAddress('recipient-email@example.com', 'Recipient Name');

    //Content
    $mail->isHTML(true);
    $mail->Subject = 'HTML Email Example';
    $mail->Body    = '<h1>Hello!</h1><p>This is an example of an HTML email.</p>';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

This code sets up a new PHPMailer object and configures it to use Gmail as the SMTP server. It then sets the From and To email addresses, as well as the Subject and HTML message body.

This code works best till php version 7.4 and below but since the update of php 8 and 8.1, above code has started created issues on many of the sites worldwide.

Users are getting the below messages now a days:

or email is coming in RAW html like below:

Today we’ve come up with the issue to fix the same. here are some possible solutions:

Solution 1:

Update PHPMailer: Make sure you are using the latest version of PHPMailer that is compatible with PHP 8. You can check the PHPMailer website or GitHub repository for the latest version.

Solution 2:

Use a different email library: If updating PHPMailer doesn’t work, consider using a different email library that is compatible with PHP 8. Some popular alternatives include SwiftMailer and Zend Mail.

Solution 3:

Check your code for compatibility issues: PHP 8 introduced several changes to the language that may affect your code. Make sure your code is compatible with PHP 8 and check for any deprecated functions or features that may be causing issues.

Solution 4: Enable Troubleshoot

Enable error reporting: PHP 8 has stricter error reporting than previous versions, so enabling error reporting may help you identify the issue. You can enable error reporting by adding the following lines to your code:

Solution 5: Modify the code as below in your phpmail function (Tried & Tested)

Use “\r\n" instead of “\n" for your implode. This will simplify the CRLF to work with Php8 and above.

<?php
$uploadDir = 'assets/attachments/';
$response = array('status' => 0, 'message' => 'Form submission failed, please try again.');
// If form is submitted
if (isset($_POST['name']) || isset($_POST['email']) || isset($_POST['file']) || isset($_POST['subject']) || isset($_POST['message'])) {
    // Get the submitted form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    // Check whether submitted data is not empty
    if (!empty($name) && !empty($email)) {
        // Validate email
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
            $response['message'] = 'Please enter a valid email.';
        } else {
            $fromemail = $email;
            $subject = "New Email Message With Attachment";
            $email_message = '<h2>Contact Request Submitted</h2>
                    <p><b>Name:</b> ' . $name . '</p>
                    <p><b>Email:</b> ' . $email . '</p>
                    <p><b>Subject:</b> ' . $subject . '</p>
                    <p><b>Message:</b><br/>' . $message . '</p>';
            $email_message.= "Please find the attachment<br /><br /><br />";
            $email_message.= "Mail Support By: <a href='https://ismartbits.com' target='_blank'>ismartbits.com";
            $semi_rand = md5(uniqid(time()));
            $headers = "From: " . $fromemail;
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
            $headers.= "\r\nMIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";
            if ($_FILES["file"]["name"] != "") {
                $strFilesName = $_FILES["file"]["name"];
                $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
                $email_message.= "This is a multi-part message in MIME format.\r\n\r\n" . "--{$mime_boundary}\r\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n" . $email_message.= "\r\n\r\n";
                $email_message.= "--{$mime_boundary}\r\n" . "Content-Type: application/octet-stream;\r\n" . " name=\"{$strFilesName}\"\r\n" .
                //"Content-Disposition: attachment;\r\n" .
                //" filename=\"{$fileatt_name}\"\r\n" .
                "Content-Transfer-Encoding: base64\r\n\r\n" . $strContent.= "\r\n\r\n" . "--{$mime_boundary}--\n";
            }
            $toemail = "<MailID@yourdomain.com";
            if (mail($toemail, $subject, $email_message, $headers)) {
                echo json_encode(array("status" => 200, "msg" => "File Sent Successfully."));
            } else {
                echo json_encode(array("status" => 500, "msg" => "Mail could not be sent."));
            }
        }
    } else {
        //  $response['message'] = 'Please fill all the mandatory fields (name and email).';
        echo json_encode(array("status" => 500, "msg" => "Please fill all the mandatory fields (name and email)."));
    }
}
?>

If none of the above solution works, feel free to contact us. We will fix the issue at a very nominal charges.

I SMART BITS

Make World Smarter

www.ismartbits.com

Leave a Reply

Your email address will not be published. Required fields are marked *