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 = 'me@mysite.com';
$subject = 'Excel Sheet';
$message = 'My Sheet';
$additionalHeaders = "From: me@mysite.com\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
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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.