@@ -548,6 +548,83 @@ std::string Messaging::createSms(const std::string &messageId,
548548 }
549549}
550550
551+ // Added method to create a new email message.
552+ std::string Messaging::createEmail (
553+ const std::string &messageId, const std::string &subject,
554+ const std::string &content, const std::vector<std::string> &topics,
555+ const std::vector<std::string> &users,
556+ const std::vector<std::string> &targets, const std::vector<std::string> &cc,
557+ const std::vector<std::string> &bcc,
558+ const std::vector<std::string> &attachments, bool draft, bool html,
559+ const std::string &scheduled_at) {
560+
561+ if (messageId.empty ()) {
562+ throw AppwriteException (" Missing required parameter: 'messageId'" );
563+ }
564+ if (subject.empty ()) {
565+ throw AppwriteException (" Missing required parameter: 'subject'" );
566+ }
567+ if (content.empty ()) {
568+ throw AppwriteException (" Missing required parameter: 'content'" );
569+ }
570+
571+ std::string payload =
572+ R"( {"messageId":")" + Utils::escapeJsonString (messageId) +
573+ R"( ","subject":")" + Utils::escapeJsonString (subject) +
574+ R"( ","content":")" + Utils::escapeJsonString (content) + R"( ")" ;
575+
576+ auto addFieldToPayload = [](std::string &payload,
577+ const std::string &fieldName,
578+ const std::vector<std::string> &items) {
579+ if (items.empty ())
580+ return ;
581+
582+ payload += R"( ,")" + fieldName + R"( ":[)" ;
583+ for (size_t i = 0 ; i < items.size (); ++i) {
584+ payload += " \" " + Utils::escapeJsonString (items[i]) + " \" " ;
585+ if (i != items.size () - 1 )
586+ payload += " ," ;
587+ }
588+ payload += " ]" ;
589+ };
590+
591+ addFieldToPayload (payload, " topics" , topics);
592+ addFieldToPayload (payload, " users" , users);
593+ addFieldToPayload (payload, " targets" , targets);
594+ addFieldToPayload (payload, " cc" , cc);
595+ addFieldToPayload (payload, " bcc" , bcc);
596+ addFieldToPayload (payload, " attachments" , attachments);
597+
598+ payload += std::string (R"( ,"draft":)" ) + (draft ? " true" : " false" );
599+
600+ payload += std::string (R"( ,"html":)" ) + (html ? " true" : " false" );
601+
602+ if (!scheduled_at.empty ()) {
603+ payload += R"( ,"scheduledAt":")" +
604+ Utils::escapeJsonString (scheduled_at) + " \" " ;
605+ }
606+
607+ payload += " }" ;
608+
609+ std::string url = Config::API_BASE_URL + " /messaging/messages/email" ;
610+
611+ std::vector<std::string> headers = Config::getHeaders (projectId);
612+ headers.push_back (" X-Appwrite-Key: " + apiKey);
613+ headers.push_back (" Content-Type: application/json" );
614+
615+ std::string response;
616+
617+ int statusCode = Utils::postRequest (url, payload, headers, response);
618+
619+ if (statusCode == HttpStatus::CREATED || statusCode == HttpStatus::OK) {
620+ return response;
621+ } else {
622+ throw AppwriteException (
623+ " Error creating a new email message. Status code: " +
624+ std::to_string (statusCode) + " \n\n Response: " + response);
625+ }
626+ }
627+
551628std::string Messaging::updateEmail (const std::string &messageId,
552629 const std::string &subject,
553630 const std::string &content) {
@@ -827,7 +904,8 @@ std::string Messaging::listTopicLogs(const std::string &topicId,
827904 throw AppwriteException (" Missing required parameter: 'topicId'" );
828905 }
829906
830- std::string url = Config::API_BASE_URL + " /messaging/topics/" + topicId + " /logs" ;
907+ std::string url =
908+ Config::API_BASE_URL + " /messaging/topics/" + topicId + " /logs" ;
831909
832910 std::string queryParam = " " ;
833911 if (!queries.empty ()) {
@@ -846,8 +924,8 @@ std::string Messaging::listTopicLogs(const std::string &topicId,
846924 if (statusCode == HttpStatus::OK) {
847925 return response;
848926 } else {
849- throw AppwriteException (
850- " Error fetching topic logs. Status code: " + std::to_string (statusCode) +
851- " \n\n Response: " + response);
927+ throw AppwriteException (" Error fetching topic logs. Status code: " +
928+ std::to_string (statusCode) +
929+ " \n\n Response: " + response);
852930 }
853931}
0 commit comments