php - 在电子邮件正文中显示 html
问题描述
我正在编写用于发送电子邮件并将电子邮件存储在已发送文件夹中的代码。我在输出中显示 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)
我使用上面的每个代码在输出中得到这个:
剥离标签:
Hey Ron,I am doing well thank you. Yes I am interested to meet you this weekend.See ya there then.Chris
实体
<p style="color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;">Hey Ron,</p><p style="color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;">I am doing well thank you.&nbsp;<br><br>Yes I am interested to meet you this weekend.<br><br>See ya there then.<br><br>Chris</p>
htmlspecialchars
<p style="color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;">Hey Ron,</p><p style="color: rgb(70, 70, 70); font-family: Roboto, RobotoDraft, Helvetica, Arial, sans-serif; font-size: 14px;">I am doing well thank you.&nbsp;<br><br>Yes I am interested to meet you this weekend.<br><br>See ya there then.<br><br>Chris</p>
这是完整的代码:
<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 标签时在输出中显示 hml。
您能否向我展示一个示例,当我打开电子邮件而不显示标签时,如何在输出中显示 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: ".$from."r\n". "To: ".$to."\r\n". "Subject: ".$subject. "\r\n". "日期: ".date("r", strtotime("now"))."\r\n". "\r\n". $mail. "\r\n" ) ;
解决方案
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);
}
}
}
?>
推荐阅读
- python - 在 Pytorch 中使用加载文本数据时出现断管错误
- windows - 通过命令提示符通过 bash 调用 ffmpeg
- react-router - 页面刷新后路由查询丢失 [nextjs]
- visual-studio-code - VS Code 开发容器:无法下载 VS Code Server
- c++ - 检查一个类是否有基础
- sql - MS Access:我如何找到最近日期只有“工作”或“恢复”状态的员工?
- javascript - 每个浏览器页面多个网络摄像头
- android - BottomNavigationView 切换标签页显示旧片段
- node.js - Mongoose CastError:模型“Task”的路径“_listId”的值“:listId”转换为ObjectId失败
- swift - 无法同时满足约束。使用 UICollectionViewFlowLayout 自适应单元格高度