【发布时间】:2017-09-26 14:26:18
【问题描述】:
我有一个潜在客户表单,填写完毕后会发送电子邮件通知。如果电子邮件成功,我怎样才能将此数据写入服务器上的 csv?
这是我的代码,现在它什么也不做,填写表格时电子邮件结束,但没有任何内容写入 CSV。
逻辑是(这可能是错误的)如果它收到 succ 消息,打开 file.csv 并将 $email_f 写入其中并关闭 csv。
我做错了什么?
<?php
if($_POST['email']){
$email_f = $_POST['email'];
$to = "myemail@example.com";
$subject = "subject";
$message = "<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
</head>
<body>
<strong>Title</strong><hr />
<table align='center' width='99%' border='0' cellpadding='0' cellspacing='0'>
<tr bgcolor='#cccccc'><td width='30%' style='padding:7px 5px; font-weight:bold;'>Email:</td><td style='padding:7px 5px;'>".$email_f."</td></tr>
</table>
</body>
</html>";
//echo $message;
$headers = "From: <from@example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'Bcc:' . "\r\n";
$mail = mail($to, $subject, $message, $headers);
if($mail)
{
echo "succ";
$file = 'file.csv';
$file = fopen($file, 'a');
fputcsv($file, $email_f);
fclose($file);
}
}
?>
我也试过这个:
$list = array ($mail());
$fp = fopen('/file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
好的,越来越近了,我添加了这个打开文件但不向其中写入任何内容的片段,有什么想法吗?:
$list = array(
mail($to, $subject, $message, $headers)
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
【问题讨论】:
-
使用php.net/manual/en/function.error-reporting.php设置捕捉和显示;它显示了什么?
-
^ 这是一个问题顺便说一句,就像你的问题一样,你也喜欢一个答案,对吧?同上 ;-)
-
fputcsv $field 参数必须是数组。尝试将 $email_f 放入数组中
-
很高兴看到输出文件的一些示例,您想要什么以及当前生成的内容。