Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get PHP output to Excel file which is emailed as attachment

Status
Not open for further replies.

jsweet

Technical User
Jul 28, 2006
88
US
Hello php newbie here

I have a openinexcel.php file which basically starts a session then I have...

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=IPOfficeConfig.xls")

then I have
echo $_SESSION['var'];
//where var is what I want to output I want to in Excel format.
?>

this works for opening up the output as an excel file but I would also like to give the user the option to click a link and get an email message popup with the address already populated and the excel output as an attached file.

Is that possible? What would I need to do?
 
why not just send it directly from php? provide the user with a web form to specify the to address.
 
If you realy need client side your into JaveScript world or you could look at the mailto option of (<a href="mailto:fred@fred.com>mailto</a>)
 
Google: Php mail attachment.

I will investigate if this works as it should:
Code:
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here 
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo = "mail send ... OK"; // or use booleans here
    } else {
        echo = "mail send ... ERROR!";
    }
}
// how to use
$my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>

I wouldn't recomment using mailto as this would only open the default mail program (Usually Outlook) and you must have everything set up correctly...
 
i was not aware that mailto could be used to attach binaries too.
 
no, its a very simple interface but you shoud be able to embed a link in the text.
Don't know never tried, like you I'd post from the server.
 
Thanks

I'm not opposed to doing a php web-based form where the user has to put in the addresses, but the output from my $_SESSION['var'] still needs to be an attachment in excel format. Would I have to safe the excel file to the server then attach to the email?
 
Yes, you will need to upload the file to the server then email it on.
You've sorted out the mime headers in your code so you could wither write the bytes direct to the output stream or you could use a PHP email package which severla have been mentioned in this forum but I can't recall the name, hopefully someone else will
 
i don't think that you need to save the file, no. as ingresman says, just encode the variable and attach it to the right part of your message. i have written some code to do all of this and posted it in the forum a few years ago, but frankly phpmailer is much much better.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top