Skip to content

Instantly share code, notes, and snippets.

@Drarok

Drarok/sendmailRedirect.php Secret

Created Mar 27, 2012
Embed
What would you like to do?
Patch up sendmail to force emails to go to you (useful in development).
echo 'Enter the path to sendmail [/usr/sbin/sendmail]: ';
if (! (bool) $sendmail = trim(fgets(STDIN))) {
$sendmail = '/usr/sbin/sendmail';
}
if (! is_writable($sendmail)) {
echo 'Cannot write to ', $sendmail, ', please run with sudo.', PHP_EOL;
exit(1);
}
echo 'Enter your email address, and press enter: ';
if (! (bool) $developerEmail = trim(fgets(STDIN))) {
echo 'You must specify an email address.', PHP_EOL;
exit(1);
}
$sendmailReal = $sendmail . '_real';
if (file_exists($sendmailReal)) {
echo $sendmailReal, ' already exists!', PHP_EOL;
exit(1);
}
// Move sendmail out of the way.
if (! rename($sendmail, $sendmailReal)) {
echo 'Failed to rename sendmail!', PHP_EOL;
exit(1);
}
$sendmailScript = <<<EOT
#!/bin/bash
formail -R cc X-original-cc \
-R to X-original-to \
-R bcc X-original-bcc \
-f -A"To: ${developerEmail}" \
| ${sendmailReal} -t -i $@
EOT;
if (! file_put_contents($sendmail, $sendmailScript)) {
echo 'Failed to write script!', PHP_EOL;
exit(1);
}
if (! chmod($sendmail, 0755)) {
echo 'Failed to make the script executable!', PHP_EOL;
exit(1);
}
echo 'All done!', PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment