繁体   English   中英

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

[英]PHP Pear Mail sending plain text instead of HTML format

Pear 邮件不发送 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;
    }
}

?>

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

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

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

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

您只需根据给定的 blove 修改标题Content-type

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

我添加了$headers['Content-Type'] = 'text/html; charset=UTF-8'; $headers['Content-Type'] = 'text/html; charset=UTF-8'; . 那修复了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM