phpemailpear-mail

PHP Pear Mail sending plain text instead of HTML format


Pear mail does not send HTML formated text instead sends plain text which shows clean html codes. I have already added "Content-type: text/html\r\n"; but still it send as plain text.

And also how can i give SMTP auth of the specific email because this goes to SPAM.

Could someone sort this issue?

<?php
include_once('Mail.php');

include_once('Mail_Mime/Mail/mime.php');

// Settings

$max_allowed_file_size = 20000; // size in KB
$allowed_extensions    = array(
    "jpg",
    "png",
    "jpeg"
);
$upload_folder         = './uploads/';
$your_email            = 'your@mail.com';
$errors                = '';

if (isset($_POST['submit'])) {

    // Get the uploaded file information

    $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);

    // get the file extension of the file

    $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);
    $size_of_uploaded_file = $_FILES["uploaded_file"]["size"] / 20000;

    // ------------Do Validations-------------

    if (empty($_POST['name']) || empty($_POST['email'])) {
        $errors .= "\n Name and Email are required fields. ";
    }

    if (IsInjected($visitor_email)) {
        $errors .= "\n Bad email value!";
    }

    if ($size_of_uploaded_file > $max_allowed_file_size) {
        $errors .= "\n Size of file should be less than $max_allowed_file_size";
    }

    // ------ Validate the file extension -----

    $allowed_ext = false;
    for ($i = 0; $i < sizeof($allowed_extensions); $i++) {
        if (strcasecmp($allowed_extensions[$i], $type_of_uploaded_file) == 0) {
            $allowed_ext = true;
        }
    }

    if (!$allowed_ext) {
        $errors .= "\n The file you are trying to upload is not supported file type. " . " Only the following file types are supported: " . implode(',', $allowed_extensions);
    }

    // send the email

    if (empty($errors)) {

        // copy the temp. uploaded file to uploads folder

        $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
        $tmp_path              = $_FILES["uploaded_file"]["tmp_name"];
        if (is_uploaded_file($tmp_path)) {
            if (!copy($tmp_path, $path_of_uploaded_file)) {
                $errors .= '\n error while copying the uploaded file';
            }
        }

        // send the email

        $name          = $_POST['name'];
        $visitor_email = $_POST['email'];
        $user_message  = $_POST['message'];
        $to            = $your_email;
        $subject       = "Request for an avatar";
        $from          = $your_email;
        $text          = "<html>
<body style='font-size: 120%;'>
  <table width='100%' border='0' cellspacing='0' cellpadding='0' style='background:#e1f5fe;'>
    <tr>
    </tr>
    </td>
    <tr>
      <td style='padding-left:5%;padding-right:5%'>
        <p>Hi $name,</p>
        <p style='padding-left:3%;'>messgae</p>
        <div style='background:#b3e5fc;'>
          <p style='padding-left:1%;'>Message : </p>
          <p style='padding-left:2%;padding-bottom:2px;'>$message</p>
        </div>
        <p>Regards,</p>
        <p style=''><b>Admin</b></p>
      </td>
    </tr>
    </table><br />
</body>
</html>";
        $headers       = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type: text/html\r\n";
        $message = new Mail_mime();
        $message->setTXTBody($text);
        $message->addAttachment($path_of_uploaded_file);
        $body         = $message->get();
        $extraheaders = array(
            "From" => $from,
            "Subject" => $subject,
            "Reply-To" => $visitor_email
        );
        $headers      = $message->headers($extraheaders);
        $mail         = Mail::factory("mail");
        $mail->send($visitor_email, $headers, $body);

        // redirect to 'thank-you page

        header('Location: thank-you.html');
    }
}

function IsInjected($str)
{
    $injections = array(
        '(\n+)',
        '(\r+)',
        '(\t+)',
        '(%0A+)',
        '(%0D+)',
        '(%08+)',
        '(%09+)'
    );
    $inject     = join('|', $injections);
    $inject     = "/$inject/i";
    if (preg_match($inject, $str)) {
        return true;
    } else {
        return false;
    }
}

?>

Solution

  • You are setting your html content as the plain text content:

     ...
     $message->setTXTBody($text);
     ...
    

    You need setHTMLBody() (or both...):

     ...
     $message->setHTMLBody($text);
     ...