Skip to content

Instantly share code, notes, and snippets.

@cleverness
Created September 22, 2015 00:11
Show Gist options
  • Save cleverness/4f6ed65a69fc02a8e513 to your computer and use it in GitHub Desktop.
Save cleverness/4f6ed65a69fc02a8e513 to your computer and use it in GitHub Desktop.
<?php
/**
*
* The Sendmail script allows DesktopServer to route low-level email to a file in
* a self purging folder that cleans up itself after 48 hours.
*
* @package DesktopServer
* @since 3.8.0
*/
// Write mail to temp/mail folder
$mail_folder = "/Applications/MAMP/tmp/mail";
# create a filename for the eml file
$i = 1;
do {
$filename = $mail_folder . '/'. date('Y-m-d_H.m.s-') . '.eml';
}while( is_file($filename) === true );
# write the email contents to the file
$email_contents = fopen('php://stdin', 'r');
$fstat = fstat($email_contents);
file_put_contents($filename, $email_contents);
// Clean up files older than 2 days
if (file_exists($mail_folder)) {
foreach (new DirectoryIterator($mail_folder) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
if (time() - $fileInfo->getCTime() >= 2*24*60*60) {
unlink($fileInfo->getRealPath());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment