-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_email.php
executable file
·207 lines (178 loc) · 5.14 KB
/
send_email.php
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
<?php
/**
* send_email.php
*
* Simple SMTP relay API for my projects where I can't use SMTP directly due to server/firewall limitations.
*
* @author Donat Marko
* @copyright 2020 Donatus
* @license GNU GPLv3 https://www.gnu.org/licenses/gpl-3.0.en.html
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'config-inc.php';
require_once 'vendor/autoload.php';
require_once 'classes/common.php';
require_once 'classes/db.php';
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$sql = new mysqli(SQL_SERVER, SQL_USERNAME, SQL_PASSWORD, SQL_DATABASE);
$db = new DB($sql);
/**
* Unifies the variously formatted to, cc, bcc objects.
* Possible forms:
* * "[email protected]"
* * ["[email protected]", "[email protected]"]
* * {"name": "test", "email": "[email protected]"}
* * {"email": "[email protected]"}
* * [{"email": "[email protected]"}, {"email": "[email protected]"}]
* * [{"name": "test", "email": "[email protected]"}, {"name": "test2", "email": "[email protected]"}]
* @param DB database instance
* @return string JSON formatted output
*/
function get_recipient_objects($data)
{
$list = [];
if (is_array($data) && !Common::IsAssociative($data))
{
foreach ($data as $elem)
{
if (is_string($elem))
{
$list[] = [
'name' => $elem,
'email' => $elem
];
}
else if (!property_exists($elem, 'name'))
{
$list[] = [
'name' => $elem->email,
'email' => $elem->email
];
}
else
{
$list[] = [
'name' => $elem->name,
'email' => $elem->email
];
}
}
}
else if (is_string($data))
{
$list[] = [
'name' => $data,
'email' => $data
];
}
else if (!property_exists($data, 'name'))
{
$list[] = [
'name' => $data->email,
'email' => $data->email
];
}
else
{
$list[] = [
'name' => $data->name,
'email' => $data->email
];
}
return $list;
}
/**
* The API itself which does the rest of the job.
* @param DB database instance
* @return string JSON formatted output
*/
function send_email($db)
{
// don't do anything without the presence of HTTP_USER_AGENT
if (!array_key_exists('HTTP_USER_AGENT', $_SERVER))
return ['error_code' => 4, 'error_text' => 'invalid header'];
// retrieve API key data from database
$apikeydata = $db->GetAPIkeyData($_POST['apikey']);
// API key not exist
if (!$apikeydata)
return ['error_code' => 1, 'error_text' => 'wrong API key'];
// IP restricted API key
if (!empty($apikeydata->ip) && !preg_match($apikeydata->ip, $_SERVER['REMOTE_ADDR']))
return ['error_code' => 403, 'error_text' => 'forbidden'];
// missing mail in request
if (!isset($_POST['mail']))
return ['error_code' => 2, 'error_text' => 'mail missing'];
$mail = json_decode($_POST['mail']);
$subject = isset($mail->subject) ? $mail->subject : '';
$body = isset($mail->body) ? $mail->body : '';
$isHTML = isset($mail->isHTML) ? $mail->isHTML == true : false;
$altBody = isset($mail->altBody) ? $mail->altBody : '';
$charset = isset($mail->charset) ? $mail->charset : 'UTF-8';
$from = isset($mail->from) ? $mail->from : null;
$tos = isset($mail->to) ? $mail->to : null;
$ccs = isset($mail->cc) ? $mail->cc : null;
$bccs = isset($mail->bcc) ? $mail->bcc : null;
$cfg_smtp = new PHPMailer(true);
try
{
$cfg_smtp->SMTPDebug = 0;
$cfg_smtp->isSMTP();
$cfg_smtp->Host = SMTP_SERVER;
$cfg_smtp->SMTPAuth = SMTP_AUTH;
$cfg_smtp->Username = SMTP_USERNAME;
$cfg_smtp->Password = SMTP_PASSWORD;
$cfg_smtp->SMTPSecure = SMTP_SECURE;
$cfg_smtp->Port = SMTP_PORT;
$cfg_smtp->CharSet = $charset;
// missing sender
if ($from === null)
return ['error_code' => 4, 'error_text' => 'sender is missing'];
$from = get_recipient_objects($from)[0];
$cfg_smtp->setFrom($from['email'], $from['name']);
// missing recepient
if ($tos === null)
return ['error_code' => 3, 'error_text' => 'recipient is missing'];
foreach (get_recipient_objects($tos) as $to)
{
$cfg_smtp->addAddress($to['email'], $to['name']);
}
// adding Carbon Copy
if ($ccs !== null)
{
foreach (get_recipient_objects($ccs) as $cc)
{
$cfg_smtp->addCC($cc['email'], $cc['name']);
}
}
// adding Blind Carbon Copy
if ($bccs !== null)
{
foreach (get_recipient_objects($bccs) as $bcc)
{
$cfg_smtp->addBCC($bcc['email'], $bcc['name']);
}
}
$cfg_smtp->isHTML($isHTML);
$cfg_smtp->Subject = $subject;
$cfg_smtp->Body = $body;
$cfg_smtp->AltBody = $isHTML ? $altBody : $body;
$cfg_smtp->send();
return ['error_code' => 0, 'error_text' => 'success'];
}
catch (Exception $e)
{
// error in SMTP process
return ['error_code' => 99, 'error_text' => $cfg_smtp->ErrorInfo];
}
}
header('Content-Type: application/json');
if (!empty($_POST) && isset($_POST['apikey']) && isset($_POST['mail']))
{
$result = json_encode(send_email($db));
$db->Log($_POST['apikey'], $_POST['mail'], $result);
echo $result;
}
$sql->close();
?>