Failed Try on "Using iterators to provide lists"

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
covina
Forum Newbie
Posts: 15
Joined: Tue May 22, 2007 1:55 pm

Failed Try on "Using iterators to provide lists"

Post by covina »

First, this is definitely a very good mailer script and has lots of tricks to apply.

I'm trying to implement Swift Mailer thru the Tuturial: "Using iterators to provide lists", but failed.
It seems that the $recipients is still empty after $recipients->setIterator($it, "to")

Code: Select all

require_once "lib/Swift/Iterator/MySQLResult.php";
 
$query = "select email, name from users";
$result = mysql_query($query);
 
$it =& new Swift_Iterator_MySQLResult($result);
 
$recipients =& new Swift_RecipientList();
$recipients->setIterator($it, "to"); //or cc, bcc
 
$swift->send($message, $recipients, $sender);
Chris, is the built-in iterator ( I mean Swift_Iterator_MySQLResult) good for use, or I have to build my own iterator?
Anyway, any help is much appreciatd.

Thanks.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I've not used the iterator before, but I reckon you are not querying your mysql correctly. Specifically, I do not see you setting the connection (although you may have omitted this from your post).

Just for sanity's sake, add some error reporting and report back if that affected anything

Code: Select all

$query = "select email, name from users";
$result = mysql_query($query) or die(mysql_error());
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The built in one should be good for use although it's not unit tested due to the nature of it. Have you dumped out the results of the query you're running just to double check?
covina
Forum Newbie
Posts: 15
Joined: Tue May 22, 2007 1:55 pm

Post by covina »

Thanks for the quick help.
Below is the content of the $recipients when I did -- print_r($recipients);
swift_recipientlist Object ( [to] => Array ( ) [cc] => Array ( ) [bcc] => Array ( ) [iterators] => Array ( [to] => swift_iterator_mysqlresult Object ( [resultSet] => Resource id #3 [currentRow] => Array ( [0] => [1] => ) [pos] => -1 [numRows] => 2 ) [cc] => [bcc] => ) ) Array ( )
For your reference, Following is the code that I actually using:

Code: Select all

mysql_select_db($database_SBM, $SBM);
$query_rsMailing = "SELECT mailing_test.Email, mailing_test.eName FROM mailing_test WHERE mailing_test.Status=0 LIMIT 90";
$rsMailing = mysql_query($query_rsMailing, $SINOBILL_MKG) or die(mysql_error());

require_once "/homepages/45/d171048273/htdocs/lib/Swift/Iterator/MySQLResult.php";

$it =& new Swift_Iterator_MySQLResult($rsMailing);
 
$recipients =& new Swift_RecipientList();
$recipients->setIterator($it, "to"); //or cc, bcc
 
$batch =& new Swift_BatchMailer($swift);
$batch->setMaxTRies(3);
$batch->setMaxSuccessiveFailures(5);
$batch->setSleepTime(10); //Sleep for 10 seconds if an error occurs
$batch->send($message, $recipients, $sender);

print_r($recipients);
print_r($batch->getFailedRecipients());
$batch->flushFailedRecipients();

//Disconnect from SMTP, we're done
$swift->disconnect();
After the code execution, no mails sent and no db mail record has been flagged as 'sent' either.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

What does this show?

Code: Select all

$it =& new Swift_Iterator_MySQLResult($rsMailing); 

if ($it->hasNext())
{
  echo "Moving to next row<br />";
  $it->next();
  echo "Current value is:";
  var_dump($it->getValue());
}
else
{
  echo "Empty result set<br />";
}
covina
Forum Newbie
Posts: 15
Joined: Tue May 22, 2007 1:55 pm

Post by covina »

Chris Corbyn wrote:What does this show?

Code: Select all

$it =& new Swift_Iterator_MySQLResult($rsMailing); 

if ($it->hasNext())
{
  echo "Moving to next row<br />";
  $it->next();
  echo "Current value is:";
  var_dump($it->getValue());
}
else
{
  echo "Empty result set<br />";
}
I'll try this and post back later.
Thanks.
covina
Forum Newbie
Posts: 15
Joined: Tue May 22, 2007 1:55 pm

Post by covina »

Chris Corbyn wrote:What does this show?

Code: Select all

$it =& new Swift_Iterator_MySQLResult($rsMailing); 

if ($it->hasNext())
{
  echo "Moving to next row<br />";
  $it->next();
  echo "Current value is:";
  var_dump($it->getValue());
}
else
{
  echo "Empty result set<br />";
}
It shows as follows:
Moving to next row
Current value is:object(swift_address)(2) { ["address"]=> string(13) "sano@sano.com" ["name"]=> string(4) "Stan" }
Also the output for print_r($recipients) is as follows:
swift_recipientlist Object ( [to] => Array ( ) [cc] => Array ( ) [bcc] => Array ( ) [iterators] => Array ( [to] => swift_iterator_mysqlresult Object ( [resultSet] => Resource id #71 [currentRow] => Array ( [0] => sano@sano.com [Email] => sano@sano.com [1] => Stan [eName] => Stan ) [pos] => 0 [numRows] => 3 ) [cc] => [bcc] => ) )
There is total of 3 emails in the recordset, still no emails sent. BTW, The email shown here(sano@sano.com, is the second of the 3 emails.

Also, if I put only 1 email in the list, the output for your code is an empty object.

Still another followup, following is the error code I got when switching from batchmailer back to $swift->send($message,$recipients, $sender)
Fatal error: Call to a member function on a non-object in /homepages/45/d171048273/htdocs/lib/Swift.php on line 443
Here are the results for a mailing list of 10 emails:
Moving to next row
Current value is:object(swift_address)(2) { ["address"]=> string(13) "sano@sano.com" ["name"]=> string(4) "Stan" }
And for print_r($recipients):
swift_recipientlist Object ( [to] => Array ( ) [cc] => Array ( ) [bcc] => Array ( ) [iterators] => Array ( [to] => swift_iterator_mysqlresult Object ( [resultSet] => Resource id #71 [currentRow] => Array ( [0] => sano@sano.com [Email] => sana@sano.com [1] => Stan [eName] => Stan ) [pos] => 0 [numRows] => 10 ) [cc] => [bcc] => ) )
Again the email is the 2nd email in the list, no other emails shown in the result.
Post Reply