You are here

function mailing_list_export in Mailing List 7

Same name and namespace in other branches
  1. 6 mailing_list.admin.inc \mailing_list_export()

Menu callback; exports a mailing list directly to the user as a CSV file.

1 string reference to 'mailing_list_export'
mailing_list_menu in ./mailing_list.module
Implement hook_menu().

File

./mailing_list.admin.inc, line 384
Mailing list admin UI.

Code

function mailing_list_export($list) {

  // create file to hold email list
  $filename = drupal_strtolower(str_replace(' ', '_', $list->name)) . '.csv';
  $full_path = 'temporary' . '://' . $filename;
  $result = mailing_list_email_get_by_list($list->mlid, 0, 0);
  if (count($result) == 0) {
    drupal_set_message('Cowardly refusing to export an empty mailing list');
    drupal_goto('admin/structure/mailing-list/' . $list->mlid);
  }

  // create string with all the emails
  foreach ($result as $row) {
    $emails[] = '"' . trim($row->mail) . '","' . trim($row->name) . '"';
  }
  $emails = implode("\n", $emails) . "\n";

  // write resultant string to file
  file_unmanaged_save_data($emails, $full_path, FILE_EXISTS_REPLACE);

  // set headers for file transfer
  $headers = array(
    'Content-Type' => 'application/octet-stream',
    'Content-Transfer-Encoding' => 'binary',
    'Content-Disposition' => 'attachment; filename="' . basename($full_path) . '";',
    'Content-Length' => sprintf('%u', filesize($full_path)),
  );
  file_transfer($full_path, $headers);
}