Skip to content

Commit 2125d40

Browse files
feat: createEmail (#96)
* Feat/createEmail * C++ docs
1 parent 09a117b commit 2125d40

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ listMessages: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listMessages.cpp
249249
getMessages: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/getMessages.cpp
250250
@mkdir -p ./$(TESTS_DIR)
251251
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/getMessages $(SRCS) $(EXAMPLES_DIR)/messaging/messages/getMessages.cpp $(LDFLAGS)
252+
createMessage: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createMessage.cpp
253+
@mkdir -p ./$(TESTS_DIR)
254+
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createMessage $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createMessage.cpp $(LDFLAGS)
252255

253256
# Messaging - Topics
254257
getTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "Appwrite.hpp"
2+
#include <iostream>
3+
4+
int main() {
5+
std::string projectId = "<your_project_id>";
6+
std::string apiKey = "<your_api_key>";
7+
8+
Appwrite appwrite(projectId, apiKey);
9+
10+
std::string messageId = "<your_message_id>";
11+
std::string subject = "Hello from C++ Appwrite SDK!";
12+
std::string content = "Testing email message creation with topics and targets.";
13+
14+
std::vector<std::string> topics = {"<topic_id>"};
15+
std::vector<std::string> targets = {"<target_id>"};
16+
17+
try {
18+
std::string response = appwrite.getMessaging().createMessage(
19+
messageId, subject, content, topics, targets
20+
);
21+
std::cout << "Email Message Created!\nResponse: " << response << std::endl;
22+
} catch (const AppwriteException& ex) {
23+
std::cerr << "Exception: " << ex.what() << std::endl;
24+
}
25+
26+
return 0;
27+
}

include/classes/Messaging.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,25 @@ class Messaging {
119119
std::string createPush(const std::string &messageId,
120120
const std::string &title,
121121
const std::string &body,
122-
const std::string &topicId);
122+
const std::string &topicId);
123+
/**
124+
* @brief Create a new email message.
125+
*
126+
* Sends a new email message to specific topics and/or target recipients.
127+
* At least one of `topics` or `targets` must be provided.
128+
*
129+
* @param messageId Unique ID for the message.
130+
* @param subject Subject line of the email.
131+
* @param content Body content of the email.
132+
* @param topics List of topic IDs to send the message to (optional).
133+
* @param targets List of target recipients (e.g., email:userId) (optional).
134+
* @return JSON response.
135+
*/
136+
std::string createMessage(const std::string& messageId,
137+
const std::string& subject,
138+
const std::string& content,
139+
const std::vector<std::string>& topics = {},
140+
const std::vector<std::string>& targets = {});
123141

124142
private:
125143
std::string projectId; ///< Project ID

src/services/Messaging.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nResponse: " + response);
444+
}
445+
}

0 commit comments

Comments
 (0)