Skip to content

Commit 707ff90

Browse files
me-hemUmesh Bhatt
andauthored
Added createEmail method to create new email message (#141)
* createEmail api added to create new email message and tested successfully * added addFieldToPayload lambda function to createEmail method --------- Co-authored-by: Umesh Bhatt <hatgrey@localhost.localdomain>
1 parent 5a5980e commit 707ff90

File tree

4 files changed

+165
-5
lines changed

4 files changed

+165
-5
lines changed

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 = "email001";
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: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nResponse: " + response);
625+
}
626+
}
627+
551628
std::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\nResponse: " + response);
927+
throw AppwriteException("Error fetching topic logs. Status code: " +
928+
std::to_string(statusCode) +
929+
"\n\nResponse: " + response);
852930
}
853931
}

0 commit comments

Comments
 (0)