Merhaba,
Aşağıdaki toplu mail gönderme sisteminin normal tekli olarak gönderecek şekilde yaptım sorunsuz çalışmaktaydı. Toplu mail olarak sistemi güncellediğimde gene sorunsuz çalışmakta fakat 120 saniye sonra duruyor ve hata olarak İstek yapılamadı/The request could not be made. hatası atmakta. Cron jobla v.b biraz deneme yaptım fakat sorunu çözemedim. Sorunu çözmek ve 120 saniye’nin üstüne çıkmak için ne yapabiliriz, nasıl bir yol izleyebiliriz.
Teşekkürler.
send_mail.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require '../vendor/phpmailer/src/Exception.php';
require '../vendor/phpmailer/src/PHPMailer.php';
require '../vendor/phpmailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$response = array('success' => false, 'message' => '');
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES['file'])) {
$fileTmpPath = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$fileNameCmps = explode(".", $fileName);
$fileExtension = strtolower(end($fileNameCmps));
$allowedfileExtensions = array('txt');
if (in_array($fileExtension, $allowedfileExtensions)) {
$recipients = [];
$file = fopen($fileTmpPath, "r");
if ($file) {
while (($line = fgets($file)) !== false) {
$recipients[] = trim($line);
}
fclose($file);
} else {
$response['message'] = "Dosya açılamadı.";
header('Content-Type: application/json');
echo json_encode($response);
exit;
}
$subject = $_POST['subject'];
$message = $_POST['message'];
$delay = isset($_POST['delay']) ? (int)$_POST['delay'] : 0;
$mail = new PHPMailer(true);
try {
// SMTP Ayarları
$mail->isSMTP();
$mail->Host = 'smtp.ornek.com'; // SMTP
$mail->SMTPAuth = true;
$mail->Username = 'deneme@ornek.net';
$mail->Password = 'ornek';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587; // TLS
$mail->setFrom('deneme@ornek.net', 'ornek');
foreach ($recipients as $recipient) {
$mail->clearAddresses(); // Önceki adresleri temizle
$mail->addAddress($recipient); // Her alıcıyı ekle
// İçerik
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = strip_tags($message);
$mail->send();
// Bekleme süresi
if ($delay > 0) {
sleep($delay);
}
}
$response['success'] = true;
$response['message'] = 'E-posta başarıyla gönderildi';
} catch (Exception $e) {
$response['message'] = "E-posta gönderilemedi. Hata: {$mail->ErrorInfo}";
}
} else {
$response['message'] = "Sadece .txt uzantılı dosyalara izin verilmektedir.";
}
} else {
$response['message'] = "Dosya yüklenemedi. Lütfen tekrar deneyin.";
}
// JSON olarak cevap gönder
header('Content-Type: application/json');
echo json_encode($response);
?>
index.html
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>E-Posta Gönder</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
max-width: 500px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h2 {
text-align: center;
color: #333;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 10px;
color: #555;
}
input[type="text"],
textarea,
input[type="file"],
input[type="number"] {
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px;
border: none;
border-radius: 5px;
background-color: #5cb85c;
color: #fff;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #4cae4c;
}
</style>
</head>
<body>
<div class="container">
<h2>E-Posta Gönder</h2>
<form id="emailForm" action="send_mail.php" method="post" enctype="multipart/form-data">
<label for="file">Alıcı E-posta Dosyası (TXT formatında):</label>
<input type="file" id="file" name="file" accept=".txt" required>
<label for="subject">Konu:</label>
<input type="text" id="subject" name="subject" required>
<label for="message">Mesaj:</label>
<textarea id="message" name="message" rows="4" required></textarea>
<label for="delay">Her bir mail gönderimi arasında bekleme süresi (saniye cinsinden):</label>
<input type="number" id="delay" name="delay" min="0" value="0" required>
<button type="submit">Gönder</button>
</form>
</div>
<!-- jQuery ve jQuery UI kütüphaneleri -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
// Form submit olayı
$('#emailForm').on('submit', function(event) {
event.preventDefault(); // Formun otomatik olarak submit olmasını engelle
var form = this;
var formData = new FormData(form);
$.ajax({
type: $(form).attr('method'),
url: $(form).attr('action'),
data: formData,
contentType: false,
processData: false,
dataType: 'json',
success: function(response) {
if (response.success) {
$('<div>').appendTo('body')
.html('<p>' + response.message + '</p>')
.dialog({
modal: true,
title: 'Başarılı!',
zIndex: 10000,
autoOpen: true,
width: 'auto',
resizable: false,
buttons: {
Ok: function() {
$(this).dialog("close");
form.reset();
}
},
close: function(event, ui) {
$(this).remove(); // Dialog kapatıldığında div'i kaldır
}
});
} else {
// Hata mesajı
alert('E-posta gönderilirken bir hata oluştu:\n' + response.message);
}
},
error: function() {
alert('Sunucu hatası: İstek yapılamadı.');
}
});
});
});
</script>
</body>
</html>