Skip to content

Instantly share code, notes, and snippets.

@fballiano
Created June 20, 2014 10:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fballiano/1698d2a5da4ea6d57ae7 to your computer and use it in GitHub Desktop.
Save fballiano/1698d2a5da4ea6d57ae7 to your computer and use it in GitHub Desktop.
Import newsletter subscribers (without sending any emails) into Magento
<?php
$store_id = 5;
$csv_filepath = "newsletter.csv";
$email_csv_column_index = 2;
$csv_delimiter = ',';
$csv_enclosure = '"';
$magento_path = __DIR__;
require "{$magento_path}/app/Mage.php";
Mage::app()->setCurrentStore($store_id);
$fp = fopen($csv_filepath, "r");
if (!$fp) die("{$csv_filepath} not found\n");
while (($row = fgetcsv($fp, 0, $csv_delimiter, $csv_enclosure)) !== false) {
$email = trim($row[$email_csv_column_index]);
if (strlen($email) == 0) continue;
echo "$email";
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
if ($subscriber->getId()) {
echo " already subscribed\n";
continue;
}
Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($email);
echo " ok\n";
}
echo "Import finished\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment