Multiple Recipients: Explode a string!

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
User avatar
MichaelGallagher
Forum Newbie
Posts: 7
Joined: Thu Dec 20, 2007 1:09 am
Location: Gold Coast, QLD, Australia
Contact:

Multiple Recipients: Explode a string!

Post by MichaelGallagher »

Hi again,

I have sorted my SWIFT scripts out, and I am able to send multipart emails, with attachments. I gotta give it to you Chris, it is the most efficient I have found! :D

I know that exploding strings into arrays is General PHP forum stuff, but it relates to Swift, so if needs be you can move it, or whatever.

My form enables the user to select from a select list of potential recipients, and clicking one populates this into a text area below. The reason is so they have a pre-set list of recipients with correct names and addresses from the database, and also the ability to edit these addresses once selected, or add an address to the end of the string.

However, since I have changed to use Swift, I cannot use a single string for multiple recipients, eg:

Mick <mick@nodomain.com>, Sally <sally@nodomain.com>, etc <etc@..>, ...

Instead, I require a single line for each recipient, as mentioned at http://www.swiftmailer.org/wikidocs/v3/ ... recipients:

Code: Select all

//Recipients
$recipients =& new Swift_RecipientList();
$recipients->addTo("mick@nodomain.com", "Mick");
$recipients->addTo("sally@nodomain.com", "Sally");
 
$number_sent = $swift->send($message, $recipients, "my-address@domain.tld");
Normally, splitting a string into an array is fine, and i'm using the code below (assuming string is presented very simply delimited by a comma (,)):

Code: Select all

<?php

$recipients= "Mick,mick@nodomain.com,Sally,sally@nodomain.com,hellokitty,hellokitty@nodomain.com";

$exploded = explode(",", $recipients);
for($i = 0; $i < count($exploded); $i++){
	echo "$exploded[$i] <br />";
}
?>
This code splits it into:

Code: Select all

Mick
mick@nodomain.com
Sally
sally@nodomain.com
hellokitty
hellokitty@nodomain.com
But how would I gather each name with each email and present it in the appropriate format DYNAMICALLY (could be 1 recipient, or 100):

Code: Select all

//Recipients
$recipients =& new Swift_RecipientList();
$recipients->addTo("mick@nodomain.com", "Mick");
$recipients->addTo("sally@nodomain.com", "Sally");
$recipients->addTo("hellokitty@nodomain.com", "hellokitty");
 ...
Sorry it was so long winded, but i'm all about detail! :wink:

Cheers,

Mick
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Untested (I'd probably suggest using a different delimiter for Name,Recipient vs Recipient1,Recipient2)....

Code: Select all

$recipients= "Mick,mick@nodomain.com,Sally,sally@nodomain.com,hellokitty,hellokitty@nodomain.com";

$recipientArray = array_chunk(explode(',', $recipients), 2); //explode recipients then chunk 2 at a time

foreach ($recipientArray as $recipient) {
  $recipientList->addTo($recipient[1], $recipient[0]);
}
Post Reply