Skip to content

Commit 07df0c8

Browse files
committed
createEmail api added to create new email message and tested successfully
1 parent 5a5980e commit 07df0c8

4 files changed

Lines changed: 203 additions & 5 deletions

File tree

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ listTargets: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp
279279
@mkdir -p ./$(TESTS_DIR)
280280
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listTargets $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listTargets.cpp $(LDFLAGS)
281281

282-
283282
listProviderLogs: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listProviderLogs.cpp
284283
@mkdir -p ./$(TESTS_DIR)
285284
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listProviderLogs $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listProviderLogs.cpp $(LDFLAGS)
@@ -288,6 +287,10 @@ createSms: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createSms.cpp
288287
@mkdir -p ./$(TESTS_DIR)
289288
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createSms $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createSms.cpp $(LDFLAGS)
290289

290+
createEmail: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createEmail.cpp
291+
@mkdir -p ./$(TESTS_DIR)
292+
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createEmail $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createEmail.cpp $(LDFLAGS)
293+
291294
# Messaging - Topics
292295
getTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp
293296
@mkdir -p ./$(TESTS_DIR)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "Appwrite.hpp"
2+
#include <chrono>
3+
#include <iostream>
4+
5+
int main() {
6+
std::string projectId = "";
7+
std::string apiKey = "";
8+
9+
Appwrite appwrite(projectId, apiKey);
10+
11+
std::string messageId = "6b9k4016e14b8";
12+
std::string subject = "Hello from C++ Appwrite SDK!";
13+
std::string content =
14+
"Testing Email message creation with topics, users, and targets.";
15+
16+
std::vector<std::string> topics = {};
17+
std::vector<std::string> users = {};
18+
std::vector<std::string> targets = {};
19+
std::vector<std::string> cc = {};
20+
std::vector<std::string> bcc = {};
21+
std::vector<std::string> attachments = {};
22+
23+
auto now = std::chrono::system_clock::now();
24+
auto future_time = now + std::chrono::minutes(5);
25+
auto time_t = std::chrono::system_clock::to_time_t(future_time);
26+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
27+
future_time.time_since_epoch()) %
28+
1000;
29+
30+
std::stringstream ss;
31+
ss << std::put_time(std::gmtime(&time_t), "%Y-%m-%dT%H:%M:%S");
32+
ss << "." << std::setfill('0') << std::setw(3) << ms.count() << "+00:00";
33+
std::string scheduled_at = ss.str();
34+
35+
bool draft = true;
36+
bool html = false;
37+
38+
try {
39+
std::string response = appwrite.getMessaging().createEmail(
40+
messageId, subject, content, topics, users, targets, cc, bcc,
41+
attachments, draft, html, scheduled_at);
42+
std::cout << "Email Message Created!\nResponse: " << response
43+
<< std::endl;
44+
} catch (const AppwriteException &ex) {
45+
std::cerr << "Exception: " << ex.what() << std::endl;
46+
}
47+
48+
return 0;
49+
}

include/classes/Messaging.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,36 @@ class Messaging {
177177
bool draft = false,
178178
const std::string &scheduled_at = "");
179179

180+
/**
181+
* @brief Create a new email message.
182+
*
183+
* @param messageId Unique ID for the message.
184+
* @param subject Subject line of the email.
185+
* @param content Email Content.
186+
* @param topics List of topic IDs (optional).
187+
* @param users List of User IDs (optional).
188+
* @param targets List of target IDs (optional).
189+
* @param cc List of target IDs to be added as CC.
190+
* @param bcc List of target IDs to be added as BCC.
191+
* @param attachments List of compound ID strings of bucket IDs and file IDs
192+
* to be attached to the email.
193+
* @param draft If true, saves the message as a draft.
194+
* @param html Is content of type HTML
195+
* @param scheduled_at Scheduled delivery time for message.
196+
* @return JSON response.
197+
*/
198+
std::string createEmail(const std::string &messageId,
199+
const std::string &subject,
200+
const std::string &content,
201+
const std::vector<std::string> &topics = {},
202+
const std::vector<std::string> &users = {},
203+
const std::vector<std::string> &targets = {},
204+
const std::vector<std::string> &cc = {},
205+
const std::vector<std::string> &bcc = {},
206+
const std::vector<std::string> &attachments = {},
207+
bool draft = false, bool html = false,
208+
const std::string &scheduled_at = "");
209+
180210
/**
181211
* @brief Updates an existing push notification
182212
* message.

src/services/Messaging.cpp

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nResponse: " + response);
663+
}
664+
}
665+
551666
std::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\nResponse: " + response);
965+
throw AppwriteException("Error fetching topic logs. Status code: " +
966+
std::to_string(statusCode) +
967+
"\n\nResponse: " + response);
852968
}
853969
}

0 commit comments

Comments
 (0)