2014-11-22 81 views
0

我想通过使用PHP将多个变量从我的html表单发送到我的电子邮件。问题是我对PHP没有任何了解。我找到了一个简单的表单,但它只包含3个变量,主题,电子邮件和消息。但是这一次我有7.HTML联系表格,PHP代码

这是代码。

<?php 

$projectname = htmlentities($_POST['projectname']); 
$projectemail = trim(strip_tags($_POST['projectemail'])); 
$projectphone = htmlentities($_POST['projectphone']); 
$projectcompany = htmlentities($_POST['projectcompany']); 
$projecttype = trim(strip_tags($_POST['projecttype'])); 
$projecttimeline = htmlentities($_POST['projecttimeline']); 
$aboutproject = htmlentities($_POST['aboutproject']); 

$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}"; 


$subject = $projecttype; 
$to = '[email protected]'; 

$body = <<<HTML 
$message 
HTML; 

$headers = "From: $projectemail\r\n"; 
$headers .= "Content-type: text/html\r\n"; 


mail($to, $subject, $body, $headers); 


header('Location: thanks.html'); 
?> 

以及相应的HTML

<form id="formproject" action="thank_you_project.php" method="post"> 

<label for="name">Name</label> 
<input type="text" id="projectname" name="name"> 

<label for="email">Email</label> 
<input type="text" id="projectemail" name="email"> 

<label for="phone">Phone</label> 
<input type="text" id="projectphone" name="phone"> 

<label for="company">Company</label> 
<input type="text" id="projectcompany" name="company"> 

<label for="typeofproject">Type of project</label> 
<input type="text" id="projecttype" name="typeofproject"> 

<label for="timeline">Timeline</label> 
<input type="text" id="projecttimeline" name="timeline"> 

<label for="message">Message</label> 
<textarea name="message" id="aboutproject" cols="30" rows="10"></textarea> 

<input type="submit" id="projectsend" value="Send"></input> 
</form> 

</div> <!-- end form --> 

编辑的PHP,离开了身体,并把邮件消息,而不是,仍然没有工作。

<?php 

$projectname = htmlentities($_POST['projectname']); 
$projectemail = trim(strip_tags($_POST['projectemail'])); 
$projectphone = htmlentities($_POST['projectphone']); 
$projectcompany = htmlentities($_POST['projectcompany']); 
$projecttype = trim(strip_tags($_POST['projecttype'])); 
$projecttimeline = htmlentities($_POST['projecttimeline']); 
$aboutproject = htmlentities($_POST['aboutproject']); 

$message = "{$projectname}{$projectphone}{$projectcompany}{$projecttimeline}{$aboutproject}"; 


$subject = $projecttype; 
$to = '[email protected]'; 


$headers = "From: $projectemail\r\n"; 
$headers .= "Content-type: text/html\r\n"; 


mail($to, $subject, $message, $headers); 


header('Location: thanks.html'); 
?> 
+0

你能显示你的html吗? – peppeocchi 2014-11-22 11:24:46

回答

0

尝试此,

<?php 
$to = '[email protected]'; 
$projectname = htmlentities($_POST['projectname']); 
$projectemail = trim(strip_tags($_POST['projectemail'])); 
$projectphone = htmlentities($_POST['projectphone']); 
$projectcompany = htmlentities($_POST['projectcompany']); 
$projecttype = trim(strip_tags($_POST['projecttype'])); 
$projecttimeline = htmlentities($_POST['projecttimeline']); 
$aboutproject = htmlentities($_POST['aboutproject']); 
$header = "From: $projectemail \r\n"; 
$subject = $projecttype; 
$message = " 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<p>Your HTML</p> 
Project Name : $projectname 
<br/> 
Project Email : $projectemail 
<br/> 
Project Phone : $projectphone 
... 
... 
... 
... 
</body> 
</html> 
"; 
$message .= ""; 
$header .= "MIME-Version: 1.0\r\n"; 
$header .= "Content-type: text/html\r\n"; 
$retval = mail ($to,$subject,$message,$header); 
if($retval == true) 
{ 
header('Location: thanks.html'); 
exit(); 
} 
?> 
} 

HTML

<form id="formproject" action="thank_you_project.php" method="post"> 

<label for="name">Name</label> 
<input type="text" id="projectname" name="projectname"> 

<label for="email">Email</label> 
<input type="text" id="projectemail" name="projectemail"> 

<label for="phone">Phone</label> 
<input type="text" id="projectphone" name="projectphone"> 

<label for="company">Company</label> 
<input type="text" id="projectcompany" name="projectcompany"> 

<label for="typeofproject">Type of project</label> 
<input type="text" id="projecttype" name="projecttype"> 

<label for="timeline">Timeline</label> 
<input type="text" id="projecttimeline" name="projecttimeline"> 

<label for="message">Message</label> 
<textarea name="aboutproject" id="aboutproject" cols="30" rows="10"></textarea> 

<input type="submit" id="projectsend" value="Send"></input> 
</form> 

</div> <!-- end form --> 
+0

当我使用此代码时,我发送到一个空白页面,并收到一封电子邮件,说明由于没有收件人而发送失败。 :/ – 2014-11-22 11:55:57

+0

对不起,需要添加* $到='[email protected]'; * – ARJUN 2014-11-22 11:57:44

+0

我试过你的方法,但是现在当我收到邮件时,我只能得到字符串(比如Project Name:),没有主题也没有收件人:/我不知道有什么问题我看着PHP和HTML,希望能找到某种错误,但我找不到任何东西 – 2014-11-22 12:38:17

0

添加所有变量成$body(这是mail函数的第三个PARAM)。

//$body = <<<HTML 
//$message <br> 
//$projectname <br> 
//$projectcompany<br> 
//... 
//HTML; 

第二种方法是直接将这些变量添加到第三个邮件参数中。

mail($to, $subject, $message, $headers); 
// this code is updated as I wrote in my comment below. 
+0

因此,如果我将所有变量放入消息中,然后仅在消息体中发布消息是不够的。 – 2014-11-22 11:29:27

+0

你把它们放到'$ body'(我的第一个代码段),或者直接到'mail'函数(第二个片段)。两者都可以工作(我只使用两个变量来向你展示如何,你也需要添加其他变量)。 – panther 2014-11-22 11:31:38

+0

谢谢你,我会定义尝试它。 – 2014-11-22 11:33:28

0

形式贴输入的值是使用 'name' 属性的值标识,

<form id="formproject" action="thank_you_project.php" method="post"> 

    <label for="name">Name</label> 
    <input type="text" id="projectname" name="name"> 

    <label for="email">Email</label> 
    <input type="text" id="projectemail" name="email"> 

    <label for="phone">Phone</label> 
    <input type="text" id="projectphone" name="phone"> 

    <label for="company">Company</label> 
    <input type="text" id="projectcompany" name="company"> 

    <label for="typeofproject">Type of project</label> 
    <input type="text" id="projecttype" name="typeofproject"> 

    <label for="timeline">Timeline</label> 
    <input type="text" id="projecttimeline" name="timeline"> 

    <label for="message">Message</label> 
    <textarea name="message" id="aboutproject" cols="30" rows="10"></textarea> 

    <input type="submit" id="projectsend" value="Send"></input> 
</form> 

<!-- end form --> 

    <?php 
     $projectname = htmlentities($_POST['name']); 
     $projectemail = trim(strip_tags($_POST['email'])); 
     $projectphone = htmlentities($_POST['phone']); 
     $projectcompany = htmlentities($_POST['company']); 
     $projecttype = trim(strip_tags($_POST['typeofproject'])); 
     $projecttimeline = htmlentities($_POST['timeline']); 
     $aboutproject = htmlentities($_POST['message']); 
    ?>