@@ -548,6 +548,121 @@ 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+ if (!topics.empty ()) {
577+ payload += R"( ,"topics":[)" ;
578+ for (size_t i = 0 ; i < topics.size (); ++i) {
579+ payload += " \" " + Utils::escapeJsonString (topics[i]) + " \" " ;
580+ if (i != topics.size () - 1 )
581+ payload += " ," ;
582+ }
583+ payload += " ]" ;
584+ }
585+
586+ if (!users.empty ()) {
587+ payload += R"( ,"users":[)" ;
588+ for (size_t i = 0 ; i < users.size (); ++i) {
589+ payload += " \" " + Utils::escapeJsonString (users[i]) + " \" " ;
590+ if (i != users.size () - 1 )
591+ payload += " ," ;
592+ }
593+ payload += " ]" ;
594+ }
595+
596+ if (!targets.empty ()) {
597+ payload += R"( ,"targets":[)" ;
598+ for (size_t i = 0 ; i < targets.size (); ++i) {
599+ payload += " \" " + Utils::escapeJsonString (targets[i]) + " \" " ;
600+ if (i != targets.size () - 1 )
601+ payload += " ," ;
602+ }
603+ payload += " ]" ;
604+ }
605+
606+ if (!cc.empty ()) {
607+ payload += R"( ,"cc":[)" ;
608+ for (size_t i = 0 ; i < cc.size (); ++i) {
609+ payload += " \" " + Utils::escapeJsonString (cc[i]) + " \" " ;
610+ if (i != cc.size () - 1 )
611+ payload += " ," ;
612+ }
613+ payload += " ]" ;
614+ }
615+
616+ if (!bcc.empty ()) {
617+ payload += R"( ,"bcc":[)" ;
618+ for (size_t i = 0 ; i < bcc.size (); ++i) {
619+ payload += " \" " + Utils::escapeJsonString (bcc[i]) + " \" " ;
620+ if (i != bcc.size () - 1 )
621+ payload += " ," ;
622+ }
623+ payload += " ]" ;
624+ }
625+
626+ if (!attachments.empty ()) {
627+ payload += R"( ,"attachments":[)" ;
628+ for (size_t i = 0 ; i < attachments.size (); ++i) {
629+ payload += " \" " + Utils::escapeJsonString (attachments[i]) + " \" " ;
630+ if (i != attachments.size () - 1 )
631+ payload += " ," ;
632+ }
633+ payload += " ]" ;
634+ }
635+
636+ payload += std::string (R"( ,"draft":)" ) + (draft ? " true" : " false" );
637+
638+ payload += std::string (R"( ,"html":)" ) + (html ? " true" : " false" );
639+
640+ if (!scheduled_at.empty ()) {
641+ payload += R"( ,"scheduledAt":")" +
642+ Utils::escapeJsonString (scheduled_at) + " \" " ;
643+ }
644+
645+ payload += " }" ;
646+
647+ std::string url = Config::API_BASE_URL + " /messaging/messages/email" ;
648+
649+ std::vector<std::string> headers = Config::getHeaders (projectId);
650+ headers.push_back (" X-Appwrite-Key: " + apiKey);
651+ headers.push_back (" Content-Type: application/json" );
652+
653+ std::string response;
654+
655+ int statusCode = Utils::postRequest (url, payload, headers, response);
656+
657+ if (statusCode == HttpStatus::CREATED || statusCode == HttpStatus::OK ) {
658+ return response;
659+ } else {
660+ throw AppwriteException (
661+ " Error creating a new email message. Status code: " +
662+ std::to_string (statusCode) + " \n\n Response: " + response);
663+ }
664+ }
665+
551666std::string Messaging::updateEmail (const std::string &messageId,
552667 const std::string &subject,
553668 const std::string &content) {
@@ -827,7 +942,8 @@ std::string Messaging::listTopicLogs(const std::string &topicId,
827942 throw AppwriteException (" Missing required parameter: 'topicId'" );
828943 }
829944
830- std::string url = Config::API_BASE_URL + " /messaging/topics/" + topicId + " /logs" ;
945+ std::string url =
946+ Config::API_BASE_URL + " /messaging/topics/" + topicId + " /logs" ;
831947
832948 std::string queryParam = " " ;
833949 if (!queries.empty ()) {
@@ -846,8 +962,8 @@ std::string Messaging::listTopicLogs(const std::string &topicId,
846962 if (statusCode == HttpStatus::OK ) {
847963 return response;
848964 } else {
849- throw AppwriteException (
850- " Error fetching topic logs. Status code: " + std::to_string (statusCode) +
851- " \n\n Response: " + response);
965+ throw AppwriteException (" Error fetching topic logs. Status code: " +
966+ std::to_string (statusCode) +
967+ " \n\n Response: " + response);
852968 }
853969}
0 commit comments