2011-11-29 14:58:04 +01:00
|
|
|
#!/usr/bin/env php
|
2011-05-05 08:09:42 +02:00
|
|
|
<?php
|
|
|
|
|
2013-12-12 19:59:28 +01:00
|
|
|
// NOTE: This script is very oldschool and takes the environment as an argument.
|
|
|
|
// Some day, we could take a shot at cleaning this up.
|
2011-05-06 00:11:36 +02:00
|
|
|
if ($argc > 1) {
|
2013-12-12 19:59:28 +01:00
|
|
|
foreach (array_slice($argv, 1) as $arg) {
|
|
|
|
if (!preg_match('/^-/', $arg)) {
|
|
|
|
$_SERVER['PHABRICATOR_ENV'] = $arg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-05-06 00:11:36 +02:00
|
|
|
}
|
|
|
|
|
2011-05-05 08:09:42 +02:00
|
|
|
$root = dirname(dirname(dirname(__FILE__)));
|
|
|
|
require_once $root.'/scripts/__init_script__.php';
|
Update mimemailparser from May 2011 version to 8.0.4
Summary:
Bump to version 8.0.4 from 2024-09-11 per https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases before this ancient code copy falls apart.
`scripts/mail/mail_handler.php` (used for incoming (!) mail) is the only consumer.
Closes T15940
Test Plan: Feed `mail_handler.php` with various test emails (formats: plain text, HTML, multipart; encodings: UTF-8, ASCII, ISO-8859-something) by manually replacing `php://stdin` with corresponding text files and adding some `phlog`s for output checking as I don't have mail server glue handy. Get only expected errors for broken emails.
Reviewers: O1 Blessed Committers, 20after4
Reviewed By: O1 Blessed Committers, 20after4
Subscribers: 20after4, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno
Maniphest Tasks: T15940
Differential Revision: https://we.phorge.it/D25829
2024-10-22 13:51:09 +02:00
|
|
|
require_once $root.'/externals/mimemailparser/Contracts/CharsetManager.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/Contracts/Middleware.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/Parser.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/Charset.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/Attachment.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/Exception.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/Middleware.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/MiddlewareStack.php';
|
|
|
|
require_once $root.'/externals/mimemailparser/MimePart.php';
|
2011-05-05 08:09:42 +02:00
|
|
|
|
2013-12-12 19:59:28 +01:00
|
|
|
$args = new PhutilArgumentParser($argv);
|
|
|
|
$args->parseStandardArguments();
|
|
|
|
$args->parse(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'process-duplicates',
|
|
|
|
'help' => pht(
|
|
|
|
"Process this message, even if it's a duplicate of another message. ".
|
|
|
|
"This is mostly useful when debugging issues with mail routing."),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'env',
|
|
|
|
'wildcard' => true,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
Update mimemailparser from May 2011 version to 8.0.4
Summary:
Bump to version 8.0.4 from 2024-09-11 per https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases before this ancient code copy falls apart.
`scripts/mail/mail_handler.php` (used for incoming (!) mail) is the only consumer.
Closes T15940
Test Plan: Feed `mail_handler.php` with various test emails (formats: plain text, HTML, multipart; encodings: UTF-8, ASCII, ISO-8859-something) by manually replacing `php://stdin` with corresponding text files and adding some `phlog`s for output checking as I don't have mail server glue handy. Get only expected errors for broken emails.
Reviewers: O1 Blessed Committers, 20after4
Reviewed By: O1 Blessed Committers, 20after4
Subscribers: 20after4, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno
Maniphest Tasks: T15940
Differential Revision: https://we.phorge.it/D25829
2024-10-22 13:51:09 +02:00
|
|
|
if (!extension_loaded('mailparse')) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'PhpMimeMailParser for handling incoming mail requires the PHP '.
|
|
|
|
'mailparse extension to be installed.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$parser = new \PhpMimeMailParser\Parser();
|
2011-05-05 08:09:42 +02:00
|
|
|
$parser->setText(file_get_contents('php://stdin'));
|
|
|
|
|
Transcode the HTML part of incoming email into UTF-8 as well
Summary:
D1093 did this for just the text/plain part of incoming
email. Most text/html parts choose to either use entity encoding
//or// are already UTF-8, thus obviating the need to transcode the
HTML part. However, this is not always the case, and leads to dropped
messages, by way of:
```
EXCEPTION: (Exception) Failed to JSON encode value (#5: Malformed UTF-8 characters, possibly incorrectly encoded): Dictionary value at key "html" is not valid UTF8, and cannot be JSON encoded: [snip HTML part of message content]```
Generalize the charset transcoding to not apply to just the text/plain part, but
both text/plain and text/html parts.
Test Plan:
Fed in a Windows-1252-encoded text/html part with 0x92
bytes in it; verified that $content only contained valid UTF-8 after
this change.
Reviewers: #blessed_reviewers, epriestley
Reviewed By: #blessed_reviewers, epriestley
Subscribers: Korvin, epriestley
Differential Revision: https://secure.phabricator.com/D18776
2017-11-16 09:43:35 +01:00
|
|
|
$content = array();
|
|
|
|
foreach (array('text', 'html') as $part) {
|
|
|
|
$part_body = $parser->getMessageBody($part);
|
|
|
|
$content[$part] = $part_body;
|
2011-12-02 17:47:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$headers = $parser->getHeaders();
|
2024-04-01 12:32:50 +02:00
|
|
|
if (array_key_exists('subject', $headers)) {
|
|
|
|
$headers['subject'] = phutil_decode_mime_header($headers['subject']);
|
|
|
|
}
|
2019-01-30 23:44:40 +01:00
|
|
|
$headers['from'] = phutil_decode_mime_header($headers['from']);
|
2011-12-02 17:47:45 +01:00
|
|
|
|
2013-12-12 19:59:28 +01:00
|
|
|
if ($args->getArg('process-duplicates')) {
|
|
|
|
$headers['message-id'] = Filesystem::readRandomCharacters(64);
|
|
|
|
}
|
|
|
|
|
2011-05-05 08:09:42 +02:00
|
|
|
$received = new PhabricatorMetaMTAReceivedMail();
|
2011-12-02 17:47:45 +01:00
|
|
|
$received->setHeaders($headers);
|
Transcode the HTML part of incoming email into UTF-8 as well
Summary:
D1093 did this for just the text/plain part of incoming
email. Most text/html parts choose to either use entity encoding
//or// are already UTF-8, thus obviating the need to transcode the
HTML part. However, this is not always the case, and leads to dropped
messages, by way of:
```
EXCEPTION: (Exception) Failed to JSON encode value (#5: Malformed UTF-8 characters, possibly incorrectly encoded): Dictionary value at key "html" is not valid UTF8, and cannot be JSON encoded: [snip HTML part of message content]```
Generalize the charset transcoding to not apply to just the text/plain part, but
both text/plain and text/html parts.
Test Plan:
Fed in a Windows-1252-encoded text/html part with 0x92
bytes in it; verified that $content only contained valid UTF-8 after
this change.
Reviewers: #blessed_reviewers, epriestley
Reviewed By: #blessed_reviewers, epriestley
Subscribers: Korvin, epriestley
Differential Revision: https://secure.phabricator.com/D18776
2017-11-16 09:43:35 +01:00
|
|
|
$received->setBodies($content);
|
2011-05-05 08:09:42 +02:00
|
|
|
|
|
|
|
$attachments = array();
|
2011-05-29 12:19:06 +02:00
|
|
|
foreach ($parser->getAttachments() as $attachment) {
|
2011-06-13 06:09:55 +02:00
|
|
|
if (preg_match('@text/(plain|html)@', $attachment->getContentType()) &&
|
|
|
|
$attachment->getContentDisposition() == 'inline') {
|
|
|
|
// If this is an "inline" attachment with some sort of text content-type,
|
|
|
|
// do not treat it as a file for attachment. MimeMailParser already picked
|
|
|
|
// it up in the getMessageBody() call above. We still want to treat 'inline'
|
|
|
|
// attachments with other content types (e.g., images) as attachments.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-05-05 08:09:42 +02:00
|
|
|
$file = PhabricatorFile::newFromFileData(
|
|
|
|
$attachment->getContent(),
|
|
|
|
array(
|
|
|
|
'name' => $attachment->getFilename(),
|
2015-04-02 22:41:39 +02:00
|
|
|
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
|
2011-05-05 08:09:42 +02:00
|
|
|
));
|
|
|
|
$attachments[] = $file->getPHID();
|
|
|
|
}
|
|
|
|
|
2011-10-28 17:05:27 +02:00
|
|
|
try {
|
|
|
|
$received->setAttachments($attachments);
|
|
|
|
$received->save();
|
|
|
|
$received->processReceivedMail();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$received
|
2015-05-22 09:27:56 +02:00
|
|
|
->setMessage(pht('EXCEPTION: %s', $e->getMessage()))
|
2011-10-28 17:05:27 +02:00
|
|
|
->save();
|
2013-12-12 19:59:28 +01:00
|
|
|
|
|
|
|
throw $e;
|
2011-10-28 17:05:27 +02:00
|
|
|
}
|