邮件只在php内部发送一次循环

时间:2014-12-24 17:37:41

标签: php email

我有一个让我疯狂的简单问题。

查看以下代码;我本质上是试图用数据库中的地址发送群发邮件。

问题是第一条记录只发出一条消息。我已经验证了(通过计数器和文本的连接)循环遍历每个记录。

我错过了什么?

谢谢。

function SendMassMailing($Connection, $MailingID) {

    $select_query = "SELECT `Drafts`.`SentMailingID`, `Drafts`.`FromAddress`, `Drafts`.`BCCFromAddress`, `Drafts`.`AdditionalBCC`, `Categories`.`MailingUserCategoryID`, `Drafts`.`Subject`,`Drafts`.`Message`, `Drafts`.`Notes`, `Drafts`.`DateCreated`
                        FROM `MailingDraftSent` AS `Drafts`
                        INNER JOIN `MailingUserCategories` AS `Categories`
                        ON `Drafts`.`MailingUserCategoryID` = `Categories`.`MailingUserCategoryID`
                        WHERE `Drafts`.`SentMailingID` = $MailingID ORDER BY `Drafts`.`DateCreated` DESC;";

    $select_result = mysqli_query($Connection, $select_query);

    if (!$select_result) {

        die(mysqli_error($select_result));

    }

    $main_message_row = mysqli_fetch_array($select_result, MYSQLI_ASSOC);

    $from = $main_message_row['FromAddress'];

    $CategoryID = $main_message_row['MailingUserCategoryID'];

    $subject = $main_message_row['Subject'];;

    $headers = "From: ".$from." <" . $from . ">\r\n";
    $headers .= "Reply-To: ". $from . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $message = $main_message_row['Message'];

    mysqli_free_result($select_result);

    $select_query = "SELECT `MailingUserCategoryID`, `UserCategoryQuery` FROM `MailingUserCategories` WHERE `MailingUserCategoryID` = $CategoryID;";

    $select_result = mysqli_query($Connection, $select_query);

    if (!$select_result) {

        die(mysqli_error($select_result));

    }

    if ( mysqli_num_rows($select_result) > 0 ) {

        $query_row = mysqli_fetch_array($select_result, MYSQLI_ASSOC);

        $query_to_run = $query_row['UserCategoryQuery'];

        $query_to_run = UnsanitizeQuery($query_to_run);

        mysqli_free_result($select_result);

        $select_result = mysqli_query($Connection, $query_to_run);

        if (!$select_result) {

            die(mysqli_error($select_result));

        }

        if ( mysqli_num_rows($select_result) > 0 ) {

            $MessageCount = 0;

            $originalmessage = $message;
            $originalsubject = $subject;

            while ($person_message_row = mysqli_fetch_array($select_result, MYSQLI_ASSOC)) {

                $message = str_replace("[[FullName]]", $person_message_row['FirstName']." ".$person_message_row['LastName'], $originalmessage);
                $subject = str_replace("[[FullName]]", $person_message_row['FirstName']." ".$person_message_row['LastName'], $originalsubject);
                $message = str_replace("[[FirstName]]", $person_message_row['FirstName'], $message);
                $subject = str_replace("[[FirstName]]", $person_message_row['FirstName'], $subject);
                $message = str_replace("[[LastName]]", $person_message_row['LastName'], $message);
                $subject = str_replace("[[LastName]]", $person_message_row['LastName'], $subject);
                $message = str_replace("[[EmailAddress]]", $person_message_row['PrimaryEmailAddress'], $message);
                $subject = str_replace("[[EmailAddress]]", $person_message_row['PrimaryEmailAddress'], $subject);

                mail($person_message_row['PrimaryEmailAddress'], $subject, $message, $headers);

                $MessageCount += 1;
            }

            mysqli_free_result($select_result);

            return $MessageCount; // <------- This number returns the correct count.

        } else {

            mysqli_free_result($select_result);

            return 0;

        }

    } else {

        mysqli_free_result($select_result);

        return 0;

    }

}

1 个答案:

答案 0 :(得分:0)

下面列出了一些可能有用的步骤。

  1. 检查所有电子邮件地址是否有效,存储在db。
  2. 检查相应电子邮件帐户中的垃圾邮件文件夹。
  3. 将您的代码放入try {} catch(Exception $e) {},看看是否提供了一些帮助。
  4. 在发送邮件之前,只需将数据写入文件,看看是否一切正常。
  5. 有了这些,你可能会发现一些可以继续前进的东西。