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

时间:2017-03-30 11:51:04

标签: php excel

我正在生成一个过滤数据库的Excel工作表,完成此步骤后,我必须向附加此Excel工作表的特定用户发送电子邮件。我能够创建和下载excel文件,但我无法将其作为附件发送。

我在这段代码中缺少什么?

这是我的代码:

<?php
  $link = mysqli_connect("Host", "Username", "Password", "database");
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", 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\r\nReply-To: Sender@gmail.com";
          $headers .= "\r\nContent-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 个答案:

没有答案