Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/scripts/mail/mail_handler.php
12241 views
1
#!/usr/bin/env php
2
<?php
3
4
// NOTE: This script is very oldschool and takes the environment as an argument.
5
// Some day, we could take a shot at cleaning this up.
6
if ($argc > 1) {
7
foreach (array_slice($argv, 1) as $arg) {
8
if (!preg_match('/^-/', $arg)) {
9
$_SERVER['PHABRICATOR_ENV'] = $arg;
10
break;
11
}
12
}
13
}
14
15
$root = dirname(dirname(dirname(__FILE__)));
16
require_once $root.'/scripts/__init_script__.php';
17
require_once $root.'/externals/mimemailparser/MimeMailParser.class.php';
18
19
$args = new PhutilArgumentParser($argv);
20
$args->parseStandardArguments();
21
$args->parse(
22
array(
23
array(
24
'name' => 'process-duplicates',
25
'help' => pht(
26
"Process this message, even if it's a duplicate of another message. ".
27
"This is mostly useful when debugging issues with mail routing."),
28
),
29
array(
30
'name' => 'env',
31
'wildcard' => true,
32
),
33
));
34
35
$parser = new MimeMailParser();
36
$parser->setText(file_get_contents('php://stdin'));
37
38
$content = array();
39
foreach (array('text', 'html') as $part) {
40
$part_body = $parser->getMessageBody($part);
41
42
if (strlen($part_body) && !phutil_is_utf8($part_body)) {
43
$part_headers = $parser->getMessageBodyHeaders($part);
44
if (!is_array($part_headers)) {
45
$part_headers = array();
46
}
47
$content_type = idx($part_headers, 'content-type');
48
if (preg_match('/charset="(.*?)"/', $content_type, $matches) ||
49
preg_match('/charset=(\S+)/', $content_type, $matches)) {
50
$part_body = phutil_utf8_convert($part_body, 'UTF-8', $matches[1]);
51
}
52
}
53
54
$content[$part] = $part_body;
55
}
56
57
$headers = $parser->getHeaders();
58
$headers['subject'] = phutil_decode_mime_header($headers['subject']);
59
$headers['from'] = phutil_decode_mime_header($headers['from']);
60
61
if ($args->getArg('process-duplicates')) {
62
$headers['message-id'] = Filesystem::readRandomCharacters(64);
63
}
64
65
$received = new PhabricatorMetaMTAReceivedMail();
66
$received->setHeaders($headers);
67
$received->setBodies($content);
68
69
$attachments = array();
70
foreach ($parser->getAttachments() as $attachment) {
71
if (preg_match('@text/(plain|html)@', $attachment->getContentType()) &&
72
$attachment->getContentDisposition() == 'inline') {
73
// If this is an "inline" attachment with some sort of text content-type,
74
// do not treat it as a file for attachment. MimeMailParser already picked
75
// it up in the getMessageBody() call above. We still want to treat 'inline'
76
// attachments with other content types (e.g., images) as attachments.
77
continue;
78
}
79
80
$file = PhabricatorFile::newFromFileData(
81
$attachment->getContent(),
82
array(
83
'name' => $attachment->getFilename(),
84
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
85
));
86
$attachments[] = $file->getPHID();
87
}
88
89
try {
90
$received->setAttachments($attachments);
91
$received->save();
92
$received->processReceivedMail();
93
} catch (Exception $e) {
94
$received
95
->setMessage(pht('EXCEPTION: %s', $e->getMessage()))
96
->save();
97
98
throw $e;
99
}
100
101