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();
addAttachment()
. – Synchro