Skip to content

Instantly share code, notes, and snippets.

@alex-moreno
Last active December 22, 2015 11:18
Show Gist options
  • Save alex-moreno/6464536 to your computer and use it in GitHub Desktop.
Save alex-moreno/6464536 to your computer and use it in GitHub Desktop.
fetching a view and sending the contents via download browser. two main functions, file_save_data and file_transfer
/**
* Creates and starts client download of the distribution email.
*/
function module_download() {
// Header sets content type to HTML.
$render = '<head>
<title>EDM- </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>';
// Gets email distribution view for main offers.
$main_offer_view = views_get_view('email_distribution');
$main_offer_view->set_display('email_main');
$main_offer_view->pre_execute();
$main_offer_view->execute();
$render .= $main_offer_view->render();
// Gets email distribution view for list offers.
$offers_view = views_get_view('email_distribution');
$offers_view->set_display('email_offers');
$offers_view->pre_execute();
$offers_view->execute();
$render .= $offers_view->render();
$render .= '</body>';
// Saves file to temp locations.
$success = file_save_data($render, 'temporary://');
if ($success !== FALSE) {
// If file is successfully built, begin transfer to client.
file_transfer($success->uri, array(
'Content-Type' => 'text/html; charset=utf-8',
'Content-Disposition' => 'attachment; filename="email_content.html"',
'Content-Length' => filesize($success->uri)));
}
else {
drupal_set_message(t("Unable to deliver file"), 'error');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment