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



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?


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


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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.