I have a code that generates excel file from mysql database and downloads the file. I want to send that file in my email after the file is downloaded. I am using PHPMailer liabrary for doing this. I tested PHPMailer module individually its working fine, the mail is going but when i attached that module with my existing code where the excel file is downloading its not working as intended. It gives me error "The site can't be reached." If i remove the attchment line from my code it works fine. The file is downloaded and the mail is sent. But i want that file to be attached in my email. Here is my code i'm using
<?php
require("PHPMailer/src/SMTP.php");
require("PHPMailer/src/PHPMailer.php");
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$conn = new mysqli('localhost', 'root', 'root');
mysqli_select_db($conn, 'dailyplay');
$setSql = "SELECT id, name, description FROM genres";
$setRec = mysqli_query($conn, $setSql);
$columnHeader = "ID"."\t"."Name"."\t"."Description";
$setData = '';
while ($rec = mysqli_fetch_row($setRec)) {
$rowData = '';
foreach ($rec as $value) {
$value = '"' . $value . '"' . "\t";
$rowData .= $value;
}
$setData .= trim($rowData) . "\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=INTRANSIT_Report.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo ucwords($columnHeader) . "\n" . $setData . "\n";
sleep(5);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'USERNAME_HERE';
$mail->Password = 'PASSWORD_HERE';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('EMAIL_HERE', 'USERNAME_HERE');
$mail->addAddress('RESIPIENT_HERE', 'USERNAME_HERE');
$mail->addAttachment('/home/winningcodie/Downloads/INTRANSIT_Report.xls', 'Report.xls');
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Rest of the code is working fine. The file is downloaded in excel format. If i uncomment the attachment line of mail it gives me the error "The site can't be reached".