php - 使用 SwiftMailer 批量发送电子邮件

标签 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);

正如您在上面看到的,在 setTo 中,我想从数据库中的用户开始迭代。像这样的东西:

SELECT first, last, email FROM users WHERE is_active=1

documentation状态:

注意:多次调用 setTo() 不会添加新的收件人——每次调用都会覆盖之前的调用。如果要迭代添加收件人,请使用 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();
?>

希望这就是您想要的。

关于php - 使用 SwiftMailer 批量发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1640856/

相关文章:

programming-languages - Lua 开发是否停滞不前,或者是否还有新版本计划?

bash - 使用 sed 删除文件中的括号

php - 相同字符串的不同长度

php - 是否可以将 php 代码插入 ' header("Location : example. php") ' 命令?

php - 获取不同的列并返回过滤结果

mysql - 将 Join SQL 查询重写为子查询

php - 我怎样才能使用2个同名的表单字段,但只使用填写的字段将数据插入mysql?

bash - 如何迭代 Bash 脚本中的位置参数?

php - 将 div(通过 php 数据库获得)并排放置,当行已满时彼此放置

php 无法读取 uft8 json