EMailSmartCatchAllHandler
a phpmae:HTTPInvokableClass & phpmae:Class in Lukas Rosenstock
Public PHP Methods
-
__invoke($args)
No documentation available.
Source Code
<?php use Exception; use Psr\Container\ContainerInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Email; use Webmozart\Assert\Assert; /** * Implementation for coid://lukasrosenstock.net/EMailSmartCatchAllHandler */ class EMailSmartCatchAllHandler { const AIRTABLE_URL = 'appUj1Ldwzem2IL01/Aliases'; private $mailer; private $airtableApi; private $targetAddress; public function __construct(MailerInterface $mailer, ContainerInterface $container) { $this->mailer = $mailer; $this->airtableApi = $container->get('airtable'); $this->targetAddress = $container->get('targetadr'); } public function __invoke($args) { try { /*Assert::keyExists($args, 'envelope'); Assert::keyExists($args['envelope'], 'recipient'); Assert::keyExists($args['envelope']['recipient'], 'email'); $toEMail = $args['envelope']['recipient']['email']; */ Assert::keyExists($args, 'to'); $toEMail = strtolower($args['to'][0]['email']); $toEMailPrefix = substr($toEMail, 0, strpos($toEMail, "@")); // Assert required fields Assert::keyExists($args, 'from'); Assert::keyExists($args['from'], 'name'); Assert::keyExists($args['from'], 'email'); Assert::keyExists($args, 'subject'); // Check whether we have previously received email for this address $aliasLookupResult = json_decode($this->airtableApi->get(self::AIRTABLE_URL, [ 'query' => [ 'filterByFormula' => 'Account = \''.$toEMail.'\'' ] ])->getBody(), true); if (count($aliasLookupResult['records']) == 0) { // We have never encountered this email address before, // so we add it to the table $this->airtableApi->post(self::AIRTABLE_URL, [ 'json' => [ 'fields' => [ 'Account' => $toEMail, 'Status' => 'Active' ] ] ]); // The initial status is always 'Active' as we assume // this was a valid first-time signup $status = 'Active'; $isNew = true; } else { $status = $aliasLookupResult['records'][0]['fields']['Status']; $isNew = false; } switch ($status) { case "Active": // Forward this email $email = (new Email) ->from(isset($args['from']['name']) ? $args['from']['name'].' <noreply@email.lukasrosenstock.net>' : 'noreply@email.lukasrosenstock.net' ) ->replyTo($args['from']['email']) ->to($this->targetAddress) ->subject('['.$toEMailPrefix.'] '.$args['subject']); if (isset($args['html'])) { // HTML EMail Handling $suffix = ($isNew) ? "<p>This is the first email received for the alias <strong>".$toEMail."</strong>.</p>" : "<p>This email was received with the alias <strong>".$toEMail."</strong>.</p>"; $email->html($args['html']."<hr />".$suffix); } else { // Plain Text EMail Handling $suffix = ($isNew) ? "This is the first email received for the alias '".$toEMail."'." : "This email was received with the alias '".$toEMail."'.'"; $email->text(@$args['text']."\n----\n".$suffix); } $this->mailer->send($email); break; case "Blocked": // Ignore this email break; default: throw new Exception("Status not implemented!"); } } catch (Exception $e) { // Something went wrong, send me a bug report $email = (new Email) ->from('noreply@email.lukasrosenstock.net') ->to($this->targetAddress) ->subject("EMailSmartCatchAllHandler caught ".get_class($e)) ->text("Error Message: ".$e->getMessage()."\n\n".json_encode($args)); $this->mailer->send($email); } } }
Meta
- URI / COID
- coid://lukasrosenstock.net/EMailSmartCatchAllHandler content_copy
- Revision
- 19-1e3dfa6341f286eac63aa6057bb78713 content_copy
- Short ID
- llr:EMailSmartCatchAllHandler content_copy
- Reference URL
- https://coid.link/lukasrosenstock.net/EMailSmartCatchAllHandler content_copy
- Last updated
- 2023-07-06 13:22 (UTC)
- Created at
- 2021-12-15 14:33 (UTC)
RDF
Objects in this namespace
- Lukas Rosenstock
- AsciiArtFunctions
- EMailHelperFunctions
- EMailSmartCatchAllHandler
- ImprovEMailWebhookHandler
- TestProperty
- TestProperty2
- WebhookReceiver