Magento Newsletter Subscribers Import Script

Magento Newsletter Subscribers Import Script

I was recently helping a client setup and configure their Magento newsletter module, and worked to help them get a large part of their subscriber base imported into Magento from external sources. To accomplish this, I created a newsletter subscribers import script for Magento that helped them accomplish a quick import of their subscriber base that existed outside of their Magento installation.

This script can handle one-off additions, as well as take a file upload, the format being one email address per line. It will also add the emails to the newsletter without sending a confirmation email that they’ve subscribed.

To use the subscribers script, you’ll need to change one thing, the require_once import path to point to your Magento installation’s Mage.php file.

I would also recommend that you place the file within a sub-directory in your current Magento installation, such as "newsletters", to allow you to easily access it for use (i.e. mydomain.com/newsletters/import_subscribers.php).

You can find the Magento newsletter subscribers import script below:

<?php

set_time_limit(180);

require_once "/path/to/magento/installation/app/Mage.php";

Mage::app();

?>

<h3>Subscribe Methods:</h3>

<p><strong>Method:</strong> TXT File</p>

<p>Please upload a text (.txt) file containing one (1) email address per line.</p>

<form action="" method="post" enctype="multipart/form-data">
	<label for="file">Filename:</label>
	<input type="hidden" name="subscribe_method" value="txt_file" />
	<input type="file" name="file" id="file">
	<input type="submit" name="submit" value="Submit">
</form>

<p>- or -</p>

<p><strong>Method:</strong> Individual Email</p>

<form action="" method="post">
	<label for="">Email:</label>
	<input type="hidden" name="subscribe_method" value="individual_email" />
	<input type="text" name="email" id="email" value="" />
	<input type="submit" name="submit" value="Submit" size="60" />
</form>

<?php 

if (isset($_POST['submit']) ){
	
	?>
	<h3>Subscribe Results:</h3>
	<?php
	
    if ($_POST['subscribe_method'] == 'individual_email') {

    	$email = $_POST['email'];
    	Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($email);

    	echo $email . ' has been subscribed<br/>';
        
    } else if ($_POST['subscribe_method'] == 'txt_file') {

    	if ($_FILES["file"]["error"] == 0) {

    		// Open File Ref
    		$handle = fopen($_FILES["file"]["tmp_name"], 'r');

    		if ($handle) {
    			while (($email = fgets($handle, 4096)) !== false) {

    				$email = trim($email);
			        Mage::getModel('newsletter/subscriber')->setImportMode(true)->subscribe($email);
			        
			        echo $email . ' has been subscribed<br/>';
			        
    			}
    		}

    	}
    	
    }

} 

?>

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

1 thoughts on “Magento Newsletter Subscribers Import Script

  1. Alex Morco Reply

    Thanks for sharing this guide, I was trying to setup newsletter for my Magento store, I had to send them black Friday campaign, Your post helped me a lot in configuring this.

Leave a Reply

Your email address will not be published. Required fields are marked *