Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Last active March 4, 2020 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/4552ec702d9f605a7cf65481c1f608f7 to your computer and use it in GitHub Desktop.
Save Ciantic/4552ec702d9f605a7cf65481c1f608f7 to your computer and use it in GitHub Desktop.
Disable wp_mail, and save to file only
<?php
/**
* WP Mail for the development site, should not be used on production site
*
* Throw this in to your *wp_config.php*
*/
function wp_mail($to, $subject, $message, $headers = '', $attachments = array() ) {
$emails_dir = __DIR__ . "/.emails/";
if (!file_exists($emails_dir)) {
mkdir($emails_dir);
}
$email = json_encode(array(
"to" => $to,
"subject" => $subject,
"message" => $message,
"headers" => $headers,
"attachments_count" => count($attachments),
"trace" => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
), JSON_PRETTY_PRINT);
file_put_contents($emails_dir . date("Y-m-d\\THis") . "_" . uniqid() . ".json", $email);
return true;
}
<?php
// Place this to the .emails directory, should not be used on production site
if (isset($_GET["email"])) {
$filename = preg_replace("/[^a-zA-Z0-9-\._]+/", "", $_GET["email"]);
$json = json_decode(file_get_contents($filename), true);
if (!$json) {
return;
}
?>
<?php foreach ($json as $key=>$value): ?>
<article>
<h2><?php echo $key; ?></h2>
<?php
if (is_string($value) && strpos($value, "<") === 0) {
?>
<a href="#" id="show_html">Show HTML in iframe</a><br><br>
<script>
var EMAIL_HTML = <?php echo json_encode($value); ?>;
var LINK = document.querySelector("#show_html");
LINK.addEventListener("click", function (e) {
var iframe = document.createElement("IFRAME");
iframe.setAttribute("sandbox", "");
iframe.style.width = "100%";
iframe.style.height = "700px";
iframe.setAttribute("srcdoc", EMAIL_HTML);
LINK.parentNode.insertBefore(iframe, LINK);
LINK.parentNode.removeChild(LINK);
e.preventDefault();
})
</script>
<div>
<?php echo preg_replace(
"/\s+/", " ", strip_tags($value)
);?>
</div>
<?php
} else if (is_string($value)) {
echo nl2br(htmlentities($value));
} else {
echo '<pre>';
echo strip_tags(print_r($value, true));
echo '</pre>';
}
?>
</article>
<?php endforeach; ?>
<?php
return;
}
?>
<style>
body {
font-family: "San Francisco", "Segoe UI", sans-serif;
}
table {
width: 100%;
table-layout: fixed;
}
table th {
text-align: left;
}
table td {
padding: 0.6em 0.2em;
vertical-align: top;
}
.date {
width: 4em;
}
.time {
width: 4em;
}
.to {
width: 12em;
text-overflow: ellipsis;
overflow: hidden;
}
.debug {
width: 4em;
}
.attachments {
width: 4em;
}
.email {
width: 100%;
}
pre {
font-family: "Fira Code", monospace;
font-size: 12px;
}
iframe {
border: 1px solid black;
}
</style>
<table>
<tr>
<th class="date">Date</th>
<th class="time"></th>
<th class="to">To</th>
<th class="subject">Subject</th>
<th class="attachments">Attach</th>
<th class="debug"></th>
</tr>
<?php
$emails = array_reverse(glob("*.json"));
foreach ($emails as $email) {
$json = json_decode(file_get_contents($email), true);
if (!$json) {
continue;
}
$date = filemtime($email);
$to = $json["to"];
$message = $json["message"];
$subject = $json["subject"];
$uid = uniqid();
$trace = $json["trace"];
$headers = $json["headers"];
$attachments_count = $json["attachments_count"];
?>
<tr>
<td class="date"><?php echo date("j.n.Y", $date); ?></td>
<td class="time"><?php echo date("H:i:s", $date); ?></td>
<td class="to"><?php echo htmlentities($to); ?></td>
<td class="email">
<a id="emaillink<?php echo $uid; ?>" href="#"><?php echo htmlentities($subject); ?></a>
<script>
(function () {
let LINK = document.querySelector("#emaillink<?php echo $uid; ?>");
LINK.addEventListener("click", function (e) {
let EMAIL_SHOW = document.querySelector("#emailshow<?php echo $uid; ?>");
let EMAIL_HTML = document.querySelector("#email<?php echo $uid; ?>");
if (EMAIL_SHOW.style.display == "block") {
EMAIL_SHOW.style.display = "none";
} else {
EMAIL_SHOW.style.display = "block";
}
EMAIL_SHOW.setAttribute("srcdoc", EMAIL_HTML.textContent);
// LINK.parentNode.insertBefore(iframe, LINK);
// LINK.parentNode.removeChild(LINK);
e.preventDefault();
})
})();
</script>
</td>
<td class="attachments">
<?php echo htmlentities($attachments_count); ?>
</td>
<td class="debug">
<a id="debuglink<?php echo $uid; ?>" href="#">Debug</a>
<script>
(function () {
let LINK = document.querySelector("#debuglink<?php echo $uid; ?>");
LINK.addEventListener("click", function (e) {
let DEBUG_HTML = document.querySelector("#debug<?php echo $uid; ?>");
if (DEBUG_HTML.style.display == "block") {
DEBUG_HTML.style.display = "none";
} else {
DEBUG_HTML.style.display = "block";
}
e.preventDefault();
})
})();
</script>
</td>
</tr>
<tr>
<td colspan="6">
<div id="email<?php echo $uid; ?>" style="display: none">
<?php echo htmlentities($message); ?>
</div>
<iframe id="emailshow<?php echo $uid; ?>" style="display: none; width: 100%; height: 700px;"></iframe>
<pre id="debug<?php echo $uid; ?>" style="display: none; width: 100%; overflow: scroll;"><?php print_r($headers); ?><?php print_r($trace); ?></pre>
</td>
</tr>
<?php
} ?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment