1

I'm running into the following problem: I have a list of emails in a file. I want to loop through these and create customers, then I want to do something with the customer. However in my script it's only loading the last customer.

I have the following code:

require_once("./app/Mage.php");
Mage::app();

$file = fopen("Test.txt", "r");
if ($file) {
    $emails = file("Test.txt");
    foreach($emails as $email) {

    echo $email . "<br/>"; // < shows all emails

    // load up the customer 
    $customer = Mage::getModel('customer/customer')
    ->setWebsiteId(1)
    ->loadByEmail($email);

    echo $customer->getId() . "<br/>"; // < returns blank except for the last email line

    // if we found the customer
    if ($customer->getId()){
        echo $customer->getId() . "adsf<br/>"; // < only shown on last line
        //.. do something
    }
    }

    fclose($file);
} else {
    // error opening the file.
}

I have also tried

while(!feof($file)){
    $email = fgets($file);

and

while (($email = fgets($file)) !== false)

but same result, so it appears it's not something wrong with the php code. Can someone help me?

closed as unclear what you're asking by Amit Bera, 7ochem, Marius Apr 29 '15 at 20:28

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • have you check this email is exits in admin – Amit Bera Apr 29 '15 at 14:27
  • yes they exist. I tested it also by putting a different email last in the file, then it always takes the last email – Robin Apr 29 '15 at 14:28
1

Below should read your file line by line and load the customers. I've had issues before where text files saved on Windows had unreadable line endings. Make sure it doesn't see all the email addresses as one line

if ($file = fopen("Test.txt", "rfile)) {
    while (($email = fgets($handle)) !== false) {
        echo $email . "<br/>"; // < shows all emails

        // load up the customer 
        $customer = Mage::getModel('customer/customer')
           ->setWebsiteId(1)
           ->loadByEmail(trim($email));

         echo $customer->getId() . "<br/>"; // < returns blank except for the last email line

         // if we found the customer
         if ($customer->getId()){
            echo $customer->getId() . "adsf<br/>"; // < only shown on last line
            //.. do something
         }
    }

    fclose($file);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.