how to use new Swift_RecipientList(); in v4
Moderators: Chris Corbyn, General Moderators
how to use new Swift_RecipientList(); in v4
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?
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?
- 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
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.
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.
- 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
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:
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.
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);
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.
Re: how to use new Swift_RecipientList(); in v4
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
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
Re: how to use new Swift_RecipientList(); in v4
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!
Keep up the amazing work Chris!

Re: how to use new Swift_RecipientList(); in v4
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
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
Re: how to use new Swift_RecipientList(); in v4
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
**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,**
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);
Last edited by ashleyK on Tue Mar 31, 2009 6:18 am, edited 2 times in total.
Re: how to use new Swift_RecipientList(); in v4
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.
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.
Re: how to use new Swift_RecipientList(); in v4
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.
ashleyK, did you manage to make it work?
Unfortunately there is any relative doc under Swiftmailer's web site.