Mercurial > thymian
comparison mailer/main.cpp @ 0:a4671277546c tip
created the repository for the thymian project
| author | ferencd |
|---|---|
| date | Tue, 17 Aug 2021 11:19:54 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a4671277546c |
|---|---|
| 1 #include <vmime/vmime.hpp> | |
| 2 #include <json.h> | |
| 3 #include <templater.h> | |
| 4 | |
| 5 #include <iostream> | |
| 6 #include <sstream> | |
| 7 #include <string> | |
| 8 #include <fstream> | |
| 9 #include <streambuf> | |
| 10 | |
| 11 void send_message(const std::string& destination, const std::string& subject, const std::string& body) | |
| 12 { | |
| 13 std::stringstream emailBodySS; | |
| 14 emailBodySS << body; | |
| 15 | |
| 16 // create the email message as per vmime's instructions | |
| 17 vmime::messageBuilder mb; | |
| 18 mb.setSubject(vmime::text(subject)); | |
| 19 mb.setExpeditor(vmime::mailbox("no-reply@cloudy.sh")); | |
| 20 mb.getRecipients().appendAddress(vmime::make_shared<vmime::mailbox>(destination)); | |
| 21 mb.getTextPart()->setCharset(vmime::charsets::ISO8859_15) ; | |
| 22 mb.getTextPart()->setText(vmime::make_shared<vmime::stringContentHandler>(emailBodySS.str())); | |
| 23 | |
| 24 // Message construction | |
| 25 vmime::shared_ptr<vmime::message> msg = mb.construct(); | |
| 26 | |
| 27 // Output raw message data to a stringstream | |
| 28 std::stringstream ss; | |
| 29 vmime::utility::outputStreamAdapter out(ss) ; | |
| 30 msg->generate(out); | |
| 31 std::string s = ss.str(); | |
| 32 | |
| 33 vmime::utility::url url("smtp://localhost"); | |
| 34 vmime::shared_ptr<vmime::net::session> sess = vmime::make_shared<vmime::net::session>(); | |
| 35 vmime::shared_ptr<vmime::net::transport> tr = sess->getTransport(url) ; | |
| 36 tr->connect(); | |
| 37 vmime::utility::inputStreamStringAdapter issa(s) ; | |
| 38 | |
| 39 vmime::mailbox from("no-reply@cloudy.sh") ; | |
| 40 vmime::mailboxList to; | |
| 41 to.appendMailbox(vmime::make_shared<vmime::mailbox>(destination) ) ; | |
| 42 | |
| 43 // send the email | |
| 44 tr->send( from, to, issa, s.length ( ) ) ; | |
| 45 tr->disconnect () ; | |
| 46 | |
| 47 } | |
| 48 | |
| 49 int main() | |
| 50 { | |
| 51 using nlohmann::json; | |
| 52 | |
| 53 std::ifstream i("dest.json"); | |
| 54 json j; | |
| 55 i >> j; | |
| 56 | |
| 57 std::cout << "Subject:" << j["mail"]["subject"] << std::endl; | |
| 58 std::cout << "Body" << j["mail"]["body"] << std::endl; | |
| 59 | |
| 60 | |
| 61 std::ifstream t(j["mail"]["body"]); | |
| 62 | |
| 63 if(!t) | |
| 64 { | |
| 65 std::cerr << "Invalid body file" << std::endl; | |
| 66 return 1; | |
| 67 } | |
| 68 | |
| 69 std::string str((std::istreambuf_iterator<char>(t)), | |
| 70 std::istreambuf_iterator<char>()); | |
| 71 | |
| 72 STRING_VAR_TEMPLATE(EmailBody, str); | |
| 73 | |
| 74 | |
| 75 for(const auto& t : j["mail"]["targets"]) | |
| 76 { | |
| 77 std::string to = t["to"]; | |
| 78 to.erase(remove( to.begin(), to.end(), '\"' ), to.end()); | |
| 79 | |
| 80 send_message(t["address"], j["mail"]["subject"], templater<EmailBody>().templatize("target" <is> to).get()); | |
| 81 std::cout << "To: " << t["address"] << std::endl << templater<EmailBody>().templatize("target" <is> t["to"]).get() << std::endl | |
| 82 << "----------------------------------"<< std::endl; | |
| 83 } | |
| 84 | |
| 85 } |
