Domanda

Attualmente sto utilizzando SwiftMailer per inviare e-mail a più utenti (fino a 50). L'ho impostato e funziona correttamente, tuttavia, non sono sicuro di come estrarre i destinatari dal mio database MySQL e iterare per inviarli.

Ecco quello che ho attualmente:

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

Come puoi vedere sopra, nel setTo, vorrei iterare dai miei utenti nel database. Qualcosa del tipo:

SELEZIONA prima, ultima, e-mail DA utenti DOVE is_active = 1

La documentazione afferma:

Nota: più chiamate a setTo () non aggiungeranno nuovi destinatari - ogni chiamata ha la precedenza sulle chiamate precedenti. Se si desidera aggiungere iterativamente i destinatari, utilizzare il metodo addTo ().

Ma non sono sicuro: 1: Come selezionare dalla mia base di dati in questo script e: 2: Se dovessi usare l'addo () metodo nel mio caso. Qualche suggerimento su come configurarlo correttamente?

Grazie!

È stato utile?

Soluzione

Non sono del tutto sicuro di aver fatto bene la tua domanda, ma ecco un modo per farlo:

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

Spero che sia quello che volevi.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top