Can't receive excel attachment after sending through email












1















I am trying to generate excel file and send it as an attachment through an email. Here code for generating excel file:



/** Include PHPExcel */
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
include('inc/database_connection.php');
$conn = mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);

$sql2 = '
SELECT t2.*
FROM (
SELECT staff_id, projectNo, longname, username, title, process, creation_time as min
FROM archived
WHERE creation_time IN (SELECT min(creation_time) FROM archived)
) t1
RIGHT JOIN (
SELECT staff_id, projectNo, longname, username, title, process, creation_time as max
FROM archived
WHERE creation_time IN (SELECT max(creation_time) FROM archived)
) t2
ON t1.staff_id = t2.staff_id AND
t1.projectNo = t2.projectNo AND
t1.process = t2.process
WHERE t1.staff_id is null
';
$query2 = mysqli_query($conn, $sql2);
// Instantiate a new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
// Initialise the Excel row number
$rowCount = 1;
// Iterate through each result from the SQL query in turn
// We fetch each database result row into $row in turn

while ($data2 = mysqli_fetch_assoc($query2))
{
//$oldest_date=$data2["min"];
$newest_date=$data2["max"];
$staffid=$data2["staff_id"];
$longnm=$data2["longname"];
$usrnm=$data2["username"];
$projNo=$data2["projectNo"];
$ttle=$data2["title"];
$proc=$data2["process"];

$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $staffid);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $longnm);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $usrnm);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $projNo);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, $ttle);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, $proc);
// Increment the Excel row counter
$rowCount++;
}

$file_name = 'ExcelChanges';
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save($file_name.'.xls');


And then i am trying to send email:



$to = 'powqb@mail.com';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$message = 'test';

$headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";
$headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";

$attachment = chunk_split(base64_encode(file_get_contents($file_name.'.xls')));

mail( $to, $subject, $message, $headers );


I get the message but its empty. I got only word test and no attachment inside. What can be a problem?










share|improve this question





























    1















    I am trying to generate excel file and send it as an attachment through an email. Here code for generating excel file:



    /** Include PHPExcel */
    require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
    include('inc/database_connection.php');
    $conn = mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);

    $sql2 = '
    SELECT t2.*
    FROM (
    SELECT staff_id, projectNo, longname, username, title, process, creation_time as min
    FROM archived
    WHERE creation_time IN (SELECT min(creation_time) FROM archived)
    ) t1
    RIGHT JOIN (
    SELECT staff_id, projectNo, longname, username, title, process, creation_time as max
    FROM archived
    WHERE creation_time IN (SELECT max(creation_time) FROM archived)
    ) t2
    ON t1.staff_id = t2.staff_id AND
    t1.projectNo = t2.projectNo AND
    t1.process = t2.process
    WHERE t1.staff_id is null
    ';
    $query2 = mysqli_query($conn, $sql2);
    // Instantiate a new PHPExcel object
    $objPHPExcel = new PHPExcel();
    // Set the active Excel worksheet to sheet 0
    $objPHPExcel->setActiveSheetIndex(0);
    // Initialise the Excel row number
    $rowCount = 1;
    // Iterate through each result from the SQL query in turn
    // We fetch each database result row into $row in turn

    while ($data2 = mysqli_fetch_assoc($query2))
    {
    //$oldest_date=$data2["min"];
    $newest_date=$data2["max"];
    $staffid=$data2["staff_id"];
    $longnm=$data2["longname"];
    $usrnm=$data2["username"];
    $projNo=$data2["projectNo"];
    $ttle=$data2["title"];
    $proc=$data2["process"];

    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $staffid);
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $longnm);
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $usrnm);
    $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $projNo);
    $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, $ttle);
    $objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, $proc);
    // Increment the Excel row counter
    $rowCount++;
    }

    $file_name = 'ExcelChanges';
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save($file_name.'.xls');


    And then i am trying to send email:



    $to = 'powqb@mail.com';
    $subject = 'Test email with attachment';
    $random_hash = md5(date('r', time()));
    $message = 'test';

    $headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";
    $headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";

    $attachment = chunk_split(base64_encode(file_get_contents($file_name.'.xls')));

    mail( $to, $subject, $message, $headers );


    I get the message but its empty. I got only word test and no attachment inside. What can be a problem?










    share|improve this question



























      1












      1








      1








      I am trying to generate excel file and send it as an attachment through an email. Here code for generating excel file:



      /** Include PHPExcel */
      require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
      include('inc/database_connection.php');
      $conn = mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);

      $sql2 = '
      SELECT t2.*
      FROM (
      SELECT staff_id, projectNo, longname, username, title, process, creation_time as min
      FROM archived
      WHERE creation_time IN (SELECT min(creation_time) FROM archived)
      ) t1
      RIGHT JOIN (
      SELECT staff_id, projectNo, longname, username, title, process, creation_time as max
      FROM archived
      WHERE creation_time IN (SELECT max(creation_time) FROM archived)
      ) t2
      ON t1.staff_id = t2.staff_id AND
      t1.projectNo = t2.projectNo AND
      t1.process = t2.process
      WHERE t1.staff_id is null
      ';
      $query2 = mysqli_query($conn, $sql2);
      // Instantiate a new PHPExcel object
      $objPHPExcel = new PHPExcel();
      // Set the active Excel worksheet to sheet 0
      $objPHPExcel->setActiveSheetIndex(0);
      // Initialise the Excel row number
      $rowCount = 1;
      // Iterate through each result from the SQL query in turn
      // We fetch each database result row into $row in turn

      while ($data2 = mysqli_fetch_assoc($query2))
      {
      //$oldest_date=$data2["min"];
      $newest_date=$data2["max"];
      $staffid=$data2["staff_id"];
      $longnm=$data2["longname"];
      $usrnm=$data2["username"];
      $projNo=$data2["projectNo"];
      $ttle=$data2["title"];
      $proc=$data2["process"];

      $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $staffid);
      $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $longnm);
      $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $usrnm);
      $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $projNo);
      $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, $ttle);
      $objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, $proc);
      // Increment the Excel row counter
      $rowCount++;
      }

      $file_name = 'ExcelChanges';
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
      $objWriter->save($file_name.'.xls');


      And then i am trying to send email:



      $to = 'powqb@mail.com';
      $subject = 'Test email with attachment';
      $random_hash = md5(date('r', time()));
      $message = 'test';

      $headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";
      $headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";

      $attachment = chunk_split(base64_encode(file_get_contents($file_name.'.xls')));

      mail( $to, $subject, $message, $headers );


      I get the message but its empty. I got only word test and no attachment inside. What can be a problem?










      share|improve this question
















      I am trying to generate excel file and send it as an attachment through an email. Here code for generating excel file:



      /** Include PHPExcel */
      require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
      include('inc/database_connection.php');
      $conn = mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);

      $sql2 = '
      SELECT t2.*
      FROM (
      SELECT staff_id, projectNo, longname, username, title, process, creation_time as min
      FROM archived
      WHERE creation_time IN (SELECT min(creation_time) FROM archived)
      ) t1
      RIGHT JOIN (
      SELECT staff_id, projectNo, longname, username, title, process, creation_time as max
      FROM archived
      WHERE creation_time IN (SELECT max(creation_time) FROM archived)
      ) t2
      ON t1.staff_id = t2.staff_id AND
      t1.projectNo = t2.projectNo AND
      t1.process = t2.process
      WHERE t1.staff_id is null
      ';
      $query2 = mysqli_query($conn, $sql2);
      // Instantiate a new PHPExcel object
      $objPHPExcel = new PHPExcel();
      // Set the active Excel worksheet to sheet 0
      $objPHPExcel->setActiveSheetIndex(0);
      // Initialise the Excel row number
      $rowCount = 1;
      // Iterate through each result from the SQL query in turn
      // We fetch each database result row into $row in turn

      while ($data2 = mysqli_fetch_assoc($query2))
      {
      //$oldest_date=$data2["min"];
      $newest_date=$data2["max"];
      $staffid=$data2["staff_id"];
      $longnm=$data2["longname"];
      $usrnm=$data2["username"];
      $projNo=$data2["projectNo"];
      $ttle=$data2["title"];
      $proc=$data2["process"];

      $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $staffid);
      $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $longnm);
      $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $usrnm);
      $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, $projNo);
      $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, $ttle);
      $objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, $proc);
      // Increment the Excel row counter
      $rowCount++;
      }

      $file_name = 'ExcelChanges';
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
      $objWriter->save($file_name.'.xls');


      And then i am trying to send email:



      $to = 'powqb@mail.com';
      $subject = 'Test email with attachment';
      $random_hash = md5(date('r', time()));
      $message = 'test';

      $headers = "From: webmaster@example.comrnReply-To: webmaster@example.com";
      $headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";

      $attachment = chunk_split(base64_encode(file_get_contents($file_name.'.xls')));

      mail( $to, $subject, $message, $headers );


      I get the message but its empty. I got only word test and no attachment inside. What can be a problem?







      php email






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 2:40









      Cœur

      17.6k9105145




      17.6k9105145










      asked Mar 21 '17 at 8:37









      Yevgeniy BagackiyYevgeniy Bagackiy

      513519




      513519
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42921954%2fcant-receive-excel-attachment-after-sending-through-email%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42921954%2fcant-receive-excel-attachment-after-sending-through-email%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Xamarin.form Move up view when keyboard appear

          JBPM : POST request for execute process go wrong