-1

I have string that contains comma separated email addresses.

I need to add them to my mailer object ($mailin) using the addBcc method. The object supports method chaining.

I have tried to echo in a for loop to achieve what I wanted but as expected, it did not work. It gave me 500 error.

Desired result;

$mailin = new Mailin('my@mail.com', 'apikey');
$mails = "a@example.com, b@example.com";
$mailin->
    addBcc('a@example.com')->
    addBcc('b@example.com')->

Thank you.

Progrock
  • 7,373
  • 1
  • 19
  • 25
Ece
  • 148
  • 9
  • http://stackoverflow.com/questions/11396033/stdclass-to-array cast it to array? – Richard May 24 '16 at 22:25
  • depending on the mailer addBcc, may allow arrays or strings. if not explode and loop –  May 24 '16 at 22:27

2 Answers2

1

you can use explode and a foreach loop like so:

$mails = "a@example.com, b@example.com";
$bcc = explode(',',$mails);

foreach($bcc as $address){
    $mailin->addBcc(trim($address));
}
andrew
  • 9,313
  • 7
  • 30
  • 61
0

if you have:

$mails = "a@example.com, b@example.com"; 

you could do the following to obtain an array with all the separate emails:

$mailArray = [];
$result = [];      //this will be the result
list($mailArray) = explode(",", $mails);//here you are separating the emails
foreach ($mailArray as $value)
{
    $result[] = trim($value);//here you are taking out the whitespaces left behind
}

And $result would be an array full of the emails you are looking to use. If you want to look inside the array, you can do:

foreach ($result as $value)
{
  echo "-->{$value}<br>";
}

And if you want to use it in your previous code, it would look similar to the following:

$mailin->
    addBcc($result[0])->
    addBcc($result[1])->
//... rest of your code
Webeng
  • 7,050
  • 4
  • 31
  • 59
  • wait, are you suggesting eval? – Evert May 24 '16 at 22:38
  • @Evert I'm not sure what eval is, but after googling it just now I am sure I am not suggesting it. Why? Does my code have elements of that somewhere? – Webeng May 24 '16 at 22:40
  • @Evert I think you might have been confused with the `-->{$value}
    ` string. The `-->` isn't meant to be code, it's part of the string being echoed in a `foreach` loop to show the OP the results he obtained from the code.
    – Webeng May 24 '16 at 22:43
  • Well, OP is looking to call a method on an object multiple times. Your answer doesn't do that, but instead it echos how that code looks like? – Evert May 24 '16 at 22:43
  • Ah indeed. I see what you were going for. – Evert May 24 '16 at 22:43
  • why 2 loops instead of just 1 –  May 24 '16 at 22:44
  • @Dagon the second `foreach` loop is just debugging code for the OP to actually see what is inside the array he just made. BTW, what does OP stand for? I know its the user who asked the question, but the initials O.P.? – Webeng May 24 '16 at 22:44
  • @Webeng O.P = Original Poster or Original Post. https://en.wikipedia.org/wiki/Internet_forum#Thread –  May 25 '16 at 00:00