soulglow1985
soulglow1985

Reputation: 143

PHP Foreach Associative Array Loop for Sendy API

I always struggle with loops and in particular associative arrays is still new to me and trying to get past it. I am trying to setup a form to insert multiple emails at once into our sendy email list through the use of their api. I am successful in inserting the first email into the list, but the remaining emails get lost. Your help is greatly appreciated!

PHP Below:

<?php 
require('sendyLibrary.php');
$sendy = new SendyLibrary('3');
$email = $_POST['email'];

//$textareaContents = str_replace("\r", ',', $email);
$multipleEmails = explode("\r", $email);
foreach($multipleEmails as $key => $finalEmail) {
    $sendy->subscribe(array(
        'email' => $finalEmail
    ));
    echo 'email: ' . $finalEmail .'<br/>';
}?>

Response from POST method:

email   [email protected] [email protected] [email protected] [email protected] [email protected]

Source from POST method:

email=sam%40testing.com%0D%0Asamg%40testing.com%0D%0Asamgo%40testing.com%0D%0Asamgol%40testing.com%0D%0Asamgolu%40testing.com

Thank you for your help!

Upvotes: 0

Views: 341

Answers (1)

immulatin
immulatin

Reputation: 2118

There is actually a \r\n inbetween all of the emails. I assume the subscribe method is throwing them out because when you explode the array on \r then you will have new lines on all the remaining emails. Looking like:

array("[email protected]", 
      "\[email protected]", 
      "\[email protected]");

You should explode the tring on \r\n like so:

$multipleEmails = explode("\r\n", $email);

The other option would be to fix the code that sends the POST to provide a better delimited string..

Upvotes: 1

Related Questions