Swift 邮件程序只发送 table 的最后一行
Swift Mailer sending only last row of table
我正在从数据库中提取电子邮件地址 table 并遍历它们以发送电子邮件。如果我注释掉 $mailer->send($message);
行,则会发送一封电子邮件,但只会发送到 table 中的最后一行。如果我保留该行,电子邮件将发送给所有收件人,但会发送两次到 table 中的最后一行。我做错了什么?
<?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);
}
?>
用这个函数(在echo $logger->dump();之后)
if (!$mailer->send($message, $failures))
您重新发送上一封电子邮件
如果在 if eval
中,$mailer->send($message, $failures)
也会发送电子邮件
//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 循环..但是你需要一个合适的数组 it.The 所有过程都可以简化..
看看最后一个例子..
http://swiftmailer.org/docs/sending.html
我正在从数据库中提取电子邮件地址 table 并遍历它们以发送电子邮件。如果我注释掉 $mailer->send($message);
行,则会发送一封电子邮件,但只会发送到 table 中的最后一行。如果我保留该行,电子邮件将发送给所有收件人,但会发送两次到 table 中的最后一行。我做错了什么?
<?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);
}
?>
用这个函数(在echo $logger->dump();之后)
if (!$mailer->send($message, $failures))
您重新发送上一封电子邮件
如果在 if eval
中,$mailer->send($message, $failures)
也会发送电子邮件
//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 循环..但是你需要一个合适的数组 it.The 所有过程都可以简化..
看看最后一个例子..