how to use new Swift_RecipientList(); in v4

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
ashleyK
Forum Newbie
Posts: 11
Joined: Tue Mar 18, 2008 8:10 am

how to use new Swift_RecipientList(); in v4

Post by ashleyK »

Hi, i'm just trying out v4 for the first time and I can't find anything about new Swift_RecipientList(); in the manual. Does it still exist, or has it been replaced?

I used to use the following (where $email was an array with my recipients in)

$recipients = new Swift_RecipientList();
foreach($email as $emailkey => $emailval) {
$recipients->addTo($emailval);
}
$swift->batchSend($message, $recipients, new Swift_Address("mailorder@xxx.com", "xxx"));

what is the new equivalent please?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how to use new Swift_RecipientList(); in v4

Post by Chris Corbyn »

It doesn't exist anymore... it's no longer needed.

You just add the recipients to the $message object manually via setTo() etc. There's a new addTo() method that isn't yet documented.

Lot's has change in version 4 so you basically need to just read the documentation for usage information.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: how to use new Swift_RecipientList(); in v4

Post by Chris Corbyn »

While I say it doesn't exist anymore, the facility is still there if you prefer to use it. The naming is different though.

It works like this:

Code: Select all

$recipients = new Swift_Mailer_ArrayRecipientIterator(array(
  'person-one@domain.com' => 'Name 1',
  'person-two@domain.com' => 'Name 2'
));
 
$mailer->batchSend($message, $failures, $recipients);
The second parameter $failures is not related; it's just where Swift Mailer v4 dumps the list of failed recipients.

ArrayRecipientIterator used in the code above is a simple implementation of Swift_Mailer_RecipientIterator which simply defines two methods: hasNext() and nextRecipient(). This means you can provide your own implementation that looks up recipients from a database on-the-fly.

See the file: lib/classes/Swift/Mailer/RecipientIterator.php for more details on how to implement that if you need it.
ashleyK
Forum Newbie
Posts: 11
Joined: Tue Mar 18, 2008 8:10 am

Re: how to use new Swift_RecipientList(); in v4

Post by ashleyK »

Hi Chris, i'm not sure I understand, I just need to know the easiest way if I already have an array simply containing a bunch of e-mail addresses, how to pass this information to the mailer.

Is it possible to simply use

$mailer->batchSend($message, $failures, $recipients);

If $recipients is a simple array containing just e-mail addresses, or do they need to be formatted in some way?

Thanks
Ashley
ashleyK
Forum Newbie
Posts: 11
Joined: Tue Mar 18, 2008 8:10 am

Re: how to use new Swift_RecipientList(); in v4

Post by ashleyK »

Actually after a bit of tinkering about i've figured it out for myself, sorry for being a dummy!

Keep up the amazing work Chris! :D
ashleyK
Forum Newbie
Posts: 11
Joined: Tue Mar 18, 2008 8:10 am

Re: how to use new Swift_RecipientList(); in v4

Post by ashleyK »

Hi, I can only get the Swift_Mailer_ArrayRecipientIterator to send to the first address in my array.

If I use the following to populate my array, while looping through the database (I don't need a 'name' value)

$emails['$current email address from database'] = '';


I get for instance, with print_r($emails):

Array
(
[test1@test.com] =>
[test2@test.com] =>
[test3@test.com] =>
)


Then if I use

$recipients = new Swift_Mailer_ArrayRecipientIterator($emails);
$mailer->batchSend($message, $failures, $recipients);

it only sends to the first e-mail address in the list

Please can you let me know what I am doing wrong?

Thanks
Ashley
ashleyK
Forum Newbie
Posts: 11
Joined: Tue Mar 18, 2008 8:10 am

Re: how to use new Swift_RecipientList(); in v4

Post by ashleyK »

Following on, even when I use this code exactly and substitute the two e-mail addresses for addresses I can test with, I only get the first one sent

Code: Select all

$recipients = new Swift_Mailer_ArrayRecipientIterator(array(
  'person-one@domain.com' => 'Name 1',
  'person-two@domain.com' => 'Name 2'
));
 
$mailer->batchSend($message, $failures, $recipients);
**edit - this seems to be a problem with gmail which will only show one e-mail at a time if two addresses are forwarded to it at exactly the same time,**
Last edited by ashleyK on Tue Mar 31, 2009 6:18 am, edited 2 times in total.
ashleyK
Forum Newbie
Posts: 11
Joined: Tue Mar 18, 2008 8:10 am

Re: how to use new Swift_RecipientList(); in v4

Post by ashleyK »

If I use the sample code from http://swiftmailer.org/docs/batchsend-method

I can get it to work if I 'hard code' the email addresses in there.

I need to know the exact syntax to use to populate an array which I can use with the iterator, as it just doesn't seem to be working the way I am using it.
mrdebian
Forum Newbie
Posts: 9
Joined: Thu May 10, 2007 4:38 am

Re: how to use new Swift_RecipientList(); in v4

Post by mrdebian »

This is a very old thread but got the same problem and didn't find anything related on Internet.
ashleyK, did you manage to make it work?

Unfortunately there is any relative doc under Swiftmailer's web site.
Post Reply