Swift Mailer只发送最后一行表

时间:2016-01-05 20:01:03

标签: php mysql swiftmailer

我从数据库表中提取电子邮件地址并迭代它们以发送电子邮件。如果我注释掉$mailer->send($message);行,则会发送一封电子邮件,但只发送到表格的最后一行。如果我离开该行,电子邮件将发送给所有收件人,但会发送两次到表中的最后一行。我做错了什么?

    <?php

    // Pull the data from the database
    $query = "SELECT emailAddress, firstName, lastName FROM test_table";

    if ($result = mysqli_query($link, $query))
    {
        /* Put the data into an array */
        while($row = mysqli_fetch_assoc($result))
        {
            $swimmers[] = $row;
        }

        /* free result set */
        mysqli_free_result($result);
    }

    /* close connection */
    mysqli_close($link);

    /* Create the replacements array */
    $replacements = array();
    foreach ($swimmers as $swimmmer) {
        $replacements[$swimmer["emailAddress"]] = array (
            "{firstName}" => $swimmer["firstName"],
            "{lastName}" => $swimmer["lastName"],
            "{fullName}" => ($swimmer["firstName"] . ' ' . $swimmer["lastName"])
        );
    }

    /* Create the Transport */
    $transport = Swift_SmtpTransport::newInstance()
        ;

    // Create the Mailer using your created Transport
    $decorator = new Swift_Plugins_DecoratorPlugin($replacements);
    $logger = new Swift_Plugins_Loggers_EchoLogger();
    $mailer = Swift_Mailer::newInstance($transport);
    $mailer->registerPlugin($decorator);
    $mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

    // Create the message
    $message = Swift_Message::newInstance()

        // Give the message a subject
        ->setSubject('STUFF')

        // Set the From address with an associative array
        ->setFrom(array('STUFF' => 'STUFF'))

        // Give it a body
        ->setBody('STUFF', 'text/html')

        // And optionally an alternative body
        ->addPart('STUFF', 'text/plain')


        // Optionally add any attachments
        ->attach(Swift_Attachment::fromPath('STUFF'))
    ;

        // Set the To addresses with an associative array
        foreach($swimmers as $swimmer) 
        {
            echo $swimmer['emailAddress'];
            $message->setTo($swimmer["emailAddress"], $swimmer["fullName"]);
            //$mailer->send($message);
        }

    echo $logger->dump();

        // Pass a variable name to the send() method
        if (!$mailer->send($message, $failures))
        {
            echo "Failures:";
            print_r($failures);
        }

    ?>

2 个答案:

答案 0 :(得分:1)

使用此功能(在echo $ logger-&gt; dump(); 之后)

 if (!$mailer->send($message, $failures))

您重新发送最后一封电子邮件

如果在{eval

中,$mailer->send($message, $failures)也会发送电子邮件

答案 1 :(得分:0)

//If you still want the controll if there is failures
//Set an empty array to hold $failures outside of your loop..
$failures=array();
foreach($swimmers as $swimmer) 
        {
            echo $swimmer['emailAddress'];
            $message->setTo($swimmer["emailAddress"], $swimmer["fullName"]);
            $mailer->send($message, $failures);  

         }
//Outside the loop controll if there is a failure..
if($failures){
    echo "Failures:<br>";
    print_r($failures);
}

更好的是你可以使用Swiftmailer ->setTo方法发送多封邮件。所以你不需要那个foreach循环。但是你需要一个合适的数组。所有过程都可以简化..

看看最后一个例子......

http://swiftmailer.org/docs/sending.html