forked from fledge-iot/fledge-notify-email
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmtp-mail.cpp
More file actions
executable file
·224 lines (180 loc) · 6.11 KB
/
smtp-mail.cpp
File metadata and controls
executable file
·224 lines (180 loc) · 6.11 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include <iostream>
#include <cstring>
#include <vector>
#include <ctime>
#include <curl/curl.h>
#include <email_config.h>
#include <logger.h>
#include "string_utils.h"
using namespace std;
extern "C" {
struct upload_status {
int lines_read;
vector<std::string>* payload;
};
char *getCurrTime()
{
time_t rawtime;
struct tm * timeinfo;
static char buffer[80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer, 80, "%a, %e %G %X %z", timeinfo);
return buffer;
}
void compose_payload(vector<std::string>* &payload, const EmailCfg *emailCfg, const char* msg)
{
payload->push_back("Date: " + string(getCurrTime()) + "\r\n");
// Parse address and name to compose CC pairs for payload
if (emailCfg->email_to.size())
{
std::string ToList = {"To: "};
for(int i = 0; i < emailCfg->email_to.size(); i++ )
{
if (i > 0)
{
ToList.append(",");
}
ToList.append(emailCfg->email_to_name[i] + " <" + emailCfg->email_to[i] + ">");
}
ToList.append(" \r\n");
payload->push_back(ToList);
}
if (emailCfg->email_cc.size())
{
std::string CCList = {"CC: "};
for(int i = 0; i < emailCfg->email_cc.size(); i++ )
{
if (i > 0)
{
CCList.append(",");
}
CCList.append(emailCfg->email_cc_name[i] + " <" + emailCfg->email_cc[i] + ">");
}
CCList.append(" \r\n");
payload->push_back(CCList);
}
// Do not add BCC payload otherwise it will be visible to all the recipients
payload->push_back("From: " + emailCfg->email_from_name + " <" + emailCfg->email_from + "> \r\n");
payload->push_back("Subject: " + emailCfg->subject + "\r\n");
payload->push_back("\r\n");
payload->push_back(msg);
payload->push_back("\r\n");
}
static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
struct upload_status *upload_ctx = (struct upload_status *)userp;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
if (upload_ctx->lines_read >= upload_ctx->payload->size())
return 0;
std::string s = (*upload_ctx->payload)[upload_ctx->lines_read];
size_t len = s.length();
memcpy(ptr, s.c_str(), len);
upload_ctx->payload->erase(upload_ctx->payload->begin()+upload_ctx->lines_read);
return len;
}
int sendEmailMsg(const EmailCfg *emailCfg, const char *msg)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
upload_ctx.payload = new vector<std::string>;
compose_payload(upload_ctx.payload, emailCfg, msg);
curl = curl_easy_init();
if(curl) {
if(emailCfg->use_ssl_tls)
{
/* Set username and password */
curl_easy_setopt(curl, CURLOPT_USERNAME, emailCfg->username.c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, emailCfg->password.c_str());
}
/* This is the URL for your mailserver */
string proto = "";
if (emailCfg->server.find("smtp://") == std::string::npos) proto = "smtp://";
string server_url = proto + emailCfg->server + ":" + to_string(emailCfg->port);
curl_easy_setopt(curl, CURLOPT_URL, server_url.c_str());
/* We'll start with a plain text connection, and upgrade
* to Transport Layer Security (TLS) using the STARTTLS command. */
if(emailCfg->use_ssl_tls)
{
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
//curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
}
string email_from = "<" + emailCfg->email_from + ">";
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, email_from.c_str());
// Parse CC and BCC list to append recipients
if (emailCfg->email_to.size())
{
for(auto it = emailCfg->email_to.begin(); it != emailCfg->email_to.end(); it++ )
{
string email_to = "<" + *it + ">";
recipients = curl_slist_append(recipients, email_to.c_str() );
}
}
if (emailCfg->email_cc.size())
{
for(auto it = emailCfg->email_cc.begin(); it != emailCfg->email_cc.end(); it++ )
{
string email_cc = "<" + *it + ">";
recipients = curl_slist_append(recipients, email_cc.c_str() );
}
}
if (emailCfg->email_bcc.size())
{
for(auto it = emailCfg->email_bcc.begin(); it != emailCfg->email_bcc.end(); it++ )
{
string email_bcc = "<" + *it + ">";
recipients = curl_slist_append(recipients, email_bcc.c_str() );
}
}
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
/* We're using a callback function to specify the payload (the headers and
* body of the message). You could just use the CURLOPT_READDATA option to
* specify a FILE pointer to read from. */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
/* Send the message */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* Free the list of recipients */
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
delete upload_ctx.payload;
return (int)res;
}
const char *errorString(int result)
{
return curl_easy_strerror((CURLcode)result);
}
};