PHP Pear Mail发送纯文本而不是HTML格式

时间:2016-09-01 11:03:42

标签: php email pear-mail

梨邮件不会发送HTML格式文本而是发送显示干净html代码的纯文本。我已经添加了"Content-type: text/html\r\n";但仍然以纯文本形式发送。

此外,我如何为特定电子邮件提供SMTP身份验证,因为这是垃圾邮件。

有人可以解决这个问题吗?

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

?>

3 个答案:

答案 0 :(得分:1)

您将html内容设置为纯文本内容:

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

您需要setHTMLBody()(或两者......):

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

答案 1 :(得分:0)

你必须修改给定blove的标题Content-type

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

答案 2 :(得分:0)

我添加了$headers['Content-Type'] = 'text/html; charset=UTF-8';。这样就解决了。