简体   繁体   English

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

[英]Contact form submission that send email and records data in csv

I have a contact form that on submit displays a thank you message, takes the users entries and emails them to an address but should also take four fields and place it into a CSV. 我有一个联系表单,在提交时会显示一条感谢消息,接受用户输入并通过电子邮件将其发送到一个地址,但还应包含四个字段并将其放入CSV。 Unfortunately I can't get the last part to work no matter what I try. 不幸的是,无论尝试什么,我都无法完成最后的工作。

The thank you message works, an email is sent, but the part I've added at $output (before the if/else) doesn't. 谢谢消息有效,发送了一封电子邮件,但是我在$output添加的部分(在if / else之前)不起作用。

Here's the code I'm using, thanks in advance to anyone who can help with this. 这是我正在使用的代码,在此先感谢任何可以提供帮助的人。

<?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
}
?>

Maybe this will help: 也许这会有所帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM