SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    - Michael - Bytecon's Avatar
    Join Date
    Mar 2004
    Location
    Ireland
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile CSV by email attachment - what mime type to use?

    Hi,

    I'm trying to send a CSV file as an email attachment - here is my code ATM:

    PHP Code:
    $fileatt "$filename"// Path to the file
    $fileatt_type "application/vnd.ms-excel"// File Type
    $fileatt_name "$date"// Filename that will be used for the file as the attachment

    $email_from "no-reply@domain.com"// Who the email is from
    $email_subject "Your attached file"// The Subject of the email
    $email_message "Sent Message";

    $email_to "myemail@domain.com";

    $headers "From: ".$email_from;

    $file fopen($fileatt,'rb');
    $data fread($file,filesize($fileatt));
    fclose($file);

    $semi_rand md5(time());
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message .= "\n\n";

    $data chunk_split(base64_encode($data));

    $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data .= "\n\n" .
    "--{$mime_boundary}--\n";

    $ok = @mail($email_to$email_subject$email_message$headers);

    if(
    $ok) {
    echo 
    "Sent";

    } else {
    die(
    "Problem");

    The code works fine and I receive the data. However I can't get the script to send the file as a .CSV file. I have tried using the "text/csv" and "text/comma-separated-values" MIME types and both send the file as a .txt file. Using the "application/vnd.ms-excel" MIME type it sends the file as an XLS file which doesn't display the file correctly.

    Am I doing something wrong? How can I get the PHP script to attach the file as a .csv file?

    Any help would be greatly appreciated!

    Thanks,
    Michael

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First thing I would do is use a a proper mailer, like SwiftMailer, instead of constructing the headers, etc yourself, just manipulate an easy API and voila.
    The only constant in software is change itself

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Austria
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a lot of errors in your code:
    - Some \n are wrong or missing.
    - Use Punycode for e-mail-adresses.
    - Use mb_encode_mimeheader for subject.
    - Add Content-Disposition info.
    - And all data must be put into the header.

    Finally mail must look like:

    mail('xxx@yyy.de','subject','',$headerData)

    Google: php mail attachments

    I think, csv-files are not binary-files. So I would not use 'b'.
    file_get_contents ist easier than fopen....

    For testing:
    - don´t use any @
    - At the beginning of your script: error_reporting(E_ALL|E_STRICT);

  4. #4
    - Michael - Bytecon's Avatar
    Join Date
    Mar 2004
    Location
    Ireland
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone have any idea on the MIME type issue? I understand the code may not be perfect but it sends an email fine, its just configuring it to send it as a .csv file im having trouble with.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MIME type is text/plain or text/csv for CSV files
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  6. #6
    - Michael - Bytecon's Avatar
    Join Date
    Mar 2004
    Location
    Ireland
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply Mark, when I use text/plain or text/csv to send the file as an attachement using the code above it sends the file attached as a .txt file, do you know anyway to get it to send as a .csv file?

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bytecon View Post
    Thanks for your reply Mark, when I use text/plain or text/csv to send the file as an attachement using the code above it sends the file attached as a .txt file, do you know anyway to get it to send as a .csv file?
    Give the filename the correct extension: $fileatt_name = $date.'.csv';
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  8. #8
    - Michael - Bytecon's Avatar
    Join Date
    Mar 2004
    Location
    Ireland
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mark, thanks a lot, works perfectly, don't know how I missed that one!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •