Jump to content

Mail() Excel Sheet Attachment


tetuanrp

Recommended Posts

Got two scripts which I'm trying to combine. One which when run, takes my mysql db and dumps it into an excel sheet, then prompts the browser to download. The other is your basic mail() script. I would like to combine these, so when I run the first script, instead of prompting the browser it adds it as an attachment to an email. Is that tough? I feel the hardest part is creating the file, not attaching it. Here's my code:

 

<?php
include 'config.php';
include 'opendb.php';

$query  = "SELECT * FROM orders";
$result = mysql_query($query) or die('Error, query failed');

$tsv  = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
   $tsv[]  = implode("\t", $row);
   $html[] = "<tr><td>" .implode("</td><td>", $row) .              "</td></tr>";
}

$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";

$fileName = date('m-d-Y').'.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");

echo $tsv;
//echo $html;

include 'closedb.php';

//MAIL SCRIPT
$to = '[email protected]';
$subject = 'Excel Sheet';
$message = 'My Sheet';
$additionalHeaders = "From: [email protected]\n";
$OK = mail($to, $subject, $message, $additionalHeaders);

?>

 

I take it $tsv is the file?? So how can I do a simple mail and add it as an attachment?

 

Thanks in advance

Ryan

 

Link to comment
https://forums.phpfreaks.com/topic/86944-mail-excel-sheet-attachment/
Share on other sites

<?php
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
?>

 

These are the two lines which are prompting the download, so remove those to start off with.  From that you can see that $fileName is the file, so if you find an e-mail script which will use file attachments, replacing $fileName in to it should do what you want (I hope).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.