-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
66 lines (56 loc) · 2.02 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
use Aws\S3\S3Client;
use Aws\Ses\SesClient;
use ZBateson\MailMimeParser\MailMimeParser;
require __DIR__ . '/vendor/autoload.php';
$s3Client = new S3Client(['region' => 'us-east-1', 'version' => 'latest']);
$sesClient = new SesClient(['region' => 'us-east-1', 'version' => '2010-12-01']);
$parser = new MailMimeParser();
function recapitalize(string $string)
{
return implode('', array_map(function(string $char) {
return random_int(0, 1) ? mb_strtoupper($char) : mb_strtolower($char);
}, mb_str_split($string)));
}
$dispatchS3Upload = function($event) use ($parser, $s3Client, $sesClient)
{
$rawEmail = $s3Client->getObject([
'Bucket' => $event['Records'][0]['s3']['bucket']['name'],
'Key' => $event['Records'][0]['s3']['object']['key']
])['Body'];
$email = $parser->parse($rawEmail, false);
$sesClient->sendEmail([
'Destination' => [
'ToAddresses' => [$from = $email->getHeaderValue('from')]
],
'Source' => $email->getHeader('to')->getAddresses()[0],
'Message' => [
'Subject' => [
'Charset' => 'UTF-8',
'Data' => 'RE: ' . ($subject = $email->getHeaderValue('subject'))
],
'Body' => [
'Text' => [
'Charset' => 'UTF-8',
'Data' => recapitalize($email->getTextContent())
]
]
]
]);
return 'Handled email from ' . $from . ' with subject ' . $subject;
};
$dispatchAPICall = fn ($event) => [
'statusCode' => isset($event['queryStringParameters']['message']) ? 200 : 400,
'body' => json_encode([
'message' => recapitalize(
$event['queryStringParameters']['message'] ??
'Please provide a message as "message" in your query string'
)
])
];
while (true) {
lambdaSubmitTask(
($event = lambdaGetTask())['requestId'],
call_user_func(isset($event['body']['version']) ? $dispatchAPICall : $dispatchS3Upload, $event['body'])
);
}