-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
send_email.php
384 lines (279 loc) · 12.6 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
include("database.php");
include("encryption.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require dirname(__FILE__) . '/lib/PHPMailer-master/src/Exception.php';
require dirname(__FILE__) . '/lib/PHPMailer-master/src/PHPMailer.php';
require dirname(__FILE__) . '/lib/PHPMailer-master/src/SMTP.php';
// Send Email Function
// Parameters:
// cust_id = Customer ID
// flag = Email Sent Flag (0 = Not Sent, 1 = Sent)
// type = Type of Sent ('Single' = Single Email, 'Mass' = Mass Email)
function sendEmail($cust_id, $flag, $type)
{
// Get SQL Connection
global $conn;
// Check if Send Flag is set
if ($flag) {
$variables = array();
$email_build = array();
// Select columns to Build the Email
$sql_query = "SELECT
comp_title,
comp_subtitle,
comp_owner,
comp_address,
comp_phone,
comp_email,
comp_google_place_id,
comp_google_url,
comp_facebook_url,
comp_twitter_url,
comp_linkedin_url,
comp_instagram_url,
comp_youtube_url,
comp_amazon_url,
comp_pinterest_url,
comp_etsy_url,
comp_shopify_url,
mail_smtp,
mail_from,
mail_from_password,
mail_cc,
mail_bcc,
mail_subject,
mail_body,
cust_first_name,
cust_last_name,
cust_email
FROM v_email_build
WHERE cust_id = ?";
$stmnt = mysqli_prepare($conn, $sql_query);
mysqli_stmt_bind_param($stmnt, 'i', $cust_id);
mysqli_stmt_execute($stmnt);
$result_set = mysqli_stmt_get_result($stmnt);
$email_build = mysqli_fetch_assoc($result_set);
// Check if From Email Address or Password is Missing
if (empty($email_build['mail_from']) || empty($email_build['mail_from_password'])) {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => 'Your Company does not have an Email Address or Password to use as a Sender!<br>Please add an Email Address in the Configure Menu and try again.');
// Return the error as a JSON encoded string
echo json_encode($error);
exit;
// Check if From Email Address is in a Valid Format
} else if (!filter_var($email_build['mail_from'], FILTER_VALIDATE_EMAIL)) {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => 'Your Sender Email Address format is invalid!<br>Please adjust the Email Address in the Email Template Menu and try again (It should be in a format of [email protected]).');
// Return the error as a JSON encoded string
echo json_encode($error);
exit;
// Check if Email Subject, Body, or SMTP Address is Missing
} else if (empty($email_build['mail_subject']) || empty($email_build['mail_body']) || empty($email_build['mail_smtp'])) {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => 'Your Company does not have an Email Subject, Body, or SMTP Server!<br>Please add an Email Address in the Email Template Menu and try again.');
// Return the error as a JSON encoded string
echo json_encode($error);
exit;
// Check if Customer Email Address is Missing
} else if (empty($email_build['cust_email'])) {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => 'The Customer you\'re trying to send an Email to is missing an Email Address!<br>Please add an Email Address and try again.');
// Return the error as a JSON encoded string
echo json_encode($error);
exit;
// Check if Customer Email Address is in a Valid Format
} else if (!filter_var($email_build['cust_email'], FILTER_VALIDATE_EMAIL)) {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => 'The Customers Email Address format is invalid!<br>Please adjust the Email Address and try again (It should be in a format of [email protected]).');
// Return the error as a JSON encoded string
echo json_encode($error);
exit;
} else {
// Set Variables
$variables['customer_first_name'] = $email_build['cust_first_name'];
$variables['customer_last_name'] = $email_build['cust_last_name'];
$variables['customer_email'] = $email_build['cust_email'];
$variables['company_title'] = $email_build['comp_title'];
$variables['company_subtitle'] = $email_build['comp_subtitle'];
$variables['company_owner'] = $email_build['comp_owner'];
$variables['company_address'] = $email_build['comp_address'];
$variables['company_phone'] = $email_build['comp_phone'];
$variables['company_email'] = $email_build['comp_email'];
$variables['company_google_place_id'] = $email_build['comp_google_place_id'];
$variables['url_google'] = $email_build['comp_google_url'];
$variables['url_facebook'] = $email_build['comp_facebook_url'];
$variables['url_twitter'] = $email_build['comp_twitter_url'];
$variables['url_linkedin'] = $email_build['comp_linkedin_url'];
$variables['url_instagram'] = $email_build['comp_instagram_url'];
$variables['url_youtube'] = $email_build['comp_youtube_url'];
$variables['url_amazon'] = $email_build['comp_amazon_url'];
$variables['url_pinterest'] = $email_build['comp_pinterest_url'];
$variables['url_etsy'] = $email_build['comp_etsy_url'];
$variables['url_shopify'] = $email_build['comp_shopify_url'];
// Set Email Subject and Body
$email_subject = $email_build['mail_subject'];
$email_body = htmlspecialchars_decode($email_build['mail_body'] ?? '');
// Replace Template Variables with Database Values
foreach ($variables as $key => $value) {
$email_subject = str_replace('{{ ' . $key . ' }}', $value, $email_subject);
$email_body = str_replace('{{ ' . $key . ' }}', $value, $email_body);
}
// Send Verification Email
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Decrypt Email Password
$decrypted_password = encrypt_decrypt('decrypt', $email_build['mail_from_password']);
$mail->SMTPDebug = false; // Enable verbose debug output
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = $email_build['mail_smtp']; //SMTP Host
$mail->Username = $email_build['mail_from']; //Email User
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Password = $decrypted_password; // Email Password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465; // Port
$mail->setFrom($email_build['mail_from'], $email_build['comp_title']); // From Email Address
// Split the string into an array using the semicolon as the delimiter
$emails_cc = explode(';', $email_build['mail_cc']);
// Loop through the CC array and process each email address
foreach ($emails_cc as $email_cc) {
// Trim any leading or trailing whitespace from the email address
if ($email_cc == '') continue;
$email_cc = trim($email_cc);
$mail->addCC($email_cc);
}
// Split the string into an array using the semicolon as the delimiter
$emails_bcc = explode(';', $email_build['mail_bcc']);
// Loop through the BCC array and process each email address
foreach ($emails_bcc as $email_bcc) {
if ($email_bcc == '') continue;
// Trim any leading or trailing whitespace from the email address
$email_bcc = trim($email_bcc);
$mail->addBCC($email_bcc);
}
$mail->addAddress($email_build['cust_email']); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email_subject; // Set Email Subject
$mail->Body = $email_body; // Set Email Body
//$mail->AltBody='This is the body in plain text for non-HTML mail clients'; // Set Plain Text Email Body
// Send Email
if ($mail->Send()) {
// Update Single/Mass Customer Flag = 1
$sql_query = "UPDATE customers
SET cust_email_sent = ?
WHERE cust_id = ?";
$stmnt = mysqli_prepare($conn, $sql_query);
mysqli_stmt_bind_param($stmnt, 'ii', $flag, $cust_id);
mysqli_stmt_execute($stmnt);
if (mysqli_stmt_affected_rows($stmnt) > 0) {
if ($type == 'Single') {
http_response_code(200);
header('Content-Type: application/json');
$message = array("message" => "Successfully Sent Email!");
// Return the message as a JSON encoded string
echo json_encode($message);
}
} else {
if ($type == 'Single') {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = mysqli_stmt_error($stmnt);
// Return the error as a JSON encoded string
echo json_encode($error);
}
}
} else {
if ($type == 'Single') {
//echo "Error while sending Email.";
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => 'Error Sending Email!');
// Return the error as a JSON encoded string
echo json_encode($error);
}
}
} catch (Exception $e) {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => '' . $mail->ErrorInfo . '');
// Return the error as a JSON encoded string
echo json_encode($error);
exit;
}
}
} else {
// Update Single Customer Flag = 0
$sql_query = "UPDATE customers
SET cust_email_sent = ?
WHERE cust_id = ?";
$stmnt = mysqli_prepare($conn, $sql_query);
mysqli_stmt_bind_param($stmnt, 'ii', $flag, $cust_id);
mysqli_stmt_execute($stmnt);
if (mysqli_stmt_affected_rows($stmnt) > 0) {
http_response_code(200);
header('Content-Type: application/json');
$message = array("message" => "Successfully Removed Flag!");
// Return the message as a JSON encoded string
echo json_encode($message);
} else {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = mysqli_stmt_error($stmnt);
// Return the error as a JSON encoded string
echo json_encode($error);
}
}
}
// Main Switch
switch ($_POST["command"]) {
// Send Single Email
case $case = "sendEmail";
$cust_id = $_POST["cust_id"];
$flag = $_POST["flag"];
// Call Send Email Function
sendEmail($cust_id, $flag, 'Single');
break;
case $case = "sendMassEmail";
// Retrieve all valid Email Addresses
$sql_query = "SELECT
cust_id
FROM v_email_build
WHERE cust_email != ''
AND (cust_email REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$')";
$stmnt = mysqli_prepare($conn, $sql_query);
mysqli_stmt_execute($stmnt);
$result_set = mysqli_stmt_get_result($stmnt);
// Check if the query returned any rows
if (mysqli_num_rows($result_set) > 0) {
// Loop through the rows of the result set
while ($row = mysqli_fetch_assoc($result_set)) {
// Access the columns of the row as variables
$cust_id = $row['cust_id'];
// Call Send Email Function for each row
sendEmail($cust_id, 1, 'Mass');
}
} else {
header('HTTP/1.1 400 Bad Request');
header('Content-Type: application/json');
$error = array('error' => 'No Customer Emails Found!');
// Return the error as a JSON encoded string
echo json_encode($error);
exit;
}
break;
//Update Email Flags
case $case = "updateEmailFlags";
// Update Email Flags
$sql_query = "UPDATE customers
SET cust_email_sent = 0";
$stmnt = mysqli_prepare($conn, $sql_query);
mysqli_stmt_execute($stmnt);
break;
}