【问题标题】:Batch Send Email with SwiftMailer使用 SwiftMailer 批量发送电子邮件
【发布时间】: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


    【解决方案1】:

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

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

    我希望这就是你想要的。

    【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2018-01-07
    相关资源
    最近更新 更多