سؤال

أنا حاليا باستخدام 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: كيفية اختيار من datebase في هذا السيناريو: 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