Skip to content

Instantly share code, notes, and snippets.

@yookoala
Created October 28, 2021 03:10
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 yookoala/7d0ba22c3b145a9f2a9c66d9f6ad70bc to your computer and use it in GitHub Desktop.
Save yookoala/7d0ba22c3b145a9f2a9c66d9f6ad70bc to your computer and use it in GitHub Desktop.
A small script to be used as sendmail for debugging mailing functions in php.
<?php
/**
* @file
*
* Mock Sendmail
*
* A small script to be used as sendmail for debugging mailing functions in php.
*
* Usage:
* php -d sendmail_path='php ./sendmail.php' email_sending_script.php
*
* Or you'd need to specify in php.ini the 'sendmail_path' as 'php FULL_PATH_TO/sendmail.php'.
*
* Then in PHP, use mail() normally. For example:
* <code>
* mail('email_to@gmail.com', 'Subject', "<p>A very long message</p>\n<p>Thanks</p>", ['X-User-Agent' => 'hello_php']);
* </code>
*
* To specify the JSON capture file for the specific call, you can add the capture filename:
* <code>
* mail(
* 'email_to@gmail.com',
* 'Subject',
* "<p>A very long message</p>\n<p>Thanks</p>",
* ['X-User-Agent' => 'hello_php'],
* '--capture-file="' . __DIR__ . '/email-call-123.json"',
* );
* </code>
*
* @see https://www.php.net/manual/en/function.mail.php
* @see https://www.php.net/manual/en/mail.configuration.php
*/
// Allow script user to specify the path of the file to capture.
$options = getopt('', [
'capture-file::',
]) + [
'capture-file' => './mock-sendmail-capture.json',
];
$capture_filename = $options['capture-file'];
// Capture all the inputs.
$stdin = stream_get_contents(fopen('php://stdin', 'r'));
file_put_contents($capture_filename, json_encode([
'argv' => $argv,
'stdin' => $stdin,
], JSON_PRETTY_PRINT|JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_LINE_TERMINATORS));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment