Magento – Export All Customers to CSV with Billing and Shipping Address

<?php
require_once('app/Mage.php');
umask(0);
Mage::app();
$customers = Mage::getModel('customer/customer')->getCollection();
$customers->addAttributeToSelect('*');
$customersArray[0] = array(
'entity_id',
'email',
'prefix',
'firstname',
'middlename',
'lastname',
'suffix',
'website_id',
'store_id',
'group_id',
'created_at',
'updated_at',
'is_active',
'is_subscribed',
'billing_firstname',
'billing_lastname',
'billing_street_1',
'billing_street_2',
'billing_city',
'billing_region_id',
'billing_region',
'billing_postcode',
'billing_country_id',
'billing_telephone',
'shipping_firstname',
'shipping_lastname',
'shipping_street_1',
'shipping_street_2',
'shipping_city',
'shipping_region_id',
'shipping_region',
'shipping_postcode',
'shipping_country_id',
'shipping_telephone'
);
$i = 1;
foreach ($customers as $key => $customer) {
$customersArray[$i]['entity_id'] = $customer->getData('entity_id');
$customersArray[$i]['email'] = $customer->getData('email');
$customersArray[$i]['prefix'] = $customer->getData('prefix');
$customersArray[$i]['firstname'] = $customer->getData('firstname');
$customersArray[$i]['middlename'] = $customer->getData('middlename');
$customersArray[$i]['lastname'] = $customer->getData('lastname');
$customersArray[$i]['suffix'] = $customer->getData('suffix');
$customersArray[$i]['website_id'] = $customer->getData('website_id');
$customersArray[$i]['store_id'] = $customer->getData('store_id');
$customersArray[$i]['group_id'] = $customer->getData('group_id');
$customersArray[$i]['created_at'] = $customer->getData('created_at');
$customersArray[$i]['updated_at'] = $customer->getData('updated_at');
$customersArray[$i]['is_active'] = $customer->getData('is_active');
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($customersArray[$i]['email']);
$subscriberStatus = $subscriber->isSubscribed();
$customersArray[$i]['is_subscribed'] = ($subscriberStatus? 1 : 0);
if($customer->getDefaultBilling()) {
$billingAddress = Mage::getModel('customer/address')->load($customer->getDefaultBilling());
if($billingAddress->getId()) {
$customersArray[$i]['billing_firstname'] = $billingAddress->getData('firstname');
$customersArray[$i]['billing_lastname'] = $billingAddress->getData('lastname');
$customersArray[$i]['billing_street_1'] = $billingAddress->getStreet1();
$customersArray[$i]['billing_street_2'] = $billingAddress->getStreet2();
$customersArray[$i]['billing_city'] = $billingAddress->getData('city');
$customersArray[$i]['billing_region_id'] = $billingAddress->getData('region_id');
$customersArray[$i]['billing_region'] = $billingAddress->getData('region');
$customersArray[$i]['billing_postcode'] = $billingAddress->getData('postcode');
$customersArray[$i]['billing_country_id'] = $billingAddress->getData('country_id');
$customersArray[$i]['billing_telephone'] = $billingAddress->getData('telephone');
}
}
if($customer->getDefaultShipping()) {
$shippingAddress = Mage::getModel('customer/address')->load($customer->getDefaultShipping());
if($shippingAddress->getId()) {
$customersArray[$i]['shipping_firstname'] = $shippingAddress->getData('firstname');
$customersArray[$i]['shipping_lastname'] = $shippingAddress->getData('lastname');
$customersArray[$i]['shipping_street_1'] = $shippingAddress->getStreet1();
$customersArray[$i]['shipping_street_2'] = $shippingAddress->getStreet2();
$customersArray[$i]['shipping_city'] = $shippingAddress->getData('city');
$customersArray[$i]['shipping_region_id'] = $shippingAddress->getData('region_id');
$customersArray[$i]['shipping_region'] = $shippingAddress->getData('region');
$customersArray[$i]['shipping_postcode'] = $shippingAddress->getData('postcode');
$customersArray[$i]['shipping_country_id'] = $shippingAddress->getData('country_id');
$customersArray[$i]['shipping_telephone'] = $shippingAddress->getData('telephone');
}
}
$i++;
//if ($i == 5) { break; }
}
$file = fopen('customer_export.csv', 'w');
foreach ($customersArray as $customer) {
fputcsv($file, $customer);
}
fclose($file);
//echo "<pre>";
//var_dump($customersArray);
//echo "</pre>";
?>