From 4970aa7426925e3c4d0567e5d8d3bc4c37a9ce33 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 24 Sep 2025 22:38:12 +1200 Subject: [PATCH] Send email if notify on complete is set --- app/config/locale/translations/en.json | 8 +++ src/Appwrite/Platform/Workers/Migrations.php | 66 ++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/app/config/locale/translations/en.json b/app/config/locale/translations/en.json index e2ee20b2d7..c194819744 100644 --- a/app/config/locale/translations/en.json +++ b/app/config/locale/translations/en.json @@ -54,6 +54,14 @@ "emails.recovery.thanks": "Thanks,", "emails.recovery.buttonText": "Reset password", "emails.recovery.signature": "{{project}} team", + "emails.csvExport.subject": "Your CSV export is ready", + "emails.csvExport.preview": "Your data export has been completed successfully.", + "emails.csvExport.hello": "Hello {{user}},", + "emails.csvExport.body": "Your CSV export is ready for download. Click the link below to download your data export.", + "emails.csvExport.footer": "This download link will expire in 1 hour.", + "emails.csvExport.thanks": "Thanks,", + "emails.csvExport.buttonText": "Download CSV", + "emails.csvExport.signature": "{{project}} team", "emails.invitation.subject": "Invitation to {{team}} Team at {{project}}", "emails.invitation.preview": "{{owner}} invited you to join {{team}} at {{project}}", "emails.invitation.hello": "Hello {{user}},", diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index bbdf537157..ab8c7091cb 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -516,5 +516,71 @@ class Migrations extends Action return; } + $user = $this->dbForPlatform->findOne('users', [ + Query::equal('$sequence', [$userInternalId]) + ]); + + // Set up locale + $locale = new Locale(System::getEnv('_APP_LOCALE', 'en')); + $locale->setFallback(System::getEnv('_APP_LOCALE', 'en')); + + // Generate JWT + $expiry = (new \DateTime())->add(new \DateInterval('PT1H'))->format('U'); + $encoder = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', \intval($expiry), 0); + $jwt = $encoder->encode([ + 'bucketId' => $bucketId, + 'fileId' => $fileId, + 'projectId' => $project->getId(), + ]); + + // Generate download URL with JWT + $endpoint = System::getEnv('_APP_DOMAIN', ''); + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'enabled' ? 'https' : 'http'; + $downloadUrl = "{$protocol}://{$endpoint}/v1/storage/buckets/{$bucketId}/files/{$fileId}/push?project={$project->getId()}&jwt={$jwt}"; + + // Get localized email content + $subject = $locale->getText('emails.csvExport.subject'); + $preview = $locale->getText('emails.csvExport.preview'); + $hello = $locale->getText('emails.csvExport.hello'); + $body = $locale->getText('emails.csvExport.body'); + $footer = $locale->getText('emails.csvExport.footer'); + $thanks = $locale->getText('emails.csvExport.thanks'); + $buttonText = $locale->getText('emails.csvExport.buttonText'); + $signature = $locale->getText('emails.csvExport.signature'); + + // Build email body using inner template + $message = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-inner-base.tpl'); + $message + ->setParam('{{body}}', $body, escapeHtml: false) + ->setParam('{{hello}}', $hello) + ->setParam('{{footer}}', $footer) + ->setParam('{{thanks}}', $thanks) + ->setParam('{{buttonText}}', $buttonText) + ->setParam('{{signature}}', $signature) + ->setParam('{{direction}}', $locale->getText('settings.direction')) + ->setParam('{{project}}', $project->getAttribute('name')) + ->setParam('{{user}}', $user->getAttribute('name', $user->getAttribute('email'))) + ->setParam('{{redirect}}', $downloadUrl); + + $emailBody = $message->render(); + + $emailVariables = [ + 'direction' => $locale->getText('settings.direction'), + 'project' => $project->getAttribute('name'), + 'user' => $user->getAttribute('name', $user->getAttribute('email')), + 'redirect' => $downloadUrl, + ]; + + $queueForMails + ->setSubject($subject) + ->setPreview($preview) + ->setBody($emailBody) + ->setBodyTemplate(__DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl') + ->setVariables($emailVariables) + ->setName($user->getAttribute('name', $user->getAttribute('email'))) + ->setRecipient($user->getAttribute('email')) + ->trigger(); + + Console::info('CSV export notification email sent to ' . $user->getAttribute('email')); } }