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

文档指出:

注意:多次调用setTo()不会添加新收件人&#8211;每次通话都会覆盖之前的通话。如果要迭代添加收件人,请使用addTo()方法。

但是,我不确定: 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();
?>

我希望这就是你想要的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top