【发布时间】:2010-12-11 01:59:33
【问题描述】:
我目前正在使用SwiftMailer 向多个用户(最多 50 个)发送电子邮件。我已经设置并正常工作,但是,我不太确定如何从我的 MySQL 数据库中提取收件人并迭代发送它们。
这是我目前拥有的:
<?php
require_once 'swift/lib/swift_required.php';
$mailer = Swift_Mailer::newInstance(
Swift_SmtpTransport::newInstance('smtp.connection.com', 25)
->setUsername('myUserName')
->setPassword('myPassword')
);
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(9));
$message = Swift_Message::newInstance()
->setSubject('Let\'s get together today.')
->setFrom(array('myfrom@domain.com' => 'From Me'))
->setTo(array('tom_jones@domain.com' => 'Tom Jones', 'jsmith@domain.com' => 'Jane Smith', 'j_doe@domain.com' => 'John Doe', 'bill_watson@domain.com' => 'Bill Watson',))
->setBody('Here is the message itself')
->addPart('<b>Test message being sent!!</b>', 'text/html')
;
$numSent = $mailer->batchSend($message, $failures);
printf("Sent %d messages\n", $numSent);
正如您在上面看到的,在 setTo 中,我想从数据库中的用户进行迭代。比如:
SELECT first, last, email FROM users WHERE is_active=1
documentation 声明:
Note: Multiple calls to setTo() will not add new recipients – each call overrides the previous calls. If you want to iteratively add recipients, use the addTo() method.
但是,我不确定:1:如何在此脚本中从我的数据库中选择:2:如果我需要使用 addTo()在我的情况下的方法。有关如何正确设置的任何建议?
谢谢!
【问题讨论】:
标签: php mysql scripting swiftmailer