电子邮件中的PHP附件为空 [英] PHP attachment in email is empty

查看:315
本文介绍了电子邮件中的PHP附件为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从数据库获取数据,并将其显示在可下载并通过电子邮件发送给某人的CSV文件中。我已经设法使可下载文件工作,它显示所有正确的数据。它还会向所需的人发送一个CSV文件,但该CSV文件为空,并且其中不显示任何数据。

Im trying to take data from the database and display it in a CSV file which is both downloadable and emailed to someone. I have managed to get the downloadable file working and it displays all of the correct data. It also sends a CSV file to the necessary person but that CSV file is empty and no data is displayed in it.

这是我的代码:

    $myroot = "../../";
    include($myroot . "inc/functions.php");
        // output headers so that the file is downloaded rather than displayed
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename=surveys.csv');
        $output = fopen('php://output', 'w');
// Create CSV file
fputcsv($output, array('Name', 'Branch', 'Website','Company', 'Question1', 'Question2', 'Question3', 'Question4', 'Question5'));

$mysql_connection = db_connect_enhanced('*****','*****','*****','*****');
$query='SELECT * FROM *****.*****';
$surveys = db_query_into_array_enhanced($mysql_connection, $query);
$count = count($surveys);
$data = array();
    for($i=0; $i<=$count; $i++){
    $data[] = array($surveys[$i]['FeedbackName'], $surveys[$i]['BranchName'], $surveys[$i]['FeedbackWebsite'], $surveys[$i]['FeedbackCompany'], $surveys[$i]['Question1'], $surveys[$i]['Question2'], $surveys[$i]['Question3'], $surveys[$i]['Question4'], $surveys[$i]['Question5']);  
}

foreach( $data as $row )  
{  
    fputcsv($output, $row, ',', '"');  
}  
fclose($output);  

$encoded = chunk_split(base64_encode($output));

// create the email and send it off

$subject = "File you requested from RRWH.com";
$from = "*****@*****.com";
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-Type: multipart/mixed;
   boundary="----=_NextPart_001_0011_1234ABCD.4321FDAC"' . "\n";

$message = '

This is a multi-part message in MIME format.

------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: text/plain;
       charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hello

We have attached for you the PHP script that you requested from http://rrwh.com/scripts.php
as a zip file.

Regards

------=_NextPart_001_0011_1234ABCD.4321FDAC
Content-Type: application/octet-stream;  name="';

$message .= "surveys.csv";
$message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$message .= "surveys.csv";
$message .= '"

';
$message .= $encoded;
$message .= '

------=_NextPart_001_0011_1234ABCD.4321FDAC--

';
mail("*****@*****.com", $subject, $message, $headers, "-f$from");

我花了一天半我不能看到这个问题,有人可以告诉我为什么附加的CSV文件是空的?

I've spent a day and a half on this but I cant see the problem. Could someone please point it out to me as to why the attached CSV file is empty?

我得到了一种绝望和压力:

i'm getting kind of desperate and stressed out :( please someone help me.

推荐答案

这将帮助你看看你的错误日志或至少设置

It would have helped you taking a look at your error logs or at least setting

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

,因为它会告诉你base64_encode需要一个字符串,但$ output是一个资源。

as that would have told you that base64_encode expects a string but $output is a resource.

尝试设置

ob_start(); 

到开头和

$output = ob_get_flush();

$ encoded lines。

between your fclose & $encoded lines.

尚未尝试过邮件,但至少应该能帮助你:)

haven't tryed the mail yet but this should help you at least a bit :)

我尝试这个代码,一切运行良好:

I tryed this code and everything worked well:

<?php
ob_start();
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=surveys.csv');

$output = fopen('php://output', 'w');
fputcsv($output, array('Name', 'Branch', 'Website','Company', 'Question1', 'Question2', 'Question3', 'Question4', 'Question5'));
$data = array();
$data[] = array('Name', 'Branch', 'Website','Company', 'Question1', 'Question2', 'Question3', 'Question4', 'Question5');

foreach( $data as $row )
{
    fputcsv($output, $row, ',', '"');
}
fclose($output);

$output = ob_get_flush();

$encoded = chunk_split(base64_encode($output));

这篇关于电子邮件中的PHP附件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆