This repository was archived by the owner on Jun 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsendemail.cpp
More file actions
195 lines (161 loc) · 5.1 KB
/
Copy pathsendemail.cpp
File metadata and controls
195 lines (161 loc) · 5.1 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
#include "sendemail.h"
#include "config.h"
#include "util.h"
#include <Wt/Json/Array>
#include <Wt/Json/Object>
#include <Wt/Json/Parser>
#include <Wt/Json/Value>
#include <fstream>
#include <iostream>
#include <curl/curl.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
/* This file is mostly copied from http://curl.haxx.se/libcurl/c/smtp-tls.html */
SendEmail::SendEmail(): mailHeaderToFrom(""), mailHeaderContentHTML(""), mailHeaderContentPLAIN("")
{
/* Generate mail headers */
Config* conf = Config::getConfig();
size_t recipientsCount = conf->recipients().size();
for (size_t i = 0; i < recipientsCount; ++i) {
mailHeaderToFrom += "Bcc: " + conf->recipients()[i].toUTF8() + "\n";
}
mailHeaderToFrom += "From: " + conf->from().toUTF8() + "\n";
mailHeaderContentHTML = "Mime-version: 1.0\n";
mailHeaderContentHTML += "Content-Type: text/html; charset=\"UTF-8\"\n";
mailHeaderContentHTML += "Content-Transfer-Encoding: 8bit\n";
mailHeaderContentHTML += "\n";
mailHeaderContentPLAIN = "Content-Type: text/plain; charset=\"UTF-8\"\n";
mailHeaderContentPLAIN += "\n";
}
size_t SendEmail::payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
SendEmail *_this = (SendEmail *)userp;
if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1))
return 0;
_this->emailBuffer.read((char *)ptr, size * nmemb);
return _this->emailBuffer.gcount();
}
bool SendEmail::sendUsingCurl(const std::string &content)
{
#ifndef SEND_EMAIL
UNUSED(content);
return false;
#else
CURL *curl;
CURLcode res;
struct curl_slist *curl_recipients = NULL;
Config* conf = Config::getConfig();
emailBuffer.str(content);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, conf->smtp_server().toUTF8().c_str());
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_USERNAME, conf->login().toUTF8().c_str());
curl_easy_setopt(curl, CURLOPT_PASSWORD, conf->pwd().toUTF8().c_str());
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, conf->from().toUTF8().c_str());
for (size_t i = 0; i < conf->recipients().size(); i++)
curl_recipients = curl_slist_append(curl_recipients, conf->recipients()[i].toUTF8().c_str());
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, curl_recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, this);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* Verbose ? Set to 1 if you want to debug stuff */
curl_easy_setopt(curl, CURLOPT_VERBOSE, conf->verbose()?1L:0L);
/* send the message (including headers) */
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 and clean up */
curl_slist_free_all(curl_recipients);
curl_easy_cleanup(curl);
}
return true;
#endif
}
bool SendEmail::sendUsingLocalMail(const std::string &content)
{
#ifndef SEND_EMAIL
UNUSED(content);
return false;
#else
pid_t pid;
int mailpipe[2];
int returnCode = 0;
ssize_t writeLen = 0;
ssize_t retLen = 0;
ssize_t mailLen = content.length();
if(pipe(mailpipe)){
std::cerr << "sendUsingLocalMail: Failed to create pipe.\n";
return false;
}
pid = fork();
if (pid == (pid_t) 0) {
close(mailpipe[1]);
if (dup2(mailpipe[0], STDIN_FILENO) < 0) {
std::cerr << "sendUsingLocalMail: Failed to dup2.\n";
return false;
}
execl("/usr/bin/sendmail", "/usr/bin/sendmail", "-t", NULL);
std::cerr << "sendUsingLocalMail: Failed to execl.\n";
exit(EXIT_FAILURE);
}
close(mailpipe[0]);
/* write mail content to mailpipe[1] */
while (writeLen < mailLen) {
retLen = write(mailpipe[1], content.c_str() + writeLen, sizeof(char) * (mailLen - writeLen));
if (retLen > 0) {
writeLen += retLen;
} else {
std::cerr << "sendUsingLocalMail: Failed to write the whole mail.\n";
break;
}
}
close(mailpipe[1]);
pid = waitpid(pid, &returnCode, 0);
if ((pid > 0) && (returnCode == 0))
return true;
if (pid > 0)
std::cerr << "sendUsingLocalMail: mail command returned !=0 code.\n";
else
std::cerr << "sendUsingLocalMail: Failed to waitpid().\n";
return false;
#endif
}
bool SendEmail::send(const Wt::WString &title, const Wt::WString &msg, EmailType type, const std::vector<std::string> &recipients, bool warnAdmins)
{
#ifndef SEND_EMAIL
UNUSED(title);
UNUSED(msg);
UNUSED(type);
UNUSED(recipients);
UNUSED(warnAdmins);
return false;
#else
std::string mailBuffer;
Config* conf = Config::getConfig();
if (!conf->isEnabled())
return false;
/* generate the mail */
size_t recipientsCount = recipients.size();
for (size_t i = 0; i < recipientsCount; ++i)
mailBuffer += "Bcc: " + recipients[i] + "\n";
if (warnAdmins)
mailBuffer += mailHeaderToFrom;
else
mailBuffer += "From: " + conf->from().toUTF8() + "\n";
mailBuffer += "Subject: " + title.toUTF8() + "\n";
if (type == HTML) {
mailBuffer += mailHeaderContentHTML;
} else {
mailBuffer += mailHeaderContentPLAIN;
}
mailBuffer += msg.toUTF8();
if (conf->smtp_server() == "local")
return sendUsingLocalMail(mailBuffer);
else
return sendUsingCurl(mailBuffer);
#endif
}