1,105,197 Community Members

Excel file attachment to mail()

Member Avatar
Tinnin
Posting Whiz in Training
210 posts since Jul 2012
Reputation Points: 8 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 2 [?]
 
0
 

Hi all,

I am trying to create an excel file in PHP which I then want to send as an attachment via the mail() function.

I am able to create the excel file using the following code:

function setHeader($excel_file_name)//this function used to set the header variable
    {
        header("Content-type: application/octet-stream");//A MIME attachment with the content type "application/octet-stream" is a binary file.
        //Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor. 
        header("Content-Disposition: attachment; filename=$excel_file_name");//with this extension of file name you tell what kind of file it is.
        header("Pragma: no-cache");//Prevent Caching
        header("Expires: 0");//Expires and 0 mean that the browser will not cache the page on your hard drive



    }

    setHeader("Test.xls");

which I found on the web (I forget where exactly). This is what I've been using to check that I can edit the excel file properly as it opens it with the "save as / open " option.

How do I then make it so I can attach it using mail()? Do I need to alter the header()s?

Thanks

Member Avatar
Tinnin
Posting Whiz in Training
210 posts since Jul 2012
Reputation Points: 8 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 2 [?]
 
0
 

Nevermind. Figured it out. Three days trying and as soon as I post on the forum for help I get it. /sigh

Anyway:

$headers .= 'Content-Disposition: attachment; filename=Test.xls' . "\r\n";

Just added to the $headers variable in mail(). Seems so obvious now.

Question Self-Answered as of 1 Year Ago
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article