0

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".

vinay nischal
  • 51
  • 2
  • 8
  • Look at this question https://stackoverflow.com/questions/35997961/file-attachment-with-phpmailer – zajonc Sep 04 '21 at 09:24
  • @zajonc This is not the case with me. PHPMailer is working fine untill the file downloads. I want both operations simultaneously. Fist the file must download and then the same file must be sent to email. If i comment the line of attachment the file is perfectly downloaded and the mail is send with subject and body. But if i uncomment the attachment line of code, Neither the file is downloaded nor the email is sent. It gives error "The site can't be reached". – vinay nischal Sep 04 '21 at 09:31
  • Okay, my mistake. Did you check for warnings and errors if nothing related to the file permissions appears? – zajonc Sep 04 '21 at 09:39
  • "The site can't be reached". This error is coming – vinay nischal Sep 04 '21 at 10:26

2 Answers2

0

Try this :

<?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-move_uploaded_file('/home/winningcodie/Downloads/INTRANSIT_Report.xls',"uploads/INTRANSIT_Report.xls");
        $mail->addAttachment("uploads/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}";
    }

?>
 

  
yecademy
  • 37
  • 7
0

You generate the file dynamically and send it to the browser, which saves it on the same disk, while the script tries to read the file as part of the same process - I'm afraid it won't work (and as you can see it doesn't).

Don't send the file to the browser right away. Save it on disk first and then read it for the e-mail sending process and then return it to the browser.

zajonc
  • 1,935
  • 5
  • 20
  • 25
  • The problem is if i add the attachment which is already there in downloads folder its perfectly working. But if i want to send the currently downloaded file as an attachment its not working. I thing the problem is not with the permissions. – vinay nischal Sep 04 '21 at 09:51
  • When you save file in directory manually it has your permission. If the file is writing by another process then the file permissions may be different (there is different owner). Check the permissions of the downloaded file (including owner and group). – zajonc Sep 04 '21 at 09:56
  • See the video here https://drive.google.com/file/d/1L6Pk4FbZKSK5zynhhreJ6YqoJvJfbfej/view?usp=sharing – vinay nischal Sep 04 '21 at 10:07