Skip to content

Instantly share code, notes, and snippets.

@ytorbyk
Last active November 4, 2017 14:29
Show Gist options
  • Save ytorbyk/9868298c8671a493737a53184854f0b9 to your computer and use it in GitHub Desktop.
Save ytorbyk/9868298c8671a493737a53184854f0b9 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
# create a filename for the emlx file
date_default_timezone_set("Europe/Kiev");
list($ms, $time) = explode(' ', microtime());
$filename = dirname(__FILE__) . '/' . date('Y-m-d H.i.s.', $time) . substr($ms, 2, 3) . '.eml';
# write the email contents to the file
$email_contents = fopen('php://stdin', 'r');
$fstat = fstat($email_contents);
$sEmail = "";
while (!feof($email_contents)) {
$sEmail .= fread($email_contents, 1024);
}
fclose($email_contents);
$lines = explode("\n", $sEmail);
// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
// this is a header
$headers .= $lines[$i]."\n";
// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
$to = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
$filename = dirname(__FILE__) . '/mail/'
. date('Y.m.d-H.i\'s', $time)
. '[' . substr($ms, 2, 3) . '].eml';
file_put_contents($filename, $fstat['size']."\n");
file_put_contents($filename, $sEmail, FILE_APPEND);
# open up the emlx file (using Apple Mail)
#exec('open '.escapeshellarg($filename));
?>
@ytorbyk
Copy link
Author

ytorbyk commented Nov 4, 2017

Install

$ curl -L https://gist.github.com/9868298c8671a493737a53184854f0b9.git > /Users/<your-user>/Support/smtp_catcher.php
$ chmod +x /Users/<your-user>/Support/smtp_catcher.php

Configure php.ini

sendmail_path = /Users/<your-user>/Support/smtp_catcher.php

All emails will be stored in /Users/<your-user>/Support/mail folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment