如何使用PHP将表单数据作为消息发送?

时间:2016-06-24 04:11:18

标签: php forms contact-form

我需要让我网站的访问者通过PHP发送表单数据(例如联系表单到我的电子邮箱。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

使用GET / POST查询并使用标记 html forms / w3schools 例如

.html page: 
<form method="GET" action="send.php">
<input name="fieldname" type="text" placeholder="You text here...">
<input type="submit" value="Submit">
</form>

send.php
<?php

if (isset($_GET['fieldname']) {
   // you code here..
}

示例通过函数mail()发送电子邮件 about mail() function on php.net

$from = 'fromemailsend@mail';
$to = 'emailtosend@mail';
$subject = 'your subject';
$message = 'your<br>message<br>in html code';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8 \r\n";
$headers .= 'To: Author <' .$to . ' >' . "\r\n";
$headers .= 'From: '.$author.' <'.$from.'>' . "\r\n";
mail($to, $subject, $message, $headers);