SwiftMailerでメールをバッチ送信する
-
08-07-2019 - |
質問
現在、 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で、データベース内のユーザーから繰り返したいと思います。次のようなもの:
最初、最後、ユーザーからのメールを選択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();
?>
それがあなたが望んでいたことを願っています。
所属していません StackOverflow