6

我目前正在使用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

文档指出:

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() 方法。关于如何正确设置的任何建议?

谢谢!

4

1 回答 1

7

我不太确定我的问题是否正确,但这是一种方法:

<?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();
?>

我希望这就是你想要的。

于 2009-10-29T00:28:50.897 回答