douzangdang2225 2017-03-30 11:51
浏览 164

在php中创建并发送excel表作为电子邮件附件

I am generating an excel sheet filtering the database and after completing this step, I have to send an email to particular user attaching this excel sheet. I was able to create and download the excel file however I am not able to send it as an attachment.

What am I missing in this code?

Here is my code:

<?php
  $link = mysqli_connect("Host", "Username", "Password", "database");
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
  }
  else{
    $yesterday = date('d-m-Y',strtotime("-1 days")); //Get yesterday's date
    $file="E-Call_List - ".$yesterday.".xls"; 
    $sql="SELECT DISTINCT a.psemobile, b.activity, a.memid1, b.LoginTime, b.LogoutTime FROM tblMembers a, tblMonitor b WHERE a.memid1 = b.memberid AND b.date ='".$yesterday."'";
    $result = $link->query($sql);
    if ($result->num_rows > 0) {
              $test ="<table border=1>
                    <tr>
                      <th>DATE</th>
                      <th>MEMBER ID</th>
                      <th>LOGIN TIME</th>
                      <th>LOGOUT TIME</th>
                      <th>MOBILE</th>
                      <th>ACTIVITY</th>
                    </tr>";
          echo $test;
          while($row = $result->fetch_assoc())
          {
            echo "<tr>";
              echo "<td>".$yesterday."</td>";
              echo "<td>".$row["memid1"]."</td>";
              echo "<td>".$row["LoginTime"]."</td>";
              echo "<td>".$row["LogoutTime"]."</td>";
              echo "<td>".$row["psemobile"]."</td>";
              echo "<td>".$row["activity"]."</td>";
            echo "<tr>";
          }
          $to = 'rec@outlook.com';
          $subject = 'Test email Partner Solution';
          $random_hash = md5(date('r', time()));
          $headers = "From: Sender@gmail.com
Reply-To: Sender@gmail.com";
          $headers .= "
Content-Type: multipart/mixed;";
          $attachment = chunk_split(base64_encode(file_get_contents($file)));
          $message = "<b>Greetings.</b>";
          $message .= "<b>This is a test email.</b>";
          $message = ob_get_clean();
          $mail_sent = mail( $to, $subject, $message, $headers );
    }
    else {
      echo "0 results";
    }
    $link->close();
  }
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法