php - 在 PHP 中正确显示 HTML 电子邮件
问题描述
我正在使用 PHP 开发一个网络电子邮件应用程序。目前,我在 Modal 上使用简单的imap_fetchbody
HTMLPurifier 显示每封邮件,但我注意到 HTML 电子邮件无法正确显示。
这是两封电子邮件:https ://i.imgur.com/EbjBoza.png https://imgur.com/a/wJhcVtq
基本上CSS有点搞砸了。而且我需要以应有的方式显示它们。
如果有人有想法,我很想尝试一下。
提前致谢!
PS:如果您需要任何内容,请发表评论。
代码(有点乱,抱歉!):
<?php
require('get-imap-details.php');
session_start();
//require('database-logging.php');
require('htmlpurifier/library/HTMLPurifier.auto.php');
date_default_timezone_set('UTC');
function DisplayEmails($email_address)
{
//Open Imap with given credentials.
//LogAllActions(); //Un-Command this line if you want to log all actions.
$imap = OpenImap();
$messages = imap_check($imap); //Load all messages that are in our catchall inbox
$messages_displayed = 0; //Count of emails already displayed
$max_messages_on_screen = 50; //Max messages you want to display at once, in this case 50
$email_in_blacklist = false;
$imap_result = imap_fetch_overview($imap,"1:{$messages->Nmsgs}",0);
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
foreach(array_reverse($imap_result) as $email)
{
//Check if the email address isn't blacklisted, if it is, don't display anything.
if(!BlacklistedEmailAddresses(explode('@',$email_address)[0]))
{
if($email->deleted !=1)
{
//Go through all emails, but display only emails for the current email address
if($email->to == $email_address && $messages_displayed <= $max_messages_on_screen)
{
$messages_displayed += 1;
$message_id = $email->msgno;
$header = imap_header($imap, $message_id);
//The part of message we'll display as preview, you can change how many characters you want to display below.
$characters_to_display = 50;
$overview = imap_fetch_overview($imap , $message_id , 0);
$message_body = imap_body($imap , $message_id );
$num = imap_num_msg($imap);
$message_preview = substr(imap_fetchbody($imap, $message_id, 1), 0, $characters_to_display);
$sender_address = $header->from[0]->mailbox."@".$header->from[0]->host;
$date = $header->date;
$mailtext = imap_fetchbody($imap , $message_id, 1 );
//$clean_html = $purifier->purify($mailtext);
$clean_html = $mailtext;
$_SESSION['subject'] = $email->subject;
$_SESSION['message'] = $clean_html;
//Display all email as separate table raw.
//You can change this by chaning the echo below onto your own one
?>
<tr>
<td scope="row"> <?php echo $sender_address; ?> </td><td> <?php echo $email->subject; ?></td><td><a href="#" style="color:#dc3545;" data-message=<?php //$message_body; ?> class="message" data-toggle="modal" data-target="#addModal<?php echo $message_id ?>"><p> view message</p></a></td> <td><a href="#" style="color:#dc3545;" data-toggle="modal" data-target="#forwardModal<?php echo $message_id; ?>">forward Mail</a></td>
</tr>
<!-- Message Modal -->
<div id="addModal<?php echo $message_id; ?>" class="modal fade" role="dialog">
<div class="modal-dialog" style="max-width:960px;">
<div class="modal-content" style="background-color:#131516;color: rgb(188, 182, 173);">
<div class="modal-header">
<h5> Details </h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body" id="coeg">
<p>From : <?php echo $sender_address; ?> </p>
<p>Date : <?php echo $header->date; ?> </p>
<p>Title : <?php echo $email->subject; ?> </p>
<hr>
<p>Message :</p>
<p id="message<?php echo $message_id; ?>"> </p>
</div>
</div>
</div>
</div>
<!-- Forwarding Modal -->
<div id="forwardModal<?php echo $message_id; ?>" class="modal fade" role="dialog">
<div class="modal-dialog" style="max-width:960px;">
<div class="modal-content" style="background-color:#131516;color: rgb(188, 182, 173);">
<div class="modal-header">
<h5>Forward Email</h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p>Forward an email to your personal account if you want to save that email.</p>
<hr>
<form action="/assets/php/forward.php" method="post">
<p>E-Mail address : <input type="email" name="forwarders_email"></p>
<p><input type="submit" class="btn btn-outline-secondary" value="Forward E-Mail"></p>
</form>
</div>
</div>
</div>
</div>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<!-- Bootstrap -->
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<!-- dataTables -->
<script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<!-- loading-overlay -->
<script src="//cdn.jsdelivr.net/npm/gasparesganga-jquery-loading-overlay@1.6.0/src/loadingoverlay.min.js"></script>
<script>
//Message Modal Box
$(function() {
// $('#inbox').DataTable();
$('body').on('click' , 'message', function() {
var message = $(this).data('message');
$('#coeg').html(message);
document.getElementById("message<?php echo $message_id ?>").innerHTML = <?php echo json_encode($clean_html); ?>;
});
});
var msg = <?php echo json_encode($clean_html); ?>;
document.getElementById("message<?php echo $message_id ?>").innerHTML = msg;
</script>
<?php
}
$email_timestamp = $email->udate;
//Check for every email if it's older than 7 days. If it is, delete.
//This is only used when the page is loaded by someone, if you want to automate this in cron jobs please check 'delete-old-emails.php'
//You can change delete timeframe in 'get-imap-details.php'
if(time()-GetDeleteTime() > $email_timestamp)
{
imap_delete($imap, $email->msgno); //Mark message with specific id as 'for delete'
}
}
}
else
{
$email_in_blacklist = true; //Email is in blacklist, don't display anything.
}
}
//Display this content if there are no messages matching this email address
if($messages_displayed == 0 && !$email_in_blacklist){
echo "<tr><td><div style=\"color:grey;\">
Nothing recieved yet. Click the Refresh Inbox button to reload your Email Inbox.
</div></td></tr>";
}
//Display if current email address is in blacklist, thus user can't access its content
else if($email_in_blacklist){
echo "<tr><th scope=\"row\">You can't view this email address</th></tr>";
}
//Delete all messages marked for delete, you can command the line below and keep them in your 'trash bin'
imap_expunge($imap);
imap_close($imap);
}
?>
解决方案
推荐阅读
- python - 熊猫/日期时间问题
- python - 使用 pandas 从另一个创建一个新的 df 并用条件填充它
- python - 我如何使用 selenium python 在 Ibstagram 上获得用户的关注
- javascript - 待办事项交叉列表和闭包
- javascript - 在 Node.js 中,我正在使用无法正常工作的 console.log
- machine-learning - BPE vs WordPiece Tokenization - 何时使用/哪个?
- python - 根据日期列表过滤数据框
- http - 当端口打开但被过滤时,nmap 查找支持的 SSL/TLS 版本
- ocaml - 合并模式匹配案例时的语法错误
- c# - 材质设计对话框内按钮中的 WPF 命令绑定