Dod*_*nas 6 php mysql scripting swiftmailer
我目前正在使用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);
Run Code Online (Sandbox Code Playgroud)
如上所示,在setTo中,我想从数据库中的用户进行迭代.就像是:
SELECT first, last, email FROM users WHERE is_active=1
该文件规定:
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
$message = Swift_Message::newInstance()
->setSubject('Let\'s get together today.')
->setFrom(array('myfrom@domain.com' => 'From Me'))
->setBody('Here is the message itself')
->addPart('<b>Test message being sent!!</b>', 'text/html')
;
$data = mysql_query('SELECT first, last, email FROM users WHERE is_active=1') or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
$message->addTo($row['email'], $row['first'] . ' ' . $row['last']);
}
$message->batchSend();
?>
Run Code Online (Sandbox Code Playgroud)
我希望这就是你想要的.
| 归档时间: |
|
| 查看次数: |
7128 次 |
| 最近记录: |