ı have code something like below. I m trying to send emails with some limits, for example if i have 10 email accounts in ‘to_emails[]’ i have to wait 210 seconds for finis of loading page and after that json gives success message to console but i want to send succes message before it and Even users left page i want to keep sending emails.
$to_emails = [];
foreach ($_POST['Gpname'] as $group_name) {
if ($group_name == "hs") {
$query = "SELECT email FROM login";
$result = $con->query($query);
while ($row = $result->fetch_assoc()) {
$to_emails[] = $row['email'];
}
} elseif ($group_name == "BY" || $group_name == "MY") {
$query = "SELECT email FROM login WHERE group_name = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("s", $group_name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$to_emails[] = $row['email'];
}
} else {
$query = "SELECT email FROM login WHERE name = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("s", $group_name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$to_emails[] = $row['email'];
}
}
}
}
$success = "success";
header('Content-Type: application/json');
echo json_encode($success);
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.example.net';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = '';
$mail->SMTPSecure = 'TLS';
$mail->Port = 587;
$mail->CharSet = 'utf-8';
$mail->setFrom('[email protected]', 'IT example');
$limit = 120;
$total_sent = 0;
foreach ($to_emails as $email) {
$mail->addAddress($email);
$subject = "example";
$message = "";
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
$mail->clearAddresses();
if ($total_sent == $limit || $total_sent == count($to_emails)) {
break;
}
$total_sent++;
$interval = 21;
sleep($interval);
}
} catch (Exception $e) {
echo "E-posta gönderme hatası: {$mail->ErrorInfo}";
}
New contributor
Emir Karakutuk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.