diff --git a/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Create.php b/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Create.php index 06342a0e79..6a6f719b2b 100644 --- a/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Create.php +++ b/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Create.php @@ -21,6 +21,7 @@ use Utopia\Database\Helpers\ID; use Utopia\Database\Query; use Utopia\Database\Validator\Datetime as DatetimeValidator; use Utopia\Database\Validator\UID; +use Utopia\Emails\Validator\Email; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\ArrayList; @@ -71,6 +72,8 @@ class Create extends Action ->param('cc', [], fn (Database $dbForProject) => new ArrayList(new UID($dbForProject->getAdapter()->getMaxUIDLength())), 'Array of target IDs to be added as CC.', true, ['dbForProject']) ->param('bcc', [], fn (Database $dbForProject) => new ArrayList(new UID($dbForProject->getAdapter()->getMaxUIDLength())), 'Array of target IDs to be added as BCC.', true, ['dbForProject']) ->param('attachments', [], new ArrayList(new CompoundUID()), 'Array of compound ID strings of bucket IDs and file IDs to be attached to the email. They should be formatted as :.', true) + ->param('replyToEmail', '', new Email(allowEmpty: true), 'Email address to reply to. If not set, defaults to the sender email address.', true) + ->param('replyToName', '', new Text(128, 0), 'Name of the reply to recipient. If not set, defaults to the sender name.', true) ->param('draft', false, new Boolean(), 'Is message a draft', true) ->param('html', false, new Boolean(), 'Is content of type HTML', true) ->param('scheduledAt', null, new Nullable(new DatetimeValidator(requireDateInFuture: true)), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true) @@ -83,7 +86,7 @@ class Create extends Action ->callback($this->action(...)); } - public function action(string $messageId, string $subject, string $content, ?array $topics, ?array $users, ?array $targets, ?array $cc, ?array $bcc, ?array $attachments, bool $draft, bool $html, ?string $scheduledAt, Event $queueForEvents, Database $dbForProject, Database $dbForPlatform, Document $project, MessagingPublisher $publisherForMessaging, Response $response) + public function action(string $messageId, string $subject, string $content, ?array $topics, ?array $users, ?array $targets, ?array $cc, ?array $bcc, ?array $attachments, string $replyToEmail, string $replyToName, bool $draft, bool $html, ?string $scheduledAt, Event $queueForEvents, Database $dbForProject, Database $dbForPlatform, Document $project, MessagingPublisher $publisherForMessaging, Response $response) { $messageId = $messageId == 'unique()' ? ID::unique() @@ -158,6 +161,8 @@ class Create extends Action 'cc' => $cc, 'bcc' => $bcc, 'attachments' => $attachments, + 'replyToEmail' => $replyToEmail, + 'replyToName' => $replyToName, ], 'status' => $status, ]); diff --git a/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Update.php b/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Update.php index 540bb2703e..a85f96d4f0 100644 --- a/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Update.php +++ b/src/Appwrite/Platform/Modules/Messaging/Http/Messages/Email/Update.php @@ -17,6 +17,7 @@ use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Datetime as DatetimeValidator; use Utopia\Database\Validator\UID; +use Utopia\Emails\Validator\Email; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\ArrayList; @@ -68,6 +69,8 @@ class Update extends Action ->param('html', null, new Nullable(new Boolean()), 'Is content of type HTML', true) ->param('cc', null, fn (Database $dbForProject) => new Nullable(new ArrayList(new UID($dbForProject->getAdapter()->getMaxUIDLength()))), 'Array of target IDs to be added as CC.', true, ['dbForProject']) ->param('bcc', null, fn (Database $dbForProject) => new Nullable(new ArrayList(new UID($dbForProject->getAdapter()->getMaxUIDLength()))), 'Array of target IDs to be added as BCC.', true, ['dbForProject']) + ->param('replyToEmail', null, new Nullable(new Email(allowEmpty: true)), 'Email address to reply to. Pass an empty string to restore the provider or sender default.', true) + ->param('replyToName', null, new Nullable(new Text(128, 0)), 'Name of the reply to recipient. Pass an empty string to restore the provider or sender default.', true) ->param('scheduledAt', null, new Nullable(new DatetimeValidator(requireDateInFuture: true)), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true) ->param('attachments', null, new Nullable(new ArrayList(new CompoundUID())), 'Array of compound ID strings of bucket IDs and file IDs to be attached to the email. They should be formatted as :.', true) ->inject('queueForEvents') @@ -79,7 +82,7 @@ class Update extends Action ->callback($this->action(...)); } - public function action(string $messageId, ?array $topics, ?array $users, ?array $targets, ?string $subject, ?string $content, ?bool $draft, ?bool $html, ?array $cc, ?array $bcc, ?string $scheduledAt, ?array $attachments, Event $queueForEvents, Database $dbForProject, Database $dbForPlatform, Document $project, MessagingPublisher $publisherForMessaging, Response $response) + public function action(string $messageId, ?array $topics, ?array $users, ?array $targets, ?string $subject, ?string $content, ?bool $draft, ?bool $html, ?array $cc, ?array $bcc, ?string $replyToEmail, ?string $replyToName, ?string $scheduledAt, ?array $attachments, Event $queueForEvents, Database $dbForProject, Database $dbForPlatform, Document $project, MessagingPublisher $publisherForMessaging, Response $response) { $message = $dbForProject->getDocument('messages', $messageId); @@ -229,6 +232,14 @@ class Update extends Action $data['bcc'] = $bcc; } + if (!\is_null($replyToEmail)) { + $data['replyToEmail'] = $replyToEmail; + } + + if (!\is_null($replyToName)) { + $data['replyToName'] = $replyToName; + } + $message->setAttribute('data', $data); if (!\is_null($status)) { diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index d1db450aef..1c7dfd9415 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -996,9 +996,9 @@ class Messaging extends Action ): Email { $fromName = $provider['options']['fromName'] ?? null; $fromEmail = $provider['options']['fromEmail'] ?? null; - $replyToEmail = $provider['options']['replyToEmail'] ?? null; - $replyToName = $provider['options']['replyToName'] ?? null; $data = $message['data'] ?? []; + $replyToEmail = !empty($data['replyToEmail']) ? $data['replyToEmail'] : ($provider['options']['replyToEmail'] ?? null); + $replyToName = !empty($data['replyToName']) ? $data['replyToName'] : ($provider['options']['replyToName'] ?? null); $ccTargets = $data['cc'] ?? []; $bccTargets = $data['bcc'] ?? []; $cc = []; diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index e8fd23b49c..f1233d0cfc 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -2772,6 +2772,211 @@ trait MessagingBase $this->assertSame(0, \count($message['body']['deliveryErrors'])); } + public function testEmailReplyTo(): void + { + // Create Topic + $topic = $this->client->call(Client::METHOD_POST, '/messaging/topics', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'topicId' => ID::unique(), + 'name' => 'topic-reply-to', + ]); + + $this->assertEquals(201, $topic['headers']['status-code']); + + /** + * Test for SUCCESS + */ + $email = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'messageId' => ID::unique(), + 'draft' => true, + 'topics' => [$topic['body']['$id']], + 'subject' => 'Reply to test', + 'content' => 'Reply to the sender', + 'replyToEmail' => 'reply@example.com', + 'replyToName' => 'Reply Person', + ]); + + $this->assertEquals(201, $email['headers']['status-code']); + $this->assertEquals('reply@example.com', $email['body']['data']['replyToEmail']); + $this->assertEquals('Reply Person', $email['body']['data']['replyToName']); + + $messageId = $email['body']['$id']; + + // Update the reply to fields + $updated = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $messageId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'replyToEmail' => 'newreply@example.com', + 'replyToName' => 'New Reply Person', + ]); + + $this->assertEquals(200, $updated['headers']['status-code']); + $this->assertEquals('newreply@example.com', $updated['body']['data']['replyToEmail']); + $this->assertEquals('New Reply Person', $updated['body']['data']['replyToName']); + + // Untouched reply to fields stay intact when other fields are updated + $updated = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $messageId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'subject' => 'Reply to test updated', + ]); + + $this->assertEquals(200, $updated['headers']['status-code']); + $this->assertEquals('newreply@example.com', $updated['body']['data']['replyToEmail']); + $this->assertEquals('New Reply Person', $updated['body']['data']['replyToName']); + + // An empty string clears the custom reply to, restoring the provider or sender default + $updated = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $messageId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'replyToEmail' => '', + 'replyToName' => '', + ]); + + $this->assertEquals(200, $updated['headers']['status-code']); + $this->assertEquals('', $updated['body']['data']['replyToEmail']); + $this->assertEquals('', $updated['body']['data']['replyToName']); + + /** + * Test for FAILURE + */ + $email = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'messageId' => ID::unique(), + 'draft' => true, + 'topics' => [$topic['body']['$id']], + 'subject' => 'Reply to test', + 'content' => 'Reply to the sender', + 'replyToEmail' => 'not-an-email', + ]); + + $this->assertEquals(400, $email['headers']['status-code']); + } + + public function testSendEmailWithReplyTo(): void + { + $provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/smtp', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'providerId' => ID::unique(), + 'name' => 'Maildev', + 'host' => System::getEnv('_APP_SMTP_HOST', 'maildev'), + 'port' => (int) System::getEnv('_APP_SMTP_PORT', '1025'), + 'username' => System::getEnv('_APP_SMTP_USERNAME', 'user'), + 'password' => System::getEnv('_APP_SMTP_PASSWORD', 'password'), + 'fromName' => 'Appwrite', + 'fromEmail' => 'sender@appwrite.io', + 'enabled' => true, + ]); + + $this->assertEquals(201, $provider['headers']['status-code']); + + $topic = $this->client->call(Client::METHOD_POST, '/messaging/topics', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'topicId' => ID::unique(), + 'name' => 'reply-to-send', + ]); + + $this->assertEquals(201, $topic['headers']['status-code']); + + $user = $this->client->call(Client::METHOD_POST, '/users', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'userId' => ID::unique(), + 'email' => \uniqid() . '@appwrite.io', + 'password' => 'password', + 'name' => 'Reply To User', + ]); + + $this->assertEquals(201, $user['headers']['status-code']); + + $recipient = \uniqid() . '@appwrite.io'; + $target = $this->client->call(Client::METHOD_POST, '/users/' . $user['body']['$id'] . '/targets', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'targetId' => ID::unique(), + 'providerType' => 'email', + 'providerId' => $provider['body']['$id'], + 'identifier' => $recipient, + ]); + + $this->assertEquals(201, $target['headers']['status-code']); + + $subscriber = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topic['body']['$id'] . '/subscribers', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'subscriberId' => ID::unique(), + 'targetId' => $target['body']['$id'], + ]); + + $this->assertEquals(201, $subscriber['headers']['status-code']); + + $subject = 'Reply to send ' . \uniqid(); + + $email = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'messageId' => ID::unique(), + 'topics' => [$topic['body']['$id']], + 'subject' => $subject, + 'content' => 'Reply to the custom address', + 'replyToEmail' => 'support@appwrite.io', + 'replyToName' => 'Appwrite Support', + ]); + + $this->assertEquals(201, $email['headers']['status-code']); + + $messageId = $email['body']['$id']; + $this->assertEventually(function () use ($messageId) { + $response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $messageId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(MessageStatus::SENT, $response['body']['status']); + }, 30000, 500); + + // The custom reply-to overrides the provider default (which falls back to the sender). + $mail = $this->getLastEmail(1, fn (array $mail) => $this->assertSame($subject, $mail['subject'])); + + $this->assertSame('support@appwrite.io', $mail['replyTo'][0]['address']); + $this->assertSame('Appwrite Support', $mail['replyTo'][0]['name']); + + $this->client->call(Client::METHOD_DELETE, '/messaging/providers/' . $provider['body']['$id'], [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + } + public function testSendSMS(): void { if (empty(System::getEnv('_APP_MESSAGE_SMS_TEST_DSN'))) {