-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_emails.php
82 lines (69 loc) · 2.37 KB
/
send_emails.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
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mailList = [];
$names = [];
if (isset($_FILES['mailFile']) && $_FILES['mailFile']['error'] === UPLOAD_ERR_OK) {
$fileTmpPath = $_FILES['mailFile']['tmp_name'];
$fileContent = file($fileTmpPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($fileContent as $line) {
list($email, $name) = explode(',', $line);
$mailList[] = trim($email);
$names[] = trim($name);
}
}
if (isset($_POST['emails']) && !empty($_POST['emails'])) {
$emails = explode(',', $_POST['emails']);
$emails = array_map('trim', $emails);
$mailList = array_merge($mailList, $emails);
$postedNames = array_fill(0, count($emails), '');
$names = array_merge($names, $postedNames);
}
if (empty($mailList)) {
die(json_encode(['status' => 'completed', 'message' => 'Hiçbir mail adresi girilmedi veya yüklenmedi.']));
}
$subject = isset($_POST['subject']) ? $_POST['subject'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$mail = new PHPMailer(true);
$totalEmails = count($mailList);
$sentEmails = 0;
try {
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'mail.yourmailserver.com';
$mail->SMTPAuth = true;
$mail->Username = 'mail@yourdomain.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('mail@yourdomain.com', 'Sender Name');
$mail->isHTML(true);
$mail->Charset = 'UTF-8';
$mail->Subject = $subject;
foreach ($mailList as $index => $recipient) {
$personalizedMessage = str_replace('{{name}}', $names[$index], $message);
$mail->Body = $personalizedMessage;
$mail->addAddress($recipient);
$success = $mail->send();
$mail->clearAddresses();
$sentEmails++;
echo json_encode([
'status' => 'progress',
'email' => $recipient,
'success' => $success
]);
ob_flush();
flush();
}
echo json_encode([
'status' => 'completed',
'message' => "Tüm mailler gönderildi. ($sentEmails / $totalEmails)"
]);
} catch (Exception $e) {
echo json_encode([
'status' => 'completed',
'message' => "Mail gönderilemedi. Hata: {$mail->ErrorInfo}"
]);
}
?>