Running a multi website and store setup.


Website - Japan Store - JP Store View - JP

I'm trying to import newsletter subscribers from a CSV.

I'm using the following code in a file called import.php that I run from the root dir. eg: dev.xyz.com/import.php and the subscribers.csv is also in the root dir.

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');

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

$customerAccountManagement =$obj

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

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


CSV Headers/Column names as follows:

ID | Email | Type | Customer First Name | Customer Last Name | Status | Website Store | Store View

But I get the following error


Warning: Cannot modify header information - headers already sent by (output started at /home/xyz/dev.xyz.com/import.php:36) in /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php on line 148

Fatal error: Uncaught Magento\Framework\Stdlib\Cookie\FailureToSendException: The cookie with "mage-cache-sessid" cookieName couldn't be deleted. in /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php:154 Stack trace: #0 /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php(301): Magento\Framework\Stdlib\Cookie\PhpCookieManager->setCookie('mage-cache-sess...', '', Array) #1 /home/xyz/dev.xyz.com/vendor/magento/module-customer/CustomerData/Plugin/SessionChecker.php(54): Magento\Framework\Stdlib\Cookie\PhpCookieManager->deleteCookie('mage-cache-sess...', Object(Magento\Framework\Stdlib\Cookie\CookieMetadata)) #2 /home/xyz/dev.xyz.com/vendor/magento/framework/Interception/Interceptor.php(121): Magento\Customer\CustomerData\Plugin\SessionChecker->beforeStart(Object(Magento\Framework\Session\Generic\Interceptor)) #3 /home/xyz/dev.xyz.com/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\Sessio in /home/xyz/dev.xyz.com/vendor/magento/framework/Stdlib/Cookie/PhpCookieManager.php on line 154

How can I import newsletter subscribers from a CSV?

Thanks in advance.


Changed code as suggested by Rajesh Hothi

Now I get the following error


Fatal error: Uncaught Exception: The input is not a valid email address. Use the basic format local-part@hostname in /home/xyz/dev.xyz.com/vendor/magento/module-newsletter/Model/Subscriber.php:501 Stack trace: #0 /home/xyz/dev.xyz.com/import.php(37): Magento\Newsletter\Model\Subscriber->subscribe('ID') #1 {main} thrown in /home/xyz/dev.xyz.com/vendor/magento/module-newsletter/Model/Subscriber.php on line 501

Format of CSV file shown below.

enter image description here

It uses comma ',' as seperator

  • ID,Email,Type,Customer First Name,Customer Last Name,Status,Website,Store,Store View
  • 1,lht@gmail.com,Customer,村瀬,康洋,Subscribed,Japan,JP,Japan
  • 2,4337@icloud.com,Guest, ----, ----,Subscribed,Japan,JP,Japan
  • 3,naka1222@gmail.com,Customer,TANAKA,MASAKI,Subscribed,Japan,JP,Japan
  • 4,eshire@gmail.com,Customer,古賀,裕章,Subscribed,Japan,JP,Japan

It imports one line in which the email ID doesn't show and its not in the order of the csv file, as below.

enter image description here


Changed code as suggested by Raul Sanchez, it gave the same error, I figured it was the header row (row 1) that was causing error and removed it.

now it gives the following error


Fatal error: Uncaught Error: Call to a member function create() on integer in /home/xyz/dev.xyz.com/import.php:37 Stack trace: #0 {main} thrown in /home/xyz/dev.xyz.com/import.php on line 37

its not importing the subscriber names, type, nor the correct website, store or store view.

enter image description here

  • Email is the second column in csv file, so you should get it as $data[1] – Raul Sanchez Nov 6 '19 at 6:47
  • have updated my question, based on your suggestion. – user2240778 Nov 6 '19 at 7:03
  • 1
    Try changing $_subscriberFactory = $_subscriberFactory->create()->subscribe($emailId ); for just $_subscriberFactory->create()->subscribe($emailId); – Raul Sanchez Nov 6 '19 at 8:57
  • changed $_subscriberFactory = $_subscriberFactory->create()->subscribe($emailId ); for just $_subscriberFactory->create()->subscribe($emailId); that imports the email ID but the Type | Customer First Name | Customer Last Name | Status | Website Store | Store View does not get imported. – user2240778 Nov 6 '19 at 9:01
  • Well, you should parse the csv data & send that info to update subscriber info... in your code it seems you are just getting the email $emailId = $data[1]; – Raul Sanchez Nov 6 '19 at 9:09

1.Remove Whitespace before (start) <?php and after (end) ?>

2.Put ob_start(); at top and ob_end_flush(); at end of the script


  // code 

|improve this answer|||||
  • I get another error, have updated my question. – user2240778 Nov 6 '19 at 6:24
  • Please print array echo $data[1]; exit; and check what will it print ? – Rajesh Hothi Nov 6 '19 at 7:42
  • also check your csv file format is UTF-8 ? – Rajesh Hothi Nov 6 '19 at 7:48
  • it is UTF-8 created on Google sheets, and echo prints the first email ID – user2240778 Nov 6 '19 at 7:53
  • Please try with blog.mdnsolutions.com/… – Rajesh Hothi Nov 6 '19 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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