| Current Path : /home/happyrenas/find.myreco.online/cron/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/happyrenas/find.myreco.online/cron/send_one_email.php |
<?php
// cron/send_one_email.php
// À appeler toutes les minutes. Envoie 1 seul email de email_queue.
ini_set('display_errors', '0');
error_reporting(E_ALL);
//header('Content-Type: text/plain; charset=utf-8');
set_time_limit(50);
require_once __DIR__ . '/../configuration.php';
require_once __DIR__ . '/../includes/fonctions.php';
echo "<hr>[" . date('Y-m-d H:i:s');
echo '<meta http-equiv="refresh" content="300">';
// Ici, on suppose que $db (ezSQL) existe déjà via tes includes.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once __DIR__ . '/../PHPMailer-6.9.1/src/Exception.php';
require_once __DIR__ . '/../PHPMailer-6.9.1/src/PHPMailer.php';
require_once __DIR__ . '/../PHPMailer-6.9.1/src/SMTP.php';
/* ===========================
SMTP Gmail (mets ça en config idéalement)
=========================== */
$SMTP_HOST = 'smtp.gmail.com';
$SMTP_PORT = 587; // 587 STARTTLS
$SMTP_USER = 'contact@myreco.online';
$SMTP_PASS = 'ibvt ptcg euex mmnp'; // recommandé
$SMTP_SECURE = 'tls'; // 'tls' pour 587, 'ssl' pour 465
$MAX_RETRIES = 6;
$BACKOFF_BASE = 60; // 60s, 120s, 240s...
/* ===========================
1) Prendre 1 mail à envoyer
=========================== */
$row = $db->get_row("
SELECT *
FROM email_queue
WHERE status='queued'
AND (next_attempt IS NULL OR next_attempt <= NOW())
ORDER BY id ASC
LIMIT 1
");
if (!$row) {
echo "OK: nothing queued\n";
exit;
}
$id = (int)$row->id;
/* ===========================
2) Le “claim” : passer en sending
(évite double envoi si 2 cron se chevauchent)
=========================== */
$db->query("
UPDATE email_queue
SET status='sending', last_attempt=NOW()
WHERE id=$id AND status='queued'
LIMIT 1
");
// si pas de ligne affectée => quelqu’un d’autre l’a prise
if (isset($db->rows_affected) && (int)$db->rows_affected === 0) {
echo "SKIP: already claimed\n";
exit;
}
/* ===========================
3) Préparer les champs
=========================== */
$from = trim((string)$row->expediteur);
$to = trim((string)$row->destinataire_principal);
//$to="philippe-jean@outlook.fr";
$cc = trim((string)($row->destinataire_copie ?? ''));
$subj = (string)$row->objet;
$html = (string)$row->message;
if ($to === '' || !filter_var($to, FILTER_VALIDATE_EMAIL)) {
$db->query("
UPDATE email_queue
SET status='failed',
error_message='".$db->escape("Destinataire invalide: $to")."',
retries=retries+1,
next_attempt=NULL,
last_attempt=NOW()
WHERE id=$id
LIMIT 1
");
echo "FAIL: invalid to\n";
exit;
}
if ($from === '' || !filter_var($from, FILTER_VALIDATE_EMAIL)) {
// fallback si expediteur invalide
$from = $SMTP_USER;
}
/* ===========================
4) Envoi SMTP via PHPMailer
=========================== */
try {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = $SMTP_HOST;
$mail->SMTPAuth = true;
$mail->Username = $SMTP_USER;
$mail->Password = $SMTP_PASS;
$mail->Port = (int)$SMTP_PORT;
$mail->CharSet = 'UTF-8';
if ($SMTP_SECURE === 'ssl') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
} else {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
}
$mail->setFrom($from, 'MyReco');
$mail->addAddress($to);
// CC optionnel : "a@b.com,c@d.com"
if ($cc !== '') {
$parts = preg_split('/[,\s;]+/', $cc);
foreach ($parts as $p) {
$p = trim($p);
if ($p && filter_var($p, FILTER_VALIDATE_EMAIL)) {
$mail->addCC($p);
}
}
}
$mail->Subject = $subj;
$mail->isHTML(true);
$mail->Body = $html;
$mail->AltBody = trim(strip_tags(str_replace(["<br>","<br/>","<br />"], "\n", $html)));
$mail->send();
// OK => sent
$db->query("
UPDATE email_queue
SET status='sent',
error_message=NULL,
sent_at=NOW(),
next_attempt=NULL,
last_attempt=NOW()
WHERE id=$id
LIMIT 1
");
echo "SENT: id=$id to=$to\n";
exit;
} catch (Exception $e) {
$err = "PHPMailer: " . $e->getMessage();
if (strlen($err) > 1900) $err = substr($err, 0, 1900);
$retries = (int)($row->retries ?? 0) + 1;
if ($retries >= $MAX_RETRIES) {
$db->query("
UPDATE email_queue
SET status='failed',
retries=$retries,
error_message='".$db->escape($err)."',
last_attempt=NOW(),
next_attempt=NULL
WHERE id=$id
LIMIT 1
");
echo "FAILED: id=$id (max retries)\n";
exit;
}
// backoff
$delay = (int)($BACKOFF_BASE * (2 ** max(0, $retries - 1)));
if ($delay > 3600) $delay = 3600;
$next = date('Y-m-d H:i:s', time() + $delay);
$db->query("
UPDATE email_queue
SET status='queued',
retries=$retries,
error_message='".$db->escape($err)."',
last_attempt=NOW(),
next_attempt='".$db->escape($next)."'
WHERE id=$id
LIMIT 1
");
echo "RETRY: id=$id retries=$retries next=$next\n";
exit;
}