On a Webform I can download a tsv (tab delimited) file and select the 'Only last submissions since download' option.

What I want to do is have a daily cronjob which does the same thing and emails this file automatically.

How would I do this? Is there any existing Module to do this in Drupal 7?


Using a custom module, you can implement hook_cron() to export the csv and send an email:

function MODULE_cron()
    //this is the function that creates the csv, you'll need to research how to use it properly.
    webform_results_download($node, $export_info);
    //send email

As far as sending email, you can use php mail or drupal_mail(), but phpMailer might be the easiest to use.


You could probably do this using Webform MySQL Views and Views Rules which will also give you flexibility of what should be in the email that you send.


i did the same functionality but my final goal is want to upload the webform results in sftp server automatically.

if you have a webform then get its node id ( $node->nid )

function hook_cron()


$node = node_load(1063);

$format = 'delimited';

module_load_include('inc', 'webform', 'includes/webform.export');
module_load_include('inc', 'webform', 'includes/webform.components');
module_load_include('inc', 'webform', 'includes/webform.submissions');

$submission_information = array(
    'serial' => t('Serial'),
    'sid' => t('SID'),
    'time' => t('Time'),
    'draft' => t('Draft'),
    'ip_address' => t('IP Address'),
    'uid' => t('UID'),
    'username' => t('Username'),

$options = array(
      'delimiter' => variable_get('webform_csv_delimiter', ','),
      'components' => array_merge(array_keys($submission_information), array_keys(webform_component_list($node, 'csv', TRUE))),
      'select_keys' => 0,
      'select_format' => 'separate',
      'range_type' => 'all',
      'download' => 1,

  $exporter = webform_export_create_handler($format, $options);

  $file_name = drupal_tempnam('temporary://', 'webform_');
  $handle = @fopen($file_name, 'w'); // The @ suppresses errors.

  // Fill in the header for the submission information (if any).
   $header[2] = $header[1] = $header[0] = count($submission_information) ? array_fill(0, count($submission_information), '') : array();
  if (count($submission_information)) {
    $header[0][0] = $node->title;
    $header[1][0] = t('Submission Details');
    foreach (array_values($submission_information) as $column => $label) {
      $header[2][$column] = $label;

  // Compile header information for components.
  foreach ($options['components'] as $cid) {
    if (isset($node->webform['components'][$cid])) {
      $component = $node->webform['components'][$cid];

      // Let each component determine its headers.
      if (webform_component_feature($component['type'], 'csv')) {
    $component_header = (array) webform_component_invoke($component['type'], 'csv_headers', $component, $options);
    $header[0] = array_merge($header[0], (array) $component_header[0]);
    $header[1] = array_merge($header[1], (array) $component_header[1]);
    $header[2] = array_merge($header[2], (array) $component_header[2]);

    // Allow modules to modify the CSV header.
    drupal_alter('webform_csv_header', $component_header, $component);

  // Add headers to the file.
  foreach ($header as $row) {
    $exporter->add_row($handle, $row);

  // Get all the required submissions for the download.
  $filters['nid'] = $node->nid;

  if (!empty($options['sids'])){
    $filters['sid'] = $options['sids'];

  $submissions = webform_get_submissions($filters);

  // Generate a row for each submission.
  $row_count = 0;
  $sid = 0;
  foreach ($submissions as $sid => $submission) {

    $row = array();
    if (isset($submission_information['serial'])) {
      $row[] = $row_count;
    if (isset($submission_information['sid'])) {
      $row[] = $sid;
    if (isset($submission_information['time'])) {
      $row[] = format_date($submission->submitted, 'short');
    if (isset($submission_information['draft'])) {
      $row[] = $submission->is_draft;
    if (isset($submission_information['ip_address'])) {
      $row[] =  $submission->remote_addr;
    if (isset($submission_information['uid'])) {
      $row[] = $submission->uid;
    if (isset($submission_information['username'])) {
      $row[] = $submission->name;

    foreach ($options['components'] as $cid) {
      if (isset($node->webform['components'][$cid])) {
    $component = $node->webform['components'][$cid];
    // Let each component add its data.
    $raw_data = isset($submission->data[$cid]['value']) ? $submission->data[$cid]['value'] : NULL;
    if (webform_component_feature($component['type'], 'csv')) {
      $data = webform_component_invoke($component['type'], 'csv_data', $component, $options, $raw_data);

      // Allow modules to modify the CSV data.
      drupal_alter('webform_csv_data', $data, $component, $submission);

      if (is_array($data)) {
        $row = array_merge($row, array_values($data));
      else {
        $row[] = isset($data) ? $data : '';

    // Write data from submissions.
    $data = $exporter->add_row($handle, $row);

  // Add the closing bytes.

  // Close the file.

  $export_info['options'] = $options;
  $export_info['file_name'] = $file_name;
  $export_info['exporter'] = $exporter;
  $export_info['row_count'] = $row_count;
  $export_info['last_sid'] = $sid;


In $export_info we get the csv file (which is a php temp file) 1) if you want to download call webform_results_download($node, $export_info) function 2) If you want to send mail you write your mail function and assign the file. 3) if you want to upload the file automatically in another server through SFTP you call your upload function (i have this SFTP upload functionality soon i publish in drupal else if you want mail me i send my code vishnumoorthy.yaso@gmail.com)

Reason for incude this files:

module_load_include('inc', 'webform', 'includes/webform.export'); module_load_include('inc', 'webform', 'includes/webform.components'); module_load_include('inc', 'webform', 'includes/webform.submissions');

To access the function 1) webform_export_create_handler() 2) webform_component_feature() 3) webform_get_submissions()

we include this function in above code these function all available in webform so we just use the function by include the files here.

Hope this help you lot. If i did any mistake please be free to point me. This my first drupal answer . I am happy to give a correct solution. Lets rocking drupal and drupal developers.


  • Hello and welcome to Drupal Answers. Please re-format your answer to properly include the code in the relevant area. I would also recommend to re-check your indendation and to use single-line code formatting with ``.
    – Wtower
    Nov 12 '14 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.