gpt4 book ai didi

php - Swift Mailer 只发送表格的最后一行

转载 作者:行者123 更新时间:2023-11-28 23:40:10 25 4
gpt4 key购买 nike

我正在从数据库表中提取电子邮件地址并遍历它们以发送电子邮件。如果我注释掉 $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);
}

?>

最佳答案

使用此函数(在 echo $logger->dump(); 之后)

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

你重新发送了最后一封邮件

如果在 if eval 中,$mailer->send($message, $failures) 也发送电子邮件

关于php - Swift Mailer 只发送表格的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com