单个html表单电子邮件发送与csv发布simltaniously

时间:2015-03-11 03:57:36

标签: php html email csv

我正在提交带有php代码的HTML表单,它会发送电子邮件。我需要的是发送电子邮件,在csv文件中更新细节(同时使用单个提交按钮。发送电子邮件的PHP代码是:

<?php
    if(!isset($_POST['submit']))
           {
        header('Location: error.html');
        exit;
           }
    $name = $_POST['name'];
    $place = $_POST['place'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $email_body = 
              "NAME:     $name\n".
              "PLACE:    $place\n".
              "EMAIL-ID: $email\n\n".
              "Message:  $message\n" ;
    $to = "to@xyz.com";
    //Send the email!
    mail($to, $email_body);
    //done
    header('Location: thank.html');
 ?>

当我添加以下代码时,它无效:

$fp = fopen("datacsv.csv", "a+");

fputcsv($name, $place,  $email,  $message);
fclose($fp);

任何机构都可以指导我纠正代码。

1 个答案:

答案 0 :(得分:0)

函数fputcsv()的第一个参数是文件句柄,第二个参数是数组,所以试试这个:

<?php
if(!isset($_POST['submit']))
{
    header('Location: error.html');
    exit;
}
$name = $_POST['name'];
$place = $_POST['place'];
$email = $_POST['email'];
$message = $_POST['message'];

$email_body = "NAME:     $name\n".
              "PLACE:    $place\n".
              "EMAIL-ID: $email\n\n".
              "Message:  $message\n";

$to = "to@xyz.com";

//Send the email!
mail($to, "My Subject", $email_body);

$fp = fopen("datacsv.csv", "a+");
fputcsv($fp, array($name, $place,  $email,  $message));
fclose($fp);

//done
header('Location: thank.html');
?>

请务必在header()函数之前添加代码。另外,检查文件datacsv.csv上的权限是否正确。