I have implemented custom function to send email with csv file as attachment. CSV file content is created on the fly. When I setup email header but text "Content-Type: text/html; charset="utf-8", this getting append in .csv file.

Here is code from custom module function

$message = array(
      'headers' => array(
      'From' => $from,
      'Sender' => $from,
      'Return-Path' => $from,
      'X-Mailer' => 'Drupal',
      'Content-Type' => 'text/html; charset=UTF-8;'
   'id' => 'pathchange_notice',
   'to' => $to,
   'subject' => $subject,
   'body' => array($message),


$uid = md5(uniqid(time()));
$boundary = "--$uid";  
$content = //csv content

$headertext .= "Content-type: text/csv;boundary=".$boundary." name=".$name."\n"; 
$headertext .= "Content-Transfer-Encoding: 7bit\n";
$headertext .= "Content-Disposition: attachment; filename=".$name."\n\n";
$headertext .= $content;

//$message['headers']['Content-type'] = "";
$message['headers']['Content-Transfer-Encoding'] = '7bit'.$headertext;
$message['headers']['Content-Disposition'] = 'attachment;filename='.$name."--".$boundary ."--\n";

$system = drupal_mail_system('mime', 'notice');
$message = $system->format($message);

I can receive email with attachment but when I open csv file, I can see Content-Type: text/html; charset="utf-8".

"Voucher Id","First Name","Last Name" "876-56325-827","Hollis","Rutledge"

Content-Type: text/html; charset="utf-8"

  • Check if you have permissions for attaching/sending files via email in the permissions page.
    – Binny
    Nov 7 '12 at 9:23

Your Answer

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

Browse other questions tagged or ask your own question.