在电子邮件正文中显示html

时间:2019-06-10 23:21:28

标签: php jquery imap

我正在处理我的代码,以发送电子邮件并将电子邮件存储在已发送的文件夹中。我在输出中显示html时遇到问题,因为它将像这样显示html标签:

Hey Ron!<div><br></div><div>How are you?</div><div><br></div><div>I hope you are well.</div><div><br></div><div>Speak soon,</div><div>Chris</div>

应该是:

Hey Ron!

How are you?

I hope you are well.

Speak soon,
Chris

我已经尝试过了:

strip_tags($message)
htmlentities($message)
htmlspecialchars($message)

我使用上面的每个代码在输出中获取此信息:

strip_tags:

Hey Ron,I am doing well thank you.&nbsp;Yes I am interested to meet you this weekend.See ya there then.Chris

html实体

&lt;p style=&quot;color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;Hey Ron,&lt;/p&gt;&lt;p style=&quot;color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;I am doing well thank you.&amp;nbsp;&lt;br&gt;&lt;br&gt;Yes I am interested to meet you this weekend.&lt;br&gt;&lt;br&gt;See ya there then.&lt;br&gt;&lt;br&gt;Chris&lt;/p&gt;

html特殊字符

&lt;p style=&quot;color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;Hey Ron,&lt;/p&gt;&lt;p style=&quot;color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;&quot;&gt;I am doing well thank you.&amp;nbsp;&lt;br&gt;&lt;br&gt;Yes I am interested to meet you this weekend.&lt;br&gt;&lt;br&gt;See ya there then.&lt;br&gt;&lt;br&gt;Chris&lt;/p&gt;

这是完整的代码:

<span id="avater_email" class="avater_email">chris@domain.com</span>
<span id="avater_name" class="avater_email">chris</span>
<span id="emailsubject"><b>Hey  </b></span>
<span id="send_to" email="chris@domain.com" data-email-address="chris@domain.com">chris<chris@domain.com></chris@domain.com></span>

<div id="quickreply" class="reply-message" hidefocus="false" aria-label="Message Body" g_editable="true" role="textbox" aria-hidden="true" aria-multiline="true" contenteditable="true" tabindex="1" style="direction: ltr;min-height: 135px;width: 840px; padding-left: 25px; font-size: 13px;" itacorner="6,7:1,1,0,0">Hey Ron!<div><br></div><div>How are you?</div><div><br></div><div>I hope you are well.</div><div><br></div><div>Speak soon,</div><div>Chris</div></div>

var username = 'myusername';
var password = 'mypassword';
var mailserver = '{imap.domain.com:993/imap/ssl/novalidate-cert}';

$(document).on('click','#send_button', function(e) {
    var name = $("#send_to").text();
    var email = $("#send_to").attr('data-email-address');
    var from_name = $("#avater_name").text();
    var from_email = $('#avater_email').text();
    var emailbody = $("#quickreply").html();
    var subject = $("#emailsubject").text();

    if($("#replymessage").css("display") == "block")
    {
        subject = 'Re: ' + $("#emailsubject").text();
    }


    $.ajax({
        url: 'sendMail.php',
        type: 'POST',

        data : {
            username: username,
            password: password,
            mailserver: mailserver,
            to_name: name,
            send_to: email,
            from: from_name,
            from_email: from_email,
            emailsubject: subject,
            emailbody: emailbody
        },

        success: function(result)
        {
            alert(result);
            //$(this).attr("disabled", false);
        }
    });
});


sendMail.php:

<?php

require_once "Mail.php";
require_once "Mail/mime.php";


if (isset($_POST['send_to']))
{
    $toArrs = explode(",", $_POST['send_to']);
    $toArr = array_unique($toArrs);

    foreach($toArr as $to) 
    {
        $from = $_POST['from_email'];
        $to = $_POST['send_to'];
        $subject = $_POST['emailsubject'];
        $message = $_POST['emailbody'];
        $host = "smtp.domain.com";
        $port = "587";
        $username = $_POST['username'];
        $password = $_POST['password'];
        $mailserver = $_POST['mailserver'];
        //$mailserver = $mailserver .'.Sent';

        $headers = array ('From' => $from, 
            'To' => $to, 'Subject' => $subject,
            'Reply-To' => $from,
            'MIME-Version' => '1.0',
            'Content-Type'  => 'Content-Type: text/plain; charset=UTF-8',
            'Received' => 'from smtp.domain.com',
            'Date'  => date("r"),
            'Message-ID' => sprintf("<%s.%s@%s>",
                base_convert(microtime(), 10, 36),
                base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
                'domain.com'));

        $params = array ('host' => $host,
            'port' => $port,
            'auth' => 'PLAIN', // Note 1
            'socket_options' => array('ssl' => array('verify_peer_name' => false, 'verify_peer' => false)), // Note 2
            'username' => $username,
            'password' => $password);

        $mime_params = array(
            'text_encoding' => '7bit',
            'text_charset'  => 'UTF-8',
            'html_charset'  => 'UTF-8',
            'head_charset'  => 'UTF-8'
        );

        $crlf = "\r\n";
        $mime = new Mail_mime(array('eol' => $crlf));
        $body = $message;
        $mime->setTXTBody($body);
        $mime->setHTMLBody($body);
        $body = $mime->get($mime_params);
        $headers = $mime->headers($headers);

        $smtp = Mail::factory ('smtp', $params);
        $mail = $smtp->send($to, $headers, $body);


        if (PEAR::isError($mail)) 
        {
            echo("<p>" . $mail->getMessage() . "</p>");
        } 
        else 
        {
            echo("<p>Email has been sent!</p>");

            $mailbox = imap_open($mailserver, $username, $password);
            imap_append($mailbox, $mailserver.'.Sent',
                "From: ".$from."r\n".
                "To: ".$to."\r\n".
                "Subject: ".$subject."\r\n".
                "Date: ".date("r", strtotime("now"))."\r\n".
                "\r\n".
                $body.
                "\r\n"
                );

            // close mail connection.
            imap_close($mailbox);
        }
    }
}
?>

我想要实现的是当我发送电子邮件并将电子邮件存储在已发送文件夹中时,我想在打开电子邮件而不显示html标签时在输出中显示html。

您能给我一个例子,当我打开电子邮件而不显示标签时如何在输出中显示html吗?

谢谢。

编辑:这是更新代码:

<?php
require_once "Mail.php";
require_once('Mail/IMAPv2.php');
require_once "Mail/mime.php";

if (isset($_POST['send_to']))
{
    $from = "Chris <chris@domain.com>";
    $to = $_POST['send_to'];
    $subject = $_POST['emailsubject'];
    $message = $_POST['emailbody'];
    $smtp_hostname = "smtp.domain.com";
    $port = "587";
    $username = $_POST['username'];
    $password = $_POST['password'];
    $mailserver = $_POST['mailserver'];

    $headers = array ('From' => $from, 
        'To' => $to, 'Subject' => $subject,
        'Reply-To' => $from,
        'Content-Type'  => 'Content-Type: text/plain; charset=UTF-8',
        'MIME-Version' => '1.0',
        'Received' => 'from smtp.domain.com',
        'Date'  => date("r"),
        'Message-ID' => sprintf("<%s.%s@%s>",
            base_convert(microtime(), 10, 36),
            base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
            'domain.com'));

    $params = array ('host' => $smtp_hostname,
        'port' => $port,
        'auth' => 'PLAIN', // Note 1
        'socket_options' => array('ssl' => array('verify_peer_name' => false, 'verify_peer' => false)), // Note 2
            'username' => $username,
            'password' => $password);

    $mime_params = array(
        'text_encoding' => '7bit',
        'text_charset'  => 'UTF-8',
        'html_charset'  => 'UTF-8',
        'head_charset'  => 'UTF-8'
    );

    $crlf = "\r\n";
    $mime = new Mail_mime(array('eol' => $crlf));
    $body = $message;

    $mime->setTXTBody($body);
    $mime->setHTMLBody($body);
    $body = $mime->get($mime_params);
    $headers = $mime->headers($headers);
    $smtp = Mail::factory ('smtp', $params);
    $mail = $smtp->send($to, $headers, $body);


    if (PEAR::isError($mail)) 
    {
        echo("<p>" . $mail->getMessage() . "</p>");
    } 
    else 
    {
        echo("<p>Email has been sent!</p>");

        $mailbox = imap_open($mailserver, $username, $password);
        imap_append($mailbox, $mailserver.'.Sent',
            "From: ".$from."r\n".
            "To: ".$to."\r\n".
            "Subject: ".$subject."\r\n".
            "Date: ".date("r", strtotime("now"))."\r\n".
            "\r\n".
            $message.
            "\r\n"
        );

        // close mail connection.
        imap_close($mailbox);
    }
}
?>

我已经尝试过:

imap_append($mailbox, $mailserver.'.Sent',
    "From: ".$from."r\n".
    "To: ".$to."\r\n".
    "Subject: ".$subject."\r\n".
    "Date: ".date("r", strtotime("now"))."\r\n".
    "\r\n".
    $body.
    "\r\n"
);

我也尝试过此操作:

imap_append($ mailbox,$ mailserver。'。Sent',         “发件人:”。$ from。“ r \ n”。         “收件人:”。$ to。“ \ r \ n”。         “主题:”。$ subject。“ \ r \ n”。         “ Date:” .date(“ r”,strtotime(“ now”))。“ \ r \ n”。         “ \ r \ n”。         $ mail。         “ \ r \ n”     );

1 个答案:

答案 0 :(得分:1)

PHP中没有功能可以满足您的要求。

您需要创建自己的PHP函数。首先将<br>替换为新行(如您的示例,我已经替换了2个新行),然后删除其他HTML标签。

function br2nl($str) {
    return preg_replace("~<br[ /]*>~", "\r\n\r\n", $str);
}

$str = br2nl('Hey Ron!<div><br></div><div>How are you?</div><div><br></div><div>I hope you are well.</div><div><br></div><div>Speak soon,</div><div>Chris</div>');
$str = strip_tags($str);

echo '<pre>';
echo $str;
echo '</pre>';


---- result
Hey Ron!

How are you?

I hope you are well.

Speak soon,Chris



完整代码sendMail.php

<?php

require_once "Mail.php";
require_once "Mail/mime.php";

function br2nl($str) {
    return preg_replace("~<br[ /]*>~", "\r\n\r\n", $str);
}

if (isset($_POST['send_to']))
{
    $toArrs = explode(",", $_POST['send_to']);
    $toArr = array_unique($toArrs);

    foreach($toArr as $to) 
    {
        $from = $_POST['from_email'];
        $to = $_POST['send_to'];
        $subject = $_POST['emailsubject'];
        $message = $_POST['emailbody'];
        $host = "smtp.domain.com";
        $port = "587";
        $username = $_POST['username'];
        $password = $_POST['password'];
        $mailserver = $_POST['mailserver'];
        //$mailserver = $mailserver .'.Sent';

        $headers = array ('From' => $from, 
            'To' => $to, 'Subject' => $subject,
            'Reply-To' => $from,
            'MIME-Version' => '1.0',
            'Content-Type'  => 'Content-Type: text/plain; charset=UTF-8',
            'Received' => 'from smtp.domain.com',
            'Date'  => date("r"),
            'Message-ID' => sprintf("<%s.%s@%s>",
                base_convert(microtime(), 10, 36),
                base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
                'domain.com'));

        $params = array ('host' => $host,
            'port' => $port,
            'auth' => 'PLAIN', // Note 1
            'socket_options' => array('ssl' => array('verify_peer_name' => false, 'verify_peer' => false)), // Note 2
            'username' => $username,
            'password' => $password);

        $mime_params = array(
            'text_encoding' => '7bit',
            'text_charset'  => 'UTF-8',
            'html_charset'  => 'UTF-8',
            'head_charset'  => 'UTF-8'
        );

        $crlf = "\r\n";
        $mime = new Mail_mime(array('eol' => $crlf));

        $body = br2nl($message);
        $body = strip_tags($body);
//      ^^^^^^^^ edited noHTML message 
        $mime->setTXTBody($body);
        $mime->setHTMLBody($body);
        $body = $mime->get($mime_params);
        $headers = $mime->headers($headers);

        $smtp = Mail::factory ('smtp', $params);
        $mail = $smtp->send($to, $headers, $body);


        if (PEAR::isError($mail)) 
        {
            echo("<p>" . $mail->getMessage() . "</p>");
        } 
        else 
        {
            echo("<p>Email has been sent!</p>");

            $mailbox = imap_open($mailserver, $username, $password);
            imap_append($mailbox, $mailserver.'.Sent',
                "From: ".$from."r\n".
                "To: ".$to."\r\n".
                "Subject: ".$subject."\r\n".
                "Date: ".date("r", strtotime("now"))."\r\n".
                "\r\n".
                $message.
//              ^^^^^^^^ original HTML message
                "\r\n"
                );

            // close mail connection.
            imap_close($mailbox);
        }
    }
}
?>