0

I have .csv in which have lot of email is exit, it want to assign those email to newsletter suscribers.

I have this

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$store_id = 1;
$csv_filepath = "subscribers.csv";
$csv_delimiter = ',';
$csv_enclosure = '"';
$magento_path = __DIR__;

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$row = 1;
if (($handle = fopen("subscribers.csv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        $status = $obj->get('Magento\Newsletter\Model\Subscriber')->subscribe($data[$c]);
        echo $data[$c] . "<br />\n";
    }
  }
  fclose($handle);
}
?>

It's work, but the problem is only import the first contact.

Help appreciated.

1
  • Can you please share CSV files?
    – Amit Bera
    Sep 28, 2017 at 17:52

2 Answers 2

3

Change

$status = $obj->get('Magento\Newsletter\Model\Subscriber')->subscribe($data[$c]);

to

$status = $obj->create('Magento\Newsletter\Model\Subscriber')->subscribe($data[$c]);
1
  • HI! If my .csv have all emails in one line work! Thank you!
    – drafiko
    Sep 28, 2017 at 23:33
0

If you csv like enter image description here

Then

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$store_id = 1;
$csv_filepath = "subscribers.csv";
$csv_delimiter = ',';
$csv_enclosure = '"';
$magento_path = __DIR__;

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$websiteId =  $obj->get('Magento\Store\Model\StoreManagerInterface')
    ->getStore()
    ->getWebsiteId();

$customerAccountManagement =$obj
    ->create('Magento\Customer\Api\AccountManagementInterface');

$_subscriberFactory = $obj->create('Magento\Newsletter\Model\SubscriberFactory');


if (($handle = fopen("subscribers.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        // email Id
        $emailId = $data[0];
       /* customer */
        if($customerAccountManagement->isEmailAvailable($emailId , $websiteId)){
            echo $emailId ."Guest <br/>";
            $_subscriberFactory = $_subscriberFactory->create()->subscribe($emailId );
        }else{
            echo $emailId ."Register <br/>";
            $customerFactory = $obj->get('Magento\Customer\Model\CustomerFactory');
            $customer=$customerFactory->create();
            $customer->setWebsiteId($websiteId);
            $customer->loadByEmail($emailId );// load customer by email address
            if($customer->getId()){
                $_subscriberFactory->create()->subscribeCustomerById($customer->getId());
            }

        }
    }
    fclose($handle);
}

Note that:

Email server should work other wise,it is not worked

1
  • Thank you for help! But i have the .csv how you display, but, don't work. I receive this message. [email protected] Guest [email protected] Guest Fatal error: Uncaught Error: Call to a member function create() on integer in import.php:36 Stack trace: #0 {main} thrown in impor6.php on line 36 Only import first email.
    – drafiko
    Sep 28, 2017 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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