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 に答える