【问题标题】:How can I use $POST in PHP to automatically collect data from HTML5 inputs and send through e-mail?如何在 PHP 中使用 $POST 自动从 HTML5 输入中收集数据并通过电子邮件发送?
【发布时间】:2016-02-03 05:40:04
【问题描述】:

我有一份包含大量输入字段的问卷。到目前为止,我启动了一个 PHP 脚本将其发送到我的电子邮件,如下所示。

<?php 

$First_Name= $_POST['First_Name'];
$Last_Name= $_POST['Last_Name'];
$E_Mail= $_POST['E_Mail'];
$FB_Name= $_POST['FB_Name'];
$Twitter_Name= $_POST['Twitter_Name'];
$Country= $_POST['Country'];
$State_Province= $_POST['State_Province'];
$City= $_POST['City'];
$Phone_01= $_POST['Phone_01'];
$Phone_02= $_POST['Phone_02'];
$Phone_03= $_POST['Phone_03'];
$How_did_you_hear_about_my_Company= $_POST['How_did_you_hear_about_my_Company'];
$Operating_your_Business= $_POST['Operating_your_Business'];
$Name_for_your_Business= $_POST['Name_for_your_Business'];
$Whats_the_name_of_your_Business= $_POST['Whats_the_name_of_your_Business'];
$What_type_of_Business= $_POST['What_type_of_Business'];
$Type_of_Business_you_run= $_POST['Type_of_Business_you_run'];
$How_long_you_been_building= $_POST['How_long_you_been_building'];
$How_long_you_been_in_business= $_POST['How_long_you_been_in_business'];
$What_do_you_plan_to_offer= $_POST['What_do_you_plan_to_offer'];
$What_do_you_offer= $_POST['What_do_you_offer'];
$List_all_products_you_offer['List_all_products_you_offer'];

$from = 'From: Your_New_Client';
$to = 'Optiqvision@gmail.com';
$subject = 'A New Questionnaire';

$body = "Name: $First_Name $Last_Name
\n E-Mail: $E_Mail
\n FB Name: $FB_Name
\n Twitter Name: $Twitter_Name
\n Country: $Country
\n State/Province: $State_Province
\n City: $City
\n Phone Number: $Phone_01 $Phone_02 $Phone_03
\n How did you hear about my Company?:\n\n $How_did_you_hear_about_my_Company
\n Where are you in terms of opperating your Business?: $Operating_your_Business
\n Have you come up with a name for your Business?: $Name_for_your_Business
\n What's the name of your Business?: $Whats_the_name_of_your_Business
\n What type of Business will you be running?: $What_type_of_Business
\n What type of Business do you run?: $Type_of_Business_you_run
\n How long have you been Building your Business?: $How_long_you_been_building
\n How long have you been in Business?: $How_long_you_been_in_business
\n What do you plan to offer?: $What_do_you_plan_to_offer
\n What do you offer?: $What_do_you_offer
\n List of products: $List_all_products_you_offer 
";

?>

<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

这可以正常工作,但我想知道如何告诉 PHP 自动收集所有数据,而不必逐个处理每个元素(比上面表示的要多得多)。

我遇到了一个用于存储到 SQL 数据库的脚本,但不是用于电子邮件的脚本。我尝试将它用于 SQL,但由于它创建了多少列而遇到了问题。

除此之外,我还有另一项任务要处理,即允许用户添加因用户而异的字段,因此我需要 $POST 方法灵活,以便它可以发送结果。以我到目前为止的编码方式,当用户添加额外的字段时,无法发布它们。

我在该网站上进行了搜索,到目前为止,这是我遇到的唯一与此问题相关的内容,但并不真正理解所讲的内容。 PHP: Possible to automatically get all POSTed data?

如果这确实是我正在尝试做的事情的答案,有人可以再分解一下并告诉我它是如何工作的吗?

【问题讨论】:

    标签: php html email


    【解决方案1】:

    你可以这样试试:

    $body="";
    foreach ($_POST as $key => $value) {
        $body.= str_replace("_"," ",$key).": ". $value."\n";
    }
    

    前提是$key 包含您需要的描述性信息,并在您的问题中使用下划线 (_) 分隔的单词。

    【讨论】:

    • 太棒了!!!!唯一的问题是
      出现在电子邮件中而不是开始一个新行,所以它都是一个长段落。
    • &lt;br/&gt; 替换为\n。如果要使用&lt;br/&gt;,需要指定content-type header
    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 2012-10-25
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多