Pages

Thursday 29 September 2016

Sending a mail with attachment in drupal

/* function for generate a CSV file  in drupal*/
 
function exportMobileCsv($filename, $column, $data) {
  $handle = fopen($filename, 'w+');
  fputcsv($handle, $column, ',');
  foreach ($data as $row) {
    fputcsv($handle, (Array)$row, ',');
  }
  file_save_data($handle, $filename);
  fclose($handle);
}


/* function for sending a mail in drupal*/

function mobileapp_mail_csv($from, $subject, $body, $filename) {
  $attachment = array(
    'filepath' => drupal_realpath($filename),
  );
  $mydate =  date('Y-m-d');
  $body = "<html><body>
    <p>
        Hi,
        <br/><br/><br/>Please find attached $mydate . <br />"."
    </p>
    <br/><br/>Regards,
    </body></html>";

  $to = 'hello@gmail.com';
  $headers['Cc'] = 'sumit.prajapati@rediff.com';
  $params = array(
    'to' => $to,
    'attachment' => $attachment,
    'body' => $body,
    'subject' => 'save',
    'headers' => $headers,
  );
  drupal_mail('mobileapp', 'mobileapp_mail_csv', $params['to'], language_default(), $params);
}



/* Function for attachemnt of file. */

function mobileapp_mail($key, &$message, $params) {
  switch ($key) {
    case 'mobileapp_mail_csv':
        $message['subject'] = $params['subject'];
        $message['body'][] = $params['body'];
      if (isset($params['attachment'])) {
        $message['params']['attachments'][] = $params['attachment'];
      }
      $message['headers'] += $params['headers'];
  }
}

No comments:

Post a Comment