Export email addresses to CSV in Magento

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.


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!

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