@@ -385,3 +385,61 @@ std::string Messaging::createPush(const std::string &messageId,
385385 }
386386}
387387
388+ std::string Messaging::createMessage (const std::string& messageId,
389+ const std::string& subject,
390+ const std::string& content,
391+ const std::vector<std::string>& topics,
392+ const std::vector<std::string>& targets) {
393+ if (messageId.empty ()) {
394+ throw AppwriteException (" Missing required parameter: 'messageId'" );
395+ }
396+ if (subject.empty ()) {
397+ throw AppwriteException (" Missing required parameter: 'subject'" );
398+ }
399+ if (content.empty ()) {
400+ throw AppwriteException (" Missing required parameter: 'content'" );
401+ }
402+ if (topics.empty () && targets.empty ()) {
403+ throw AppwriteException (" At least one of 'topics' or 'targets' must be provided" );
404+ }
405+
406+ std::string payload = R"( {"messageId":")" + Utils::escapeJsonString (messageId) +
407+ R"( ","subject":")" + Utils::escapeJsonString (subject) +
408+ R"( ","content":")" + Utils::escapeJsonString (content) + R"( ")" ;
409+
410+ if (!topics.empty ()) {
411+ payload += R"( ,"topics":[)" ;
412+ for (size_t i = 0 ; i < topics.size (); ++i) {
413+ payload += " \" " + Utils::escapeJsonString (topics[i]) + " \" " ;
414+ if (i != topics.size () - 1 ) payload += " ," ;
415+ }
416+ payload += " ]" ;
417+ }
418+
419+ if (!targets.empty ()) {
420+ payload += R"( ,"targets":[)" ;
421+ for (size_t i = 0 ; i < targets.size (); ++i) {
422+ payload += " \" " + Utils::escapeJsonString (targets[i]) + " \" " ;
423+ if (i != targets.size () - 1 ) payload += " ," ;
424+ }
425+ payload += " ]" ;
426+ }
427+
428+ payload += " }" ;
429+
430+ std::string url = Config::API_BASE_URL + " /messaging/messages/email" ;
431+
432+ std::vector<std::string> headers = Config::getHeaders (projectId);
433+ headers.push_back (" X-Appwrite-Key: " + apiKey);
434+ headers.push_back (" Content-Type: application/json" );
435+
436+ std::string response;
437+ int statusCode = Utils::postRequest (url, payload, headers, response);
438+
439+ if (statusCode == HttpStatus::CREATED || statusCode == HttpStatus::OK ) {
440+ return response;
441+ } else {
442+ throw AppwriteException (" Error creating email message. Status code: " +
443+ std::to_string (statusCode) + " \n\n Response: " + response);
444+ }
445+ }
0 commit comments