Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何编写新的CSV文件并作为电子邮件附件发送_Php_Forms_Csv - Fatal编程技术网

Php 如何编写新的CSV文件并作为电子邮件附件发送

Php 如何编写新的CSV文件并作为电子邮件附件发送,php,forms,csv,Php,Forms,Csv,好的,添加PHPMailer后,我现在有以下代码: <?php $name = $_POST['id']; $email = $_POST['username']; $password = md5($_POST['password']); $usergroup = $_POST['usergroup']; $lastsid = $_POST['lastsid']; $fname = $_POST['fname']; $lname = $_POST['lname']; $lastaccess

好的,添加PHPMailer后,我现在有以下代码:

<?php
$name = $_POST['id'];
$email = $_POST['username'];
$password = md5($_POST['password']);
$usergroup = $_POST['usergroup'];
$lastsid = $_POST['lastsid'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$lastaccess = $_POST['lastaccess'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$addone = $_POST['addone'];
$addtwo = $_POST['addtwo'];
$city = $_POST['city'];
$stateprov = $_POST['stateprov'];
$country = $_POST['country'];
$postalcode = $_POST['postalcode'];
$rescom = $_POST['rescom'];
$addbook = $_POST['addbook'];
$subscriptions = $_POST['subscriptions'];
$affilid = $_POST['affilid'];
$affilcommpct = $_POST['affilcommpct'];
$affilsalestodate = $_POST['affilsalestodate'];
$affilcommearned = $POST['affilcommearned'];
$affilcommowed = $_POST['$affilcommowed'];
$affilcommpaid = $_POST['affilcommpaid'];
$whlsalepct = $_POST['whlsalepct'];
$show_currency = $_POST['show_currency'];
$loyaltypoints = $_POST['loyaltypoints'];

$fp = fopen("feed.csv", "w+");
$savestring = $name . "," . $email . "," . $password . "," . $usergroup . "," . $lastsid . "," . $fname . "," . $lname . "," . $lastaccess . "," . $company . "," . $phone . "," . $addone . "," . $addtwo . "," . $city . "," . $stateprov . "," . $country . "," . $postalcode . "," . $rescom . "," . $addbook . "," . $subscriptions . "," . $affilid . "," . $affilcommpct . "," . $affilsalestodate . "," . $affilcommearned . "," . $affilcommowed . "," . $affilcommpaid . "," . $whlsalepct . "," . $show_currency . "," . $loyaltypoints . "\n";
fwrite($fp, $savestring);
fclose($fp);
echo "<h1>Thank you.</h1><p>We will confirm your details and send your new account details via email within the next working day.</p><p><a href='test2.php'>Back to main page</a></p>";

/* email finished file */

require_once('/public_html/xx/class.phpmailer.php');

$email = new PHPMailer();
$email->From      = 'my@emailaddress;
$email->FromName  = 'test name';
$email->Subject   = 'Form Test';
$email->Body      = $bodytext;
$email->AddAddress( 'my@emailaddress' );

$file_to_attach = '/public_html/xx/';

$email->AddAttachment( $file_to_attach, 'formdata.csv' );

return $email->Send();

?>

既然您发布了一个非常小的细节,那么我可以建议您:

而不是这个-总是写在同一个文件上:

$fp = fopen("formdata.csv", "w+");
试试这个:

$date_time = date('Y-m-d H:i:s');
$fp        = fopen("formdata" . $date_time . ".csv", "w+");
fclose($fp)之后

触发电子邮件代码并将当前生成的CSV文件作为附件发送


希望有帮助。如果您想进一步了解,请告诉我。

您将最终导致csv文件损坏,因为您正在将用户提供的原始文本转储到该文件中。e、 g.如果某人的姓氏被输入为
史密斯先生
,会发生什么情况?你应该改为使用。正如@Marc所说,在直接转储用户数据之前,你必须先处理用户数据,因为它可能包含任何东西。下面的答案也不能解决问题。请按照大家的建议去做。因此,您要说的不是使用$fwrite,而是使用fputcsv。我遇到了一个复选框用逗号分隔的问题,但我还没有解决这个问题。我已经尝试过这个问题,我应该在哪里添加它?您是否只在CSV文件中添加一条记录?如果只有一条记录,最好将其作为html内容而不是CSV发送。但是如果你真的需要CSV文件,请阅读这篇文章,@debonator我假设你正确地生成了CSV文件,并且你知道如何通过电子邮件发送CSV文件,因为你没有提到这两点。如果我不清楚,很抱歉:我想知道如何将CSV文件作为附件发送到我同事的电子邮件地址。目前该文件确实覆盖了新条目,但我希望该文件是特定的。这有帮助吗?@debonator从上面的回答中,您清楚了如何每次清除唯一的CSV文件?现在让我演示如何在电子邮件附件中发送CSV文件。请访问。如果仍然不清楚,请告诉我。