user3046528
user3046528

Reputation: 9

Creating excel report with php and sending by email

I have this php that generates an excel file for me with php reading from a database. I would like to be able to email the excel (as an attachment). The problem is that I can only get the file download but when I get the email there is no attachment

Could you help me?

When the email arrives I have only this result, without any attachment (excel file):

--90215ae30d47b3d98d4505ee5035e618 Content-Transfer-Encoding: 7bit

This is a MIME encoded message. --90215ae30d47b3d98d4505ee5035e618 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 8bit

--90215ae30d47b3d98d4505ee5035e618 Content-Type: application/octet-stream; name="test.xls" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="test.xls"

MQkwMzAyMDE4NTIzCU1HTkdQUDg0QTE4QzM1MVcJTUFHTkFOTyBESSBTQU4gTElPICAgICAgICAg ICAgICAgICAgICAgIAlHSVVTRVBQRSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCQ==

--90215ae30d47b3d98d4505ee5035e618--

<?php
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
 
$db2 = db2_connect();
$sep = "\t";
 
echo "Pr0001 \t Cdclie \t CodFis \t \n ";

$data = "Select t1.Pr0001, t2.CDCLIE, t1.CODFIS FROM MYLIB.ANAGR001F AS t1"; 

$result = db2_exec($db2, $data);
 
while ($row = db2_fetch_both($result)) {
    $schema_insert = "";
    $schema_insert .= "$row[0]".$sep;
    $schema_insert .= "$row[1]".$sep;
    $schema_insert .= "$row[2]".$sep;
    
    $schema_insert = str_replace($sep."$", "", $schema_insert);
    $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
    print(trim(str_replace(',', " ", $schema_insert)));
    print "\n";
}

file_put_contents('test.xls', $schema_insert);

$to = "[email protected]";
$from = "[email protected]";
$subject = "test mail";
$separator = md5(date('r', time()));
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "test.xls";         
 
$attachment = chunk_split(base64_encode(file_get_contents('test.xls')));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
 
$body = "--".$separator.$eol;
$headers .= "Content-type:text/plain; charset=iso-8859-1".$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream;      name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment;  filename=\"".$filename."\"".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

if (mail($to, $subject, $body, $headers)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}
?>

Upvotes: 0

Views: 139

Answers (1)

Wisdom Ighofose
Wisdom Ighofose

Reputation: 357

There is no need to reinvent the wheel when you have robust PHP libraries that do this for you.

Using PHPMailer which is one of the popular. You can send string attachment by adding it like so:

$mail->AddStringAttachment($attachment,$filename,$encoding,$type); 

$encoding and $type are optional

You can check here for the full configuration and settings and if you feel you need to be part of the invention, you can either contribute code or funding.

Upvotes: 1

Related Questions