forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageMask.php
83 lines (72 loc) · 2.02 KB
/
MessageMask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
* File: MessageMask.php
* Category: Mask
* Author: M.Goldenbaum
* Created: 14.03.19 20:49
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP\Support\Masks;
use Webklex\PHPIMAP\Attachment;
use Webklex\PHPIMAP\Message;
/**
* Class MessageMask
*
* @package Webklex\PHPIMAP\Support\Masks
*/
class MessageMask extends Mask {
/** @var Message $parent */
protected $parent;
/**
* Get the message html body
*
* @return null
*/
public function getHtmlBody(){
$bodies = $this->parent->getBodies();
if (!isset($bodies['html'])) {
return null;
}
return $bodies['html']->content;
}
/**
* Get the Message html body filtered by an optional callback
* @param callable|bool $callback
*
* @return string|null
*/
public function getCustomHTMLBody($callback = false) {
$body = $this->getHtmlBody();
if($body === null) return null;
if ($callback !== false) {
$aAttachment = $this->parent->getAttachments();
$aAttachment->each(function($oAttachment) use(&$body, $callback) {
/** @var Attachment $oAttachment */
if(is_callable($callback)) {
$body = $callback($body, $oAttachment);
}elseif(is_string($callback)) {
call_user_func($callback, [$body, $oAttachment]);
}
});
}
return $body;
}
/**
* Get the Message html body with embedded base64 images
* the resulting $body.
*
* @return string|null
*/
public function getHTMLBodyWithEmbeddedBase64Images() {
return $this->getCustomHTMLBody(function($body, $oAttachment){
/** @var \Webklex\PHPIMAP\Attachment $oAttachment */
if ($oAttachment->id && $oAttachment->getImgSrc() != null) {
$body = str_replace('cid:'.$oAttachment->id, $oAttachment->getImgSrc(), $body);
}
return $body;
});
}
}