1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#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();
}
}
|