Skip to content

Instantly share code, notes, and snippets.

@x-magic
Last active September 16, 2023 23:09
Show Gist options
  • Save x-magic/3ba692c7ebc992e263d53c5054c548d2 to your computer and use it in GitHub Desktop.
Save x-magic/3ba692c7ebc992e263d53c5054c548d2 to your computer and use it in GitHub Desktop.
A script to imitate sendmail (to capture mails to be saved locally) for PHP using PHP
<?php
// To use this script, set
// sendmail_path = "/path/to/php /path/to/mailtodisk.php"
// in php.ini
// Set the mail redirect folder path
const MAILTODISK_ROOT = "/opt/mailtodisk/";
// Generate a date/time-based random file name
do {
$filename = 'mail_' . gmdate('Y-m-d-H-i-s') . '_' . uniqid() . '.txt';
} while(is_file($filename));
// Write the mail from stdin to file
$input = file_get_contents('php://stdin');
file_put_contents(MAILTODISK_ROOT . $filename, $input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment