-
Notifications
You must be signed in to change notification settings - Fork 69
feat: add email adapter DSN parsing #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,48 @@ $messaging = new FCM('YOUR_SERVICE_ACCOUNT_JSON'); | |
| $messaging->send($message); | ||
| ``` | ||
|
|
||
| You can also create email adapters from a DSN: | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| use Utopia\Messaging\Adapter\Email; | ||
|
|
||
| $smtp = Email::fromDsn('smtp://user:[email protected]:587?secure=tls&autotls=1'); | ||
| $resend = Email::fromDsn('resend://YOUR_API_KEY@default'); | ||
| $sendgrid = Email::fromDsn('sendgrid://YOUR_API_KEY@default'); | ||
| $mailgun = Email::fromDsn('mailgun://[email protected]?eu=0'); | ||
| ``` | ||
|
|
||
| Supported email DSN schemes are `smtp`, `smtps`, `resend`, `sendgrid`, and `mailgun`. | ||
|
|
||
| ## Multiple Adapters (Failover) | ||
|
|
||
| You can use multiple adapters with automatic failover. If one adapter throws an exception, the next one will be tried. | ||
|
|
||
| ```php | ||
| <?php | ||
|
|
||
| use \Utopia\Messaging\Messenger; | ||
| use \Utopia\Messaging\Messages\SMS; | ||
| use \Utopia\Messaging\Adapter\SMS\Twilio; | ||
| use \Utopia\Messaging\Adapter\SMS\Vonage; | ||
|
|
||
| $message = new SMS( | ||
| to: ['+12025550139'], | ||
| content: 'Hello World' | ||
| ); | ||
|
|
||
| $messenger = new Messenger([ | ||
| new Twilio('YOUR_ACCOUNT_SID', 'YOUR_AUTH_TOKEN'), | ||
| new Vonage('YOUR_API_KEY', 'YOUR_API_SECRET'), | ||
| ]); | ||
|
|
||
| $messenger->send($message); | ||
| ``` | ||
|
|
||
| The `Messenger` class accepts multiple adapters and tries them in order. It stops at the first successful response and only throws an exception if all adapters fail. | ||
|
|
||
| ## Adapters | ||
|
|
||
| > Want to implement any of the missing adapters or have an idea for another? We would love to hear from you! Please check out our [contribution guide](https://github.com/utopia-php/monorepo/blob/main/CONTRIBUTING.md) and [new adapter guide](./docs/add-new-adapter.md) for more information. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,131 @@ | ||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| namespace Utopia\Messaging; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class Messenger | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| private array $adapters; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function __construct(Adapter|array $adapters) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if ($adapters instanceof Adapter) { | ||||||||||||||||||||||||||||||||||||||
| $adapters = [$adapters]; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (empty($adapters)) { | ||||||||||||||||||||||||||||||||||||||
| throw new \InvalidArgumentException('At least one adapter must be provided.'); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| foreach ($adapters as $index => $adapter) { | ||||||||||||||||||||||||||||||||||||||
| if (! $adapter instanceof Adapter) { | ||||||||||||||||||||||||||||||||||||||
| throw new \InvalidArgumentException( | ||||||||||||||||||||||||||||||||||||||
| 'All elements must be instances of Adapter, but element ' | ||||||||||||||||||||||||||||||||||||||
| .$index | ||||||||||||||||||||||||||||||||||||||
| .' is ' | ||||||||||||||||||||||||||||||||||||||
| .\get_debug_type($adapter) | ||||||||||||||||||||||||||||||||||||||
| .'.' | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $this->validateAdapters($adapters); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $this->adapters = $adapters; | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function send(Message $message): array | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| $errors = []; | ||||||||||||||||||||||||||||||||||||||
| $messageType = $this->adapters[0]->getMessageType(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (! \is_a($message, $messageType)) { | ||||||||||||||||||||||||||||||||||||||
| throw new \Exception( | ||||||||||||||||||||||||||||||||||||||
| 'Invalid message type. Expected "' | ||||||||||||||||||||||||||||||||||||||
| .$messageType | ||||||||||||||||||||||||||||||||||||||
| .'", got "' | ||||||||||||||||||||||||||||||||||||||
| .\get_class($message) | ||||||||||||||||||||||||||||||||||||||
| .'".' | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| foreach ($this->adapters as $index => $adapter) { | ||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| return $adapter->send($message); | ||||||||||||||||||||||||||||||||||||||
| } catch (\Exception $e) { | ||||||||||||||||||||||||||||||||||||||
| $errors[] = $adapter->getName() | ||||||||||||||||||||||||||||||||||||||
| .' (adapter ' | ||||||||||||||||||||||||||||||||||||||
| .($index + 1) | ||||||||||||||||||||||||||||||||||||||
| .'): ' | ||||||||||||||||||||||||||||||||||||||
| .$e->getMessage(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $adapterCount = \count($this->adapters); | ||||||||||||||||||||||||||||||||||||||
| $adapterLabel = $adapterCount === 1 ? 'adapter' : 'adapters'; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| throw new \Exception( | ||||||||||||||||||||||||||||||||||||||
| 'All ' | ||||||||||||||||||||||||||||||||||||||
| .$adapterCount | ||||||||||||||||||||||||||||||||||||||
| .' ' | ||||||||||||||||||||||||||||||||||||||
| .$adapterLabel | ||||||||||||||||||||||||||||||||||||||
| ." failed:\n" | ||||||||||||||||||||||||||||||||||||||
| .\implode("\n", $errors) | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function getMessageType(): string | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return $this->adapters[0]->getMessageType(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function getType(): string | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return $this->adapters[0]->getType(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| public function getMaxMessagesPerRequest(): int | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| return array_reduce( | ||||||||||||||||||||||||||||||||||||||
| $this->adapters, | ||||||||||||||||||||||||||||||||||||||
| fn ($min, $adapter) => min($min, $adapter->getMaxMessagesPerRequest()), | ||||||||||||||||||||||||||||||||||||||
| PHP_INT_MAX | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private function validateAdapters(array $adapters): void | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| $firstAdapter = $adapters[0]; | ||||||||||||||||||||||||||||||||||||||
| $expectedType = $firstAdapter->getType(); | ||||||||||||||||||||||||||||||||||||||
| $expectedMessageType = $firstAdapter->getMessageType(); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| foreach (\array_slice($adapters, 1, preserve_keys: true) as $index => $adapter) { | ||||||||||||||||||||||||||||||||||||||
| if ($adapter->getType() !== $expectedType) { | ||||||||||||||||||||||||||||||||||||||
| throw new \InvalidArgumentException( | ||||||||||||||||||||||||||||||||||||||
| 'All adapters must be of the same type. Expected "' | ||||||||||||||||||||||||||||||||||||||
| .$expectedType | ||||||||||||||||||||||||||||||||||||||
| .'", but adapter ' | ||||||||||||||||||||||||||||||||||||||
| .($index + 1) | ||||||||||||||||||||||||||||||||||||||
| .' (' | ||||||||||||||||||||||||||||||||||||||
| .$adapter->getName() | ||||||||||||||||||||||||||||||||||||||
| .') has type "' | ||||||||||||||||||||||||||||||||||||||
| .$adapter->getType() | ||||||||||||||||||||||||||||||||||||||
| .'".' | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if ($adapter->getMessageType() !== $expectedMessageType) { | ||||||||||||||||||||||||||||||||||||||
| throw new \InvalidArgumentException( | ||||||||||||||||||||||||||||||||||||||
| 'All adapters must support the same message type. Expected "' | ||||||||||||||||||||||||||||||||||||||
| .$expectedMessageType | ||||||||||||||||||||||||||||||||||||||
| .'", but adapter ' | ||||||||||||||||||||||||||||||||||||||
| .($index + 1) | ||||||||||||||||||||||||||||||||||||||
| .' (' | ||||||||||||||||||||||||||||||||||||||
| .$adapter->getName() | ||||||||||||||||||||||||||||||||||||||
| .') supports "' | ||||||||||||||||||||||||||||||||||||||
| .$adapter->getMessageType() | ||||||||||||||||||||||||||||||||||||||
| .'".' | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parseIntOptionskips range validation when the value is already a PHPint.parse_urlreturns$parts['port']as a native integer, so a URL likesmtp://host:0orsmtp://host:99999bypasses thectype_digitcheck entirely and passes an invalid port directly to the SMTP constructor. The same zero/overflow gap applies to query-string ports sincectype_digit("0")andctype_digit("99999")both return true. Adding a positive-integer guard closes this for all call sites (port,timeout,timelimit).