diff options
author | eportnov <eportnov@ibs.ru> | 2022-09-12 16:46:49 +0300 |
---|---|---|
committer | eportnov <eportnov@ibs.ru> | 2022-09-12 16:46:49 +0300 |
commit | df33492b80495fd36a1a488c0b0a39753f1df415 (patch) | |
tree | c465900180840832e241ca5419dd90b429459588 /src/message_sender.cpp | |
parent | 9fa4addff6f90a8b5697a594e034f5517d64dd25 (diff) | |
parent | cdb7ae4c729b6a6c51b57eeefd21574952a4b767 (diff) | |
download | obmc-sila-smtp-df33492b80495fd36a1a488c0b0a39753f1df415.tar.xz |
Merge branch 'feature/refactor'
Diffstat (limited to 'src/message_sender.cpp')
-rw-r--r-- | src/message_sender.cpp | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/src/message_sender.cpp b/src/message_sender.cpp deleted file mode 100644 index 5b2950c..0000000 --- a/src/message_sender.cpp +++ /dev/null @@ -1,123 +0,0 @@ -#include <string.h> - -#include "message_sender.hpp" -#include "message_builder/date.hpp" -#include "message_builder/mail_to.hpp" -#include "message_builder/cc.hpp" -#include "message_builder/subject.hpp" -#include "message_builder/text.hpp" -#include "message_builder/from.hpp" - -namespace smtp -{ - static std::string mText = ""; - - // - // Constructors/Destructors - // - MessageSender::MessageSender( SettingsStorage const& settings_storage ) - : mSettingsStorage( settings_storage ) - { - - } - - // - //Public methods - // - bool MessageSender::Send( std::string const& mail_from, std::string const& mail_to, std::list<std::string> const& cc, - std::string const& subject, std::string const& text ) - { - CURLcode result = CURLE_OK; - - struct curl_slist *recipients = NULL; - struct WriteThis upload_ctx; - - upload_ctx.counter = 0; - - auto curl = curl_easy_init(); - - if( !curl ) - { - //Error - return false; - } - mText = GetText( mail_from, mail_to, cc, subject, text); - FillRecipients( curl, mail_to, cc, recipients ); - - curl_easy_setopt(curl, CURLOPT_USERNAME, mSettingsStorage.username.c_str()); - curl_easy_setopt(curl, CURLOPT_PASSWORD, mSettingsStorage.password.c_str()); - curl_easy_setopt(curl, CURLOPT_URL, GetHostPortData().c_str()); - - curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); - curl_easy_setopt(curl, CURLOPT_MAIL_FROM, mail_from.c_str()); - - curl_easy_setopt(curl, CURLOPT_READFUNCTION, ReadCallBack); - curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); - curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); - - result = curl_easy_perform(curl); - - if (result != CURLE_OK) - { - fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(result)); - return false; - } - - curl_slist_free_all(recipients); - curl_easy_cleanup(curl); - return true; - } - - // - //Private methods - // - std::string MessageSender::GetText( std::string const& mail_from, std::string const& mail_to, std::list<std::string> const& cc, - std::string const& subject, std::string const& text ) const - { - auto text_decorator = std::make_shared<message_builder::Text>( text ); - text_decorator->Apply( std::make_shared<message_builder::Subject>( subject ) ) - .Apply( std::make_shared<message_builder::Cc>( cc ) ) - .Apply( std::make_shared<message_builder::From>( mail_from ) ) - .Apply( std::make_shared<message_builder::MailTo>( mail_to ) ) - .Apply( std::make_shared<message_builder::Date>() ); - return text_decorator->Get(); - } - - void MessageSender::FillRecipients( CURL* curl, std::string const& mail_to, std::list<std::string> const& cc, curl_slist* recipients ) - { - recipients = curl_slist_append( recipients, mail_to.c_str() ); - - for( const auto& recipient : cc ) - { - recipients = curl_slist_append( recipients, recipient.c_str() ); - } - - curl_easy_setopt( curl, CURLOPT_MAIL_RCPT, recipients ); - } - - std::string MessageSender::GetHostPortData() const - { - auto result = "smtp://" + mSettingsStorage.host; - if( !mSettingsStorage.port.empty() ) - { - result += ":" + mSettingsStorage.port; - } - return result; - } - - size_t MessageSender::ReadCallBack( void *ptr, size_t size, size_t nmemb, void *userp ) - { - struct WriteThis *pooh = reinterpret_cast<WriteThis*>( userp ); - if( size * nmemb < 1 ) - { - return 0; - } - if( pooh->counter++ > 0 ) - { - return 0; - } - memcpy( ptr, mText.c_str(), mText.size() ); - return mText.size(); - } -} |