1

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;
    }
}

?>
Synchro
  • 35,538
  • 15
  • 81
  • 104
Shamshid
  • 25
  • 4
  • did u try it with `$mail->IsHTML(true); ` – devpro Sep 01 '16 at 11:12
  • @devpro, that's PHPMailer syntax, which he's not using – Synchro Sep 01 '16 at 11:46
  • 1
    That code for checking file extensions is hideous - a simple `in_array` will check it in one line! Don't use `copy` for uploaded files, it's not safe; use `move_upload_file`, as the PHP docs describe. I think you should read the docs for the Pear Mail package - you should not need to do all this stuff manually. – Synchro Sep 01 '16 at 11:50
  • @Synchro why is it not safe to use copy? – Shamshid Sep 02 '16 at 14:10
  • `move_uploaded_file` will *only* move files that have actually been uploaded - PHP knows what files it processed in the request, and it validates them by using the hash it put in `$_FILES["uploaded_file"]["tmp_name"]`, ensuring that someone hasn't sneaked in something bad like `~/.ssh/id_rsa`, which `copy` alone does not do. Your usage is actually safe because you're checking `is_uploaded_file` first, but there's no particular reason to leave the uploaded files lying around in temp folders if you're going to use them, so you may as well use `move_uploaded_file` to do both things at once. – Synchro Sep 02 '16 at 15:20
  • [PHP docs on handling uploads](http://php.net/manual/en/features.file-upload.post-method.php) – Synchro Sep 02 '16 at 15:21

3 Answers3

1

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

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

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

 ...
 $message->setHTMLBody($text);
 ...
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • This fixed the issue :) could you also add the answer for SMTP authetication to add in same php? – Shamshid Sep 01 '16 at 11:56
  • @Shamshid Check this question: http://stackoverflow.com/questions/12166468/sending-html-message-through-pear-while-using-smtp-authentication-returns-an-err. If that doesn't answer it, you should ask a new question posting the details and errors of that specific section. – jeroen Sep 01 '16 at 12:05
0

you have to just modify the headers Content-type by given blove.

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
ajay panchal
  • 145
  • 1
  • 11
0

I added $headers['Content-Type'] = 'text/html; charset=UTF-8';. That fixed it.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103