我需要生成日志数据并将其以csv
格式发送到邮件。这是我的代码:
public function sendMail(){
$csv = $this->createCSV();
$subject = $this->getMessage('subject');
$message = $this->getMessage('message');
$headers = $this->generateHeaders($csv, $message);
mail('my_mail@gmail.com', $subject, $message, $headers);
}
private function createCSV(){
$data = $this->getAVG();
$csv = fopen('php://temp', 'w+');
// long statistics generation
fclose($csv);
return $csv;
}
private function generateHeaders($file, $message){
$boundary = md5(uniqid(time()));
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-Type: multipart/mixed; boundary="'.$boundary."\"\r\n";
$headers .= 'From: noreplay@'.$_SERVER['SERVER_NAME']."\r\n";
$headers .= '--'.$boundary."\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "\r\n";
$headers .= chunk_split(base64_encode($message));
$headers .= '--'.$boundary."\r\n";
$headers .= "Content-Type: text/csv; name=\"ServersLogs.csv\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"ServersLogs.csv\"\r\n";
$headers .= "\r\n";
$headers .= chunk_split(base64_encode(readfile($file)));
$headers .= "\r\n--".$boundary."--\r\n";
return $headers;
}
问题是我无法在服务器上保存文件,所以我使用php://,但是在它无法读取文件之后......