【问题标题】:Send multiple emails from using PHP and MySQL使用 PHP 和 MySQL 发送多封电子邮件
【发布时间】:2016-12-21 10:31:57
【问题描述】:

我在从 MySQL 数据库向地址子集发送电子邮件时遇到问题。

它应该发送 3 封电子邮件(email1、email2、email3),但它只是发送第一封(email1)。

<?php
    $query_iscritti = "SELECT * FROM richieste WHERE negozio LIKE 'NOMENEGOZIO'";
    $iscritti = mysql_query($query_iscritti, $conn) or die(mysql_error());
    $row_iscritti = mysql_fetch_assoc($iscritti);
    $totalRows_iscritti = mysql_num_rows($iscritti);
    $mittente = $_POST['mittente'];
    $destinatario = $row_iscritti['email'];
    $oggetto = $_POST['oggetto'];
    $messaggio = nl2br($_POST['messaggio']);
    $allegato = $_FILES['allegato']['tmp_name'];
    $allegato_type = $_FILES['allegato']['type'];
    $allegato_name = $_FILES['allegato']['name'];
    $headers = "From: " . $mittente;
    $msg = "";
    if (is_uploaded_file($allegato)) {
        $file = fopen($allegato,'rb');
        $data = fread($file, filesize($allegato));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
        $headers .= "\nMIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/mixed;\n";
        $headers .= " boundary=\"{$mime_boundary}\"";
        $msg .= "This is a multi-part message in MIME format.\n\n";
        $msg .= "--{$mime_boundary}\n";
        $msg .= "Content-Type: text/html; charset=\"UTF-8\"\n";
        $msg .= "Content-Transfer-Encoding: 7bit\n\n";
        $msg .= $messaggio . "\n\n";
        $msg .= "--{$mime_boundary}\n";
        $msg .= "Content-Disposition: attachment; filename=\"{$allegato_name}\"\n";
        $msg .= "Content-Transfer-Encoding: base64\n\n";
        $msg .= $data . "\n\n";
        $msg .= "--{$mime_boundary}--\n";
    } else {
        $msg = $messaggio;
    }
    if (mail($destinatario, $oggetto, $msg, $headers)) {
    } else {
        print "Errore";
    }
    while ($row_iscritti = mysql_fetch_assoc($iscritti)); 
    echo ("EMAIL INVIATE CORRETTAMENTE");
?>

我认为问题出在while 循环中。

【问题讨论】:

  • 请注意,代码缩进不仅仅提供视觉上的乐趣。如果您像您一样共享代码,其他人能够理解您的代码很重要。
  • 一开始只取一行,最后的循环是完全空的。
  • 一般注意事项:您使用的是过时、不安全且已弃用的 mysql_...() 函数。 php 文档中明确指出了这个问题。您应该移植您的代码并开始使用mysqliPDO 现在。否则,一旦您升级 php,您的代码将停止工作。
  • 您正在将标题添加为邮件正文的一部分 - 即:Content-Type: 等是标题
  • 你的mysql服务器连接在哪里? (即$conn

标签: php mysql email


【解决方案1】:

我解决了! 我换了

while ($row_iscritti = mysql_fetch_assoc($iscritti));

while ($row_iscritti = mysql_fetch_assoc($iscritti))
mail($row_iscritti['email'], $oggetto, $msg, $headers);

【讨论】:

    【解决方案2】:

    var_dump $row_iscritti 并检查你从数据库中得到了什么。如果您有一系列电子邮件,那么您需要循环邮件功能。如果仍有问题,请在此处评论转储结果

    foreach ($destinatario as $email)
    {
        mail($email....)
    }
    

    编辑: 您在第 6 行 ($destinario) 收到电子邮件行,然后在此处发送电子邮件:

    if (mail($destinatario, $oggetto, $msg, $headers))
    

    那么你已经开始循环了:

    while ($row_iscritti = mysql_fetch_assoc($iscritti)); 
    

    做对了,检查你有什么:

    while ($row_iscritti = mysql_fetch_assoc($iscritti))
    {
        var_dump($row_inscritti);
    }
    

    【讨论】:

      【解决方案3】:
      while ($row_iscritti = mysql_fetch_assoc($iscritti))
      {
      var_dump($row_inscritti);
      }
      

      dump 为我提供了包含 MySQL 数据库所有值的正确数组

      【讨论】:

        猜你喜欢
        • 2011-02-08
        • 1970-01-01
        • 2016-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 2016-07-17
        相关资源
        最近更新 更多