-
Couldn't load subscription status.
- Fork 66
feat: add Resend email adapter #103
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter\Email; | ||
|
|
||
| use Utopia\Messaging\Adapter\Email as EmailAdapter; | ||
| use Utopia\Messaging\Messages\Email as EmailMessage; | ||
| use Utopia\Messaging\Response; | ||
|
|
||
| class Resend extends EmailAdapter | ||
| { | ||
| protected const NAME = 'Resend'; | ||
|
|
||
| /** | ||
| * @param string $apiKey Your Resend API key to authenticate with the API. | ||
| */ | ||
| public function __construct( | ||
| private string $apiKey | ||
| ) { | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 100; | ||
| } | ||
|
|
||
| /** | ||
| * Uses Resend's batch sending API to send multiple emails at once. | ||
| * | ||
| * @link https://resend.com/docs/api-reference/emails/send-batch-emails | ||
| */ | ||
| protected function process(EmailMessage $message): array | ||
| { | ||
| // Resend doesn't support attachments yet | ||
| if (! \is_null($message->getAttachments()) && ! empty($message->getAttachments())) { | ||
| throw new \Exception('Resend does not support attachments at this time'); | ||
| } | ||
|
|
||
| $response = new Response($this->getType()); | ||
|
|
||
| $emails = []; | ||
| foreach ($message->getTo() as $to) { | ||
| $email = [ | ||
| 'from' => $message->getFromName() | ||
| ? "{$message->getFromName()} <{$message->getFromEmail()}>" | ||
| : $message->getFromEmail(), | ||
| 'to' => [$to], | ||
| 'subject' => $message->getSubject(), | ||
| ]; | ||
|
|
||
| if ($message->isHtml()) { | ||
| $email['html'] = $message->getContent(); | ||
| } else { | ||
| $email['text'] = $message->getContent(); | ||
| } | ||
|
|
||
| if (! empty($message->getReplyToEmail())) { | ||
| $email['reply_to'] = $message->getReplyToName() | ||
| ? ["{$message->getReplyToName()} <{$message->getReplyToEmail()}>"] | ||
| : [$message->getReplyToEmail()]; | ||
| } | ||
ChiragAgg5k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (! \is_null($message->getCC()) && ! empty($message->getCC())) { | ||
| $ccList = []; | ||
| foreach ($message->getCC() as $cc) { | ||
| if (! empty($cc['email'])) { | ||
| $ccList[] = ! empty($cc['name']) | ||
| ? "{$cc['name']} <{$cc['email']}>" | ||
| : $cc['email']; | ||
| } | ||
| } | ||
| if (! empty($ccList)) { | ||
| $email['cc'] = $ccList; | ||
| } | ||
| } | ||
|
|
||
| if (! \is_null($message->getBCC()) && ! empty($message->getBCC())) { | ||
| $bccList = []; | ||
| foreach ($message->getBCC() as $bcc) { | ||
| if (! empty($bcc['email'])) { | ||
| $bccList[] = ! empty($bcc['name']) | ||
| ? "{$bcc['name']} <{$bcc['email']}>" | ||
| : $bcc['email']; | ||
| } | ||
| } | ||
| if (! empty($bccList)) { | ||
| $email['bcc'] = $bccList; | ||
| } | ||
| } | ||
ChiragAgg5k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $emails[] = $email; | ||
| } | ||
|
|
||
| $headers = [ | ||
| 'Authorization: Bearer '.$this->apiKey, | ||
| 'Content-Type: application/json', | ||
| ]; | ||
|
|
||
| $result = $this->request( | ||
| method: 'POST', | ||
| url: 'https://api.resend.com/emails/batch', | ||
| headers: $headers, | ||
| body: $emails, // @phpstan-ignore-line | ||
| ); | ||
|
|
||
| $statusCode = $result['statusCode']; | ||
|
|
||
| if ($statusCode === 200) { | ||
| $responseData = $result['response']; | ||
|
|
||
| if (isset($responseData['errors']) && ! empty($responseData['errors'])) { | ||
| $failedIndices = []; | ||
| foreach ($responseData['errors'] as $error) { | ||
| $failedIndices[$error['index']] = $error['message']; | ||
| } | ||
|
|
||
| foreach ($message->getTo() as $index => $to) { | ||
| if (isset($failedIndices[$index])) { | ||
| $response->addResult($to, $failedIndices[$index]); | ||
| } else { | ||
| $response->addResult($to); | ||
| } | ||
| } | ||
|
|
||
| $successCount = \count($message->getTo()) - \count($failedIndices); | ||
| $response->setDeliveredTo($successCount); | ||
| } else { | ||
| $response->setDeliveredTo(\count($message->getTo())); | ||
| foreach ($message->getTo() as $to) { | ||
| $response->addResult($to); | ||
| } | ||
| } | ||
| } elseif ($statusCode >= 400 && $statusCode < 500) { | ||
| $errorMessage = 'Unknown error'; | ||
|
|
||
| if (\is_string($result['response'])) { | ||
| $errorMessage = $result['response']; | ||
| } elseif (isset($result['response']['message'])) { | ||
| $errorMessage = $result['response']['message']; | ||
| } elseif (isset($result['response']['error'])) { | ||
| $errorMessage = $result['response']['error']; | ||
| } | ||
|
|
||
| foreach ($message->getTo() as $to) { | ||
| $response->addResult($to, $errorMessage); | ||
| } | ||
| } elseif ($statusCode >= 500) { | ||
| $errorMessage = 'Server error'; | ||
|
|
||
| if (\is_string($result['response'])) { | ||
| $errorMessage = $result['response']; | ||
| } elseif (isset($result['response']['message'])) { | ||
| $errorMessage = $result['response']['message']; | ||
| } | ||
|
|
||
| foreach ($message->getTo() as $to) { | ||
| $response->addResult($to, $errorMessage); | ||
| } | ||
| } | ||
|
|
||
| return $response->toArray(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Adapter\Email; | ||
|
|
||
| use Utopia\Messaging\Adapter\Email\Resend; | ||
| use Utopia\Messaging\Messages\Email; | ||
| use Utopia\Messaging\Messages\Email\Attachment; | ||
| use Utopia\Tests\Adapter\Base; | ||
|
|
||
| class ResendTest extends Base | ||
| { | ||
| private Resend $sender; | ||
| private string $testEmail; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| $key = \getenv('RESEND_API_KEY'); | ||
| $this->sender = new Resend($key); | ||
| $this->testEmail = \getenv('RESEND_TEST_EMAIL'); | ||
|
|
||
| sleep(2); | ||
| } | ||
|
|
||
| public function testSendEmail(): void | ||
| { | ||
| $to = $this->testEmail; | ||
| $subject = 'Test Subject'; | ||
| $content = 'Test Content'; | ||
| $fromEmail = $this->testEmail; | ||
| $cc = [['email' => $this->testEmail]]; | ||
| $bcc = [['name' => 'Test BCC', 'email' => $this->testEmail]]; | ||
|
|
||
| $message = new Email( | ||
| to: [$to], | ||
| subject: $subject, | ||
| content: $content, | ||
| fromName: 'Test Sender', | ||
| fromEmail: $fromEmail, | ||
| cc: $cc, | ||
| bcc: $bcc, | ||
| ); | ||
|
|
||
| $response = $this->sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
|
|
||
| public function testSendEmailWithHtml(): void | ||
| { | ||
| $to = $this->testEmail; | ||
| $subject = 'Test HTML Subject'; | ||
| $content = '<h1>Test HTML Content</h1><p>This is a test email with HTML content.</p>'; | ||
| $fromEmail = $this->testEmail; | ||
|
|
||
| $message = new Email( | ||
| to: [$to], | ||
| subject: $subject, | ||
| content: $content, | ||
| fromName: 'Test Sender', | ||
| fromEmail: $fromEmail, | ||
| html: true, | ||
| ); | ||
|
|
||
| $response = $this->sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
|
|
||
| public function testSendEmailWithReplyTo(): void | ||
| { | ||
| $to = $this->testEmail; | ||
| $subject = 'Test Reply-To Subject'; | ||
| $content = 'Test Content with Reply-To'; | ||
| $fromEmail = $this->testEmail; | ||
| $replyToEmail = $this->testEmail; | ||
|
|
||
| $message = new Email( | ||
| to: [$to], | ||
| subject: $subject, | ||
| content: $content, | ||
| fromName: 'Test Sender', | ||
| fromEmail: $fromEmail, | ||
| replyToName: 'Reply To Name', | ||
| replyToEmail: $replyToEmail, | ||
| ); | ||
|
|
||
| $response = $this->sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
|
|
||
| public function testSendMultipleEmails(): void | ||
| { | ||
| $to1 = $this->testEmail; | ||
| $to2 = $this->testEmail; | ||
| $subject = 'Test Batch Subject'; | ||
| $content = 'Test Batch Content'; | ||
| $fromEmail = $this->testEmail; | ||
|
|
||
| $message = new Email( | ||
| to: [$to1, $to2], | ||
| subject: $subject, | ||
| content: $content, | ||
| fromName: 'Test Sender', | ||
| fromEmail: $fromEmail, | ||
| ); | ||
|
|
||
| $response = $this->sender->send($message); | ||
|
|
||
| $this->assertEquals(2, $response['deliveredTo'], \var_export($response, true)); | ||
| $this->assertEquals('', $response['results'][0]['error'], \var_export($response, true)); | ||
| $this->assertEquals('success', $response['results'][0]['status'], \var_export($response, true)); | ||
| $this->assertEquals('', $response['results'][1]['error'], \var_export($response, true)); | ||
| $this->assertEquals('success', $response['results'][1]['status'], \var_export($response, true)); | ||
| } | ||
|
|
||
| public function testSendEmailWithAttachmentsThrowsException(): void | ||
| { | ||
| $this->expectException(\Exception::class); | ||
| $this->expectExceptionMessage('Resend does not support attachments at this time'); | ||
|
|
||
| $to = $this->testEmail; | ||
| $subject = 'Test Subject'; | ||
| $content = 'Test Content'; | ||
| $fromEmail = $this->testEmail; | ||
|
|
||
| $message = new Email( | ||
| to: [$to], | ||
| subject: $subject, | ||
| content: $content, | ||
| fromName: 'Test Sender', | ||
| fromEmail: $fromEmail, | ||
| attachments: [new Attachment( | ||
| name: 'image.png', | ||
| path: __DIR__.'/../../../assets/image.png', | ||
| type: 'image/png' | ||
| )], | ||
| ); | ||
|
|
||
| $this->sender->send($message); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.