0
votes

I am able to create the excel file, it is saved in the downloads folder. I am able to send the mail with the correct subject, body etc... The excel file isn't attaching in the mail, please help me on this.

Edit : The AddAttachment() Statement returns false

//php code below in tags

include 'PHPExcel.php';

require_once ("C:\\xampp\\htdocs\\excel_mailer\\PHPMailer-master\\class.phpmailer.php");

$path           =   "C:\\Users\\Manish\\Downloads";
$file_name      =   $_GET['file_name'];

$DB_server      =   "localhost";
$DB_username    =   "root";
$DB_password    =   "";
$DB_dbname      =   "timetracker";
$DB_tablename   =   "tt_users";

$conn           =   mysqli_connect ($DB_server, $DB_username, $DB_password, $DB_dbname);

if (mysqli_connect_errno()) {
    die ("Connection Failed!");
}

$sql    =   "SELECT *";
$sql    .=  " FROM $DB_tablename";

if ($result = mysqli_query($conn,$sql));

else die("Couldn't execute query:<br />" . mysqli_error(). "<br />" . mysqli_errno());

$file_ending = ".xls";

$filename = $file_name.$file_ending;

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");



/*******Start of Formatting for Excel*******/

$sep        = "\t"; //tabbed character
$flag       = true;
$col_names  = "";

while($row = mysqli_fetch_assoc($result)) {
    $data_insert    = "";
            foreach($row as $field=>$data) {
        if ($flag)
            $col_names      .= $field.$sep;
        if(!isset($data))
            $data_insert    .= "NULL".$sep;
        elseif ($data != "")
            $data_insert    .= $data.$sep;
        else
            $data_insert    .= "".$sep;
    }

    if ($flag) {
        $flag       = !$flag;
        $col_names  .= "\t";
        echo (trim($col_names));
        echo "\n";
    }
    $data_insert    .= "\t";
    echo (trim($data_insert));
    echo "\n";
}

$email = new PHPMailer();

$email->From      = '[email protected]';
$email->FromName  = 'Name';
$email->Subject   = 'PFA excel file';
$email->Body      = 'Regards';
$email->AddAddress( '[email protected]' );

$file_to_attach = $path."\\".$filename;

//echo $path."\\".$filename;

$email->AddAttachment( $file_to_attach );

$email->Send();
1
Why are you calling header()? It has nothing to do with email. You need to check your paths and also the return value from addAttachment().Synchro
@Synchro header() is for the excel file. I have checked the file path.. it gets saved in the downloads folder, i have commented out an echo statement in the end of the code just to double check... The AddAttachment statement is returning false :(Anishka Aggarwal
Yes, but you don't need to call header() unless you're sending the excel file to the browser as well as sending via email. It has no effect on the email, that's all handed by PHPMailer. If addAttachment is returning false, your path is probably wrong or lacks permissions. Also you've based your code on an obsolete PHPMailer example and are probably using an old version. Get the latest.Synchro
@Synchro I just commented out the first header statement and moved the 2nd one to just before initialisation of $email.. it worked once file got attached once, but not working again :/Anishka Aggarwal
Didn't I make it clear? You do not need to call header() at all. It has nothing to do with email. Check everything - trying random things and hoping is not a viable debugging technique.Synchro

1 Answers

0
votes

Check if the file exist, maybe you have an error in the path, or the file is not readable.

$file_to_attach = $path."\\".$filename;    
if (!file_exists ( $file_to_attach ) die ("File $file_to_attach not readable");    
else $email->AddAttachment( $file_to_attach );