【问题标题】:Submit a form and email it with PHP提交表单并使用 PHP 通过电子邮件发送
【发布时间】:2009-08-21 14:53:09
【问题描述】:

我目前正在尝试获取一个脚本,以将表单提交到我网站外部的页面,但也会通过电子邮件将客户给出的答案发送给我。 mail() 函数在邮件中运行良好......但是我如何获取这些值并将它们提交到外部页面?

感谢您的帮助!

【问题讨论】:

    标签: php javascript forms


    【解决方案1】:

    如果您将表单提交到您的脚本,可以先发送电子邮件,然后使用cURL 向外部页面发出 HTTP 请求,发布您要发送的值。如果外部网站依赖于用户拥有的任何 cookie,这将不起作用,因为请求是从您的网络服务器发出的。

    例如

    <?php
    //data to post
    $data = array( 'name' => 'tom', 'another_form_field'=>'a' ); 
    
    //external site url (this should be the 'action' of the remote form you are submitting to)
    $url = "http://example.com/some/url";  
    
    $curl = curl_init($url);
    
    curl_setopt($curl, CURLOPT_POST, 1);  
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    
    //make curl return the content returned rather than printing it straight out
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    
    $result = curl_exec($curl);
    
    if ($result === false) {
        //curl error   
    }
    
    curl_close($curl);
    
    //this is what the webserver sent back when you submitted the form
    echo $result;
    

    【讨论】:

    • 不想问,但我该怎么做呢?
    【解决方案2】:

    您将不得不挖掘外部表单的来源以确定相关字段的 HTML name 以及该表单是使用 GET 还是 POST 提交的。

    如果表单使用 GET 方法,您可以轻松生成与实际表单相同的查询字符串:http://example.com/form.php?name1=value1&amp;name2=value2 ...

    另一方面,如果表单使用 POST 方法,则必须使用 cURL 库 (http://us2.php.net/curl) 之类的东西生成 HTTP POST 请求。

    【讨论】:

      【解决方案3】:

      您可以从用于发送电子邮件的脚本发送自定义 HTTP POST 请求。尝试fsockopen 建立连接,然后发送您自己的 HTTP 请求,其中包含您刚刚从表单收到的数据。

      编辑:

      更具体一点。 this 示例向您展示了如何发送简单的 HTTP POST 请求。只需像这样使用您的 $_POST 数组播种它:

      do_post_request(your_url, $_POST);
      

      这应该可以解决问题。之后,您可以选择评估响应以检查是否一切正常。

      【讨论】:

        【解决方案4】:

        对于 POST,您需要将外部页面设置为处理操作:

        <form action="http://external-page.com/processor.php" method="POST">
            <!-- Form fields go here --->
        </form>
        

        如果是 GET,您可以将表单方法更改为 GET,或者创建自定义查询字符串:

        <a href="http://external-page.com/processor.php?field1=value1&field2=value2">submit</a>
        

        编辑:我刚刚意识到您可能希望从您的 PHP 处理类中发送这些。在这种情况下,您可以使用自定义查询字符串设置位置标头:

        header("Location: http://external-page.com/processor.php?field1=value1&field2=value2");
        

        【讨论】:

        • 不,这不是问题所在。目前,表单已经执行上述操作以发送到其中具有邮件功能的提交表单。那么我如何将它也提交到外部页面呢?
        猜你喜欢
        • 2019-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多