【问题标题】:send an email for each result of mysql query in php为php中mysql查询的每个结果发送一封电子邮件
【发布时间】:2024-04-29 00:55:01
【问题描述】:

我的数据库中有 2 个表,我进行了查询以验证两个表中是否不存在电子邮件地址,列出所有用户,现在电子邮件是我用来验证用户是否唯一的电子邮件。我运行了我的脚本,它运行良好,我做了一个回显,我得到了 10 次迭代(10 个不同的用户,电子邮件不重复)。现在我需要为每次迭代发送一封电子邮件,问题是当我添加我的邮件功能时,我只收到 1 个用户并且只发送了 1 个 emai,错过了 9 个用户。我怎样才能完成 10 个用户,每个用户 10 封电子邮件。

  <?php
 include 'myDB.php'; 

$sql = " query works fine ";
$result = mysqli_query($conn, $sql);

//var_dump($result);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {

  foreach ($row as  $key => $gtm) {
   $message .= $gtm;
    $header ="From: no-reply@testme.com" . "\r\n";
    $para    = 'web2@tesy.com';
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $titulo  = 'Mailing list Newsletter';
    $message = '<html><body>';
    $message .= '<br/> <p>El siguiente usuario abandono el la compra de un paquete en booking hello </p><br/>';
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="2">';
    $message .= "<tr><td><strong>Nombre del paquete:</strong> </td><td>" . $row["nombre_del_paquete"]."</td></tr>";
    $message .= "<tr><td><strong>Precio Total del paquete:</strong> </td><td>". $row["precio_total"]."</td></tr>";
    $message .= "<tr><td><strong>Nombre:</strong> </td><td>" . $row["nombre"]. "</td></tr>";
    $message .= "<tr><td><strong>Apellido:</strong> </td><td>" . $row["apellido"]."</td></tr>";
    $message .= "<tr><td><strong>Email:</strong> </td><td>" . $row["email"]. "</td></tr>";

    $message .= "</table>";
    $message .= "</body></html>";



    if(mail($para, $titulo, $message, $header)){
        echo "recorded successfully";
                die();
    }else{
        echo "false";
    }

    }

 }
 } else {
echo "0 results";
 }


 ?>

现在当我添加邮件功能时,我只获得 1 个用户(第 1 个),如果我删除邮件功能并执行回显,我会获得所有用户(10)

【问题讨论】:

  • 清理你的代码
  • 您仅将其发送到一个电子邮件地址,在 $header 中使用 $row["email"],例如 $header ="To: " 。 $row["email"] 。 "\r\n";

标签: php mysql email


【解决方案1】:

删除die():

foreach ($row as  $key => $gtm) {
    // more cod here:

    // your problem is in this if:
    if(mail($para, $titulo, $message, $header)){
        echo "recorded successfully";
        // die(); this stops the execution
    }else{
        echo "false";
    }
}

【讨论】:

  • 我这样做了,但我收到了 400 封邮件,只有 10 封,如果将来我有 15,20 封邮件,我在哪里做或如何设置休息时间,基于数量用户它停止并且不继续向同一用户发送多个图块
  • 我的第一个问题是:为什么foreachwhile 中。看看这个:pastebin.com/Bsp6wk8k我稍微修改了你的脚本
  • 我做了,我删除了它,它有点乱,但只需删除 die() 它就可以完美地工作,非常感谢
  • 乐于助人 ;)
最近更新 更多