Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Utopia/Messaging/Adapter/Email/Resend.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,42 @@ public function getMaxMessagesPerRequest(): int
*/
protected function process(EmailMessage $message): array
{
// Resend doesn't support attachments yet
$attachments = [];
if (! \is_null($message->getAttachments()) && ! empty($message->getAttachments())) {
throw new \Exception('Resend does not support attachments at this time');
$size = 0;
foreach ($message->getAttachments() as $attachment) {
if ($attachment->getContent() !== null) {
$size += \strlen($attachment->getContent());
} else {
$fileSize = \filesize($attachment->getPath());
if ($fileSize === false) {
throw new \Exception('Failed to read attachment file: '.$attachment->getPath());
}
$size += $fileSize;
}
}

if ($size > self::MAX_ATTACHMENT_BYTES) {
throw new \Exception('Total attachment size exceeds '.self::MAX_ATTACHMENT_BYTES.' bytes');
}

foreach ($message->getAttachments() as $attachment) {
if ($attachment->getContent() !== null) {
$content = \base64_encode($attachment->getContent());
} else {
$data = \file_get_contents($attachment->getPath());
if ($data === false) {
throw new \Exception('Failed to read attachment file: '.$attachment->getPath());
}
$content = \base64_encode($data);
}

$attachments[] = [
'filename' => $attachment->getName(),
'content' => $content,
'content_type' => $attachment->getType(),
];
}
}

$response = new Response($this->getType());
Expand Down Expand Up @@ -78,6 +111,10 @@ protected function process(EmailMessage $message): array
}
}

if (! empty($attachments)) {
$email['attachments'] = $attachments;
}

if (! \is_null($message->getBCC()) && ! empty($message->getBCC())) {
$bccList = [];
foreach ($message->getBCC() as $bcc) {
Expand Down
65 changes: 52 additions & 13 deletions tests/Messaging/Adapter/Email/ResendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,68 @@ public function testSendMultipleEmails(): void
$this->assertEquals('success', $response['results'][1]['status'], \var_export($response, true));
}

public function testSendEmailWithAttachmentsThrowsException(): void
public function testSendEmailWithFileAttachment(): 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,
to: [$this->testEmail],
subject: 'Test File Attachment',
content: 'Test Content with file attachment',
fromName: 'Test Sender',
fromEmail: $fromEmail,
fromEmail: $this->testEmail,
attachments: [new Attachment(
name: 'image.png',
path: __DIR__.'/../../../assets/image.png',
type: 'image/png'
)],
);

$response = $this->sender->send($message);

$this->assertResponse($response);
}

public function testSendEmailWithStringAttachment(): void
{
$message = new Email(
to: [$this->testEmail],
subject: 'Test String Attachment',
content: 'Test Content with string attachment',
fromName: 'Test Sender',
fromEmail: $this->testEmail,
attachments: [new Attachment(
name: 'test.txt',
path: '',
type: 'text/plain',
content: 'Hello, this is a test attachment.',
)],
);

$response = $this->sender->send($message);

$this->assertResponse($response);
}

public function testSendEmailWithAttachmentExceedingMaxSize(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Total attachment size exceeds');

$largeContent = \str_repeat('x', 25 * 1024 * 1024 + 1);

$message = new Email(
to: [$this->testEmail],
subject: 'Test Oversized Attachment',
content: 'Test Content',
fromName: 'Test Sender',
fromEmail: $this->testEmail,
attachments: [new Attachment(
name: 'large.bin',
path: '',
type: 'application/octet-stream',
content: $largeContent,
)],
);

$this->sender->send($message);
}
}
Loading