Last active
December 18, 2015 17:50
-
-
Save actualys/5821756 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This script allows to redirect mail sent via the php mail function | |
* to a dedicated mailbox | |
* | |
* @date 2013-06-20 | |
* @author Sébastien MALOT / Actualys | |
* | |
* ---------------------------------- | |
* | |
* In PHP (apache et cli) | |
* | |
* sendmail_path = "php /path_to/sendmail_local.php | sendmail -t -i" | |
* | |
* ---------------------------------- | |
* | |
* In Apache/VirtualHost | |
* | |
* <VirtualHost *:80 > | |
* ... | |
* php_admin_value sendmail_path "php /path_to/sendmail_local.php | sendmail -t -i" | |
* ... | |
* </VirtualHost> | |
* | |
*/ | |
$to = '[email protected]'; | |
$content = file_get_contents('php://stdin'); | |
// debug | |
//file_put_contents('/tmp/mail.log', $content, FILE_APPEND); | |
// EOL character detection | |
$pos = strpos($content, "\n"); | |
$separator = "\n"; | |
if (substr($content, $pos + 1, 1) == "\r") $separator .= "\r"; | |
if (substr($content, $pos - 1, 1) == "\r") $separator = "\r" . $separator; | |
// split between header and body | |
$parts = preg_split('/' . $separator . $separator . '/', $content); | |
$headers = array_shift($parts); | |
$body = implode($separator . $separator, $parts); | |
// headers extraction | |
$matches = array(); | |
preg_match_all('/(?P<type>.*?):\s*(?P<value>.*?\n(\s.*?\n)*)/', $headers . $separator, $matches); | |
$headers = array(); | |
$typesToStrip = array('to', 'cc', 'bcc'); | |
foreach ($matches['type'] as $key => $type) { | |
$value = rtrim(preg_replace('/' . preg_quote($separator) . '\s+/m', ' ', $matches['value'][$key])); | |
if (in_array(strtolower($type), $typesToStrip)) { | |
$headers['X-Original-' . $type] = $value; | |
} else { | |
$headers[$type] = $value; | |
} | |
} | |
// header reconstruction | |
$headers['To'] = $to; | |
$bloc = array(); | |
foreach ($headers as $type => $value) { | |
$bloc[] = $type . ': ' . $value; | |
} | |
// output for sendmail handling | |
echo implode($separator, $bloc) . $separator . $separator . $body; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment