Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active July 3, 2024 01:48
Show Gist options
  • Save rudiedirkx/ae893b4041aadda70d42 to your computer and use it in GitHub Desktop.
Save rudiedirkx/ae893b4041aadda70d42 to your computer and use it in GitHub Desktop.
<?php
// sendmail_path="C:\XAMPP\php\php.exe C:\WWW\mailtodisk.php \"C:\XAMPP\mailoutput\__TIME__-__RAND__-__SUBJECT__.eml\""
// sendmail_path = "/usr/bin/php /var/www/mailtodisk.php \"/var/www/mailoutput/__TIME__-__RAND__-__SUBJECT__.eml\""
// file_put_contents(__DIR__ . '/mailtodisk.log', print_r($_SERVER, 1));
// Destination folder/filename is required
$destination = @$_SERVER['argv'][1];
if (!$destination) {
// file_put_contents(__DIR__ . '/mailtodisk.log', "\n\n\n\n\n\nmissing destination\n", FILE_APPEND);
exit(1);
}
// Create target folder
@mkdir(dirname($destination), 0777, true);
if (!is_dir(dirname($destination))) {
// file_put_contents(__DIR__ . '/mailtodisk.log', "\n\n\n\n\n\n\ninvalid destination\n", FILE_APPEND);
exit(2);
}
// Get entire mail content
$mail = file_get_contents('php://stdin');
// file_put_contents(__DIR__ . '/mailtodisk.log', "\n\n\n\n\n\n\n$mail\n", FILE_APPEND);
// Extract subject
$subject = 'NO SUBJECT';
if (preg_match('#Subject:\s+([^\r\n]+)#', $mail, $match)) {
$subject = imap_utf8($match[1]);
}
// Create filename with wildcards
list($usec) = explode(" ", microtime());
$destination = strtr($destination, array(
'__TIME__' => date('Y_m_d-H_i_s') . '-' . substr($usec, 2),
'__RAND__' => rand(),
'__SUBJECT__' => preg_replace('#_{2,}#', '_', trim(preg_replace('#[^a-z0-9-]#i', '_', $subject), '_')),
));
file_put_contents($destination, $mail);
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment