发送电子邮件并在 csv 中记录数据的联系表单提交

Contact form submission that send email and records data in csv

我有一个联系表单,在提交时会显示一条感谢消息,获取用户条目并将其通过电子邮件发送到一个地址,但还应该包含四个字段并将其放入 CSV 中。不幸的是,无论我尝试什么,我都无法让最后一部分工作。

感谢消息有效,已发送电子邮件,但我在 $output(在 if/else 之前)添加的部分无效。

这是我正在使用的代码,在此先感谢任何可以提供帮助的人。

<?php

$subject = 'Submission received'; 
$mailto  = ''; 

$firstName      = $_POST['firstName'];
$lastName       = $_POST['lastName'];
$email          = $_POST['email'];
$telephone      = $_POST['telephone'];
$company        = $_POST['companyName'];
$country        = $_POST['country'];
$about          = $_POST['hearAbout'];
$enquiry        = $_POST['enquiry'];

$body = "
<br>
<p>The following information was submitted through the contact form on your website:</p>
<p><b>Name</b>: $firstName $lastName<br>
<b>Email</b>: $email<br>
<b>Phone number</b>: $telephone</br>
<b>Company name</b>: $company<br>
<b>Country</b>: $country<br>
<b>Heard about company via</b>: $about<br>
<b>Enquiry</b>: $enquiry<br></p>
";

// Success Message - PAD THIS OUT
$success = "
<div class=\"\">
    <div class=\"\">
        <h3>Submission successful</h3>
        <p>Thank you.</p> 
    </div>
</div>
";

$headers = "From: $firstName $lastName <$email> \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>$body</body></html>";

$output = $firstName . "t";
$output .= $lastName . "t";
$output .= $email . "t";
$output .= $telephone . "n";
$fp = fopen("data/enquiry.csv", "a");
fwrite($fp, $output);
fclose($fp);

if (mail($mailto, $subject, $message, $headers)) {
        echo "$success"; // success
} else {
    echo 'Form submission failed. Please try again...'; // failure
}
?>

也许这会有所帮助:

$fp = fopen("data/enquiry.csv", "a");
fwrite($fp,"," . $firstName . "," . $lastName . "," . $email . "," . $telephone . "\n");
fclose($fp);