HummingbirdUK main logo

Coding solutions to business problems

About us

We use code to create solutions to business challenges, bottle-necks and headaches.

If you think your business has a problem that can be solved through code, we are happy to chat things through without any obligation.

Get in touch

Export email addresses to CSV in Magento

Home / Blog / Export email addresses to CSV in Magento

Written by Giles Bennett

This cropped up the other day - the client wanted to export all customer email addresses from their Magento store for all historical orders, not just those of registered customers. The information was to be imported into Mailchimp, so needed to be dumped into a CSV file containing the customer's first name, last name and email address.

Updated

Note that this script wasn't working on all installations - this has now been fixed with the addition of two lines at the start of the script.

The below script is the quick and dirty solution. Nothing massively complicated about it, it does exactly what it says on the tin, and if called through the browser (beware timeouts for larger stores) or over the command line, it creates a CSV called file.csv stored in the same directory on the server as the script, which contains the relevant information in it.

Here's the code - enjoy!

<?php
require_once("app/Mage.php");
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$orders = Mage::getModel('sales/order')->getCollection();
$orders->setPage(1, 3000);
$fp = fopen('file.csv', 'w');
foreach($orders as $order) {
  $fields = array($order->getBillingAddress()->getFirstname(),$order->getBillingAddress()->getLastname(),$order->getBillingAddress()->getEmail());
  fputcsv($fp, $fields);
}
?>
Author : Giles Bennett

About the author

Giles Bennett built his first website in 1996, and is old enough to miss Netscape Navigator. Initially a lawyer, he jumped ship to IT in 2008, and after 5 years as a freelancer, he founded HummingbirdUK in 2013. He can be reached by email at giles@hummingbirduk.com.