提交表格并通过PHP发送电子邮件

时间:2009-08-21 14:53:10

标签: php javascript forms

我目前正在尝试使用脚本将表单提交到我网站外部的页面,但也会将客户给出的答案通过电子邮件发送给我。 mail()函数对邮件工作正常......但是如何获取这些值并将它们提交到外部页面呢?

感谢您的帮助!

4 个答案:

答案 0 :(得分:2)

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

e.g。

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

答案 1 :(得分:0)

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

编辑:

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

do_post_request(your_url, $_POST);

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

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:-2)

对于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");