Php电子邮件脚本

时间:2011-11-09 15:03:59

标签: php forms

下面是我的php脚本:我有一个页面,客户端填写表单并通过此表单处理数据并通过电子邮件将数据发送到漂亮的表中。但是变量显示而不是数据。 (即$ name)

请帮忙。

<?php

$fullname=$_POST['fullname'];
$companyname=$_POST['companyname'];
$idnumber=$_POST['idnumber'];
$dob=$_POST['dob'];
$addressone=$_POST['addressone'];
$addresstwo=$_POST['addresstwo'];
$postalcode=$_POST['postalcode'];
$citystate=$_POST['citystate'];
$country=$_POST['country'];
$mobile=$_POST['mobile'];
$homework=$_POST['homework'];
$email=$_POST['email'];
$altemail=$_POST['altemail'];
$dob=$_POST['dob'];
$tc=$_POST['tc'];
$username=$_POST['username'];
$password=$_POST['password'];

//define the receiver of the email
$to = 'test@test.com';
//define the subject of the email
$subject = 'Test HTML email'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: test@test.com\r\nReply-To: test@test.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<span style="font-family: Arial; font-size:16pt; color: #595959;"><strong>New Account Registration</strong></span><p>&nbsp;</p>

<span style="font-family: Arial; font-size:12pt; color: #595959;"> $fullname has filled in the following information:<p>
</span>


<table cellspacing"2" border="0" "cellpadding="5px" style="font-family: Arial; font-size: 11pt; color: #777777;">


<tr>  <!-- Full Name -->
     <td>* Full Name:</td>
     <td><?php echo '$fullname'; ?></td>
</tr>


<tr>  <!-- Company Name -->
     <td>* Company Name:</td>
     <td>$companyname</td>
</tr>

<tr>  <!-- ID Number / Passport Number -->
     <td>* ID Number / Passport No.</td>
     <td>$idnumber</td>
</tr>

<tr>  <!-- Date of Birth -->
     <td>* Date of Birth</td>
     <td>$dob</td>
</tr>

<tr><td><strong>&nbsp;</strong></td></tr>
<tr>  <!-- Address Line 1 -->
     <td>* Address Line 1</td>
     <td>$addressone</td>
</tr>
<tr>  <!-- Address Line 2 -->
     <td>* Address Line 2</td>
     <td>$addresstwo</td>
</tr>
<tr>  <!-- Postal Code  -->
     <td>* Postal Code</td>
     <td>$postalcode</td>
</tr>

<tr>  <!-- City / State  -->
     <td>* City / State</td>
     <td>$citystate</td>
</tr>

<tr>  <!-- Country  -->
     <td>* Country</td>
     <td>$country</td>
</tr>

<tr><td><strong>&nbsp;</strong></td></tr>

<tr>  <!-- Mobile Number  -->
     <td>* Mobile Number</td>
     <td>$mobile</td>
</tr>

<tr>  <!-- Home / Work  -->
     <td>* Home / Work No.</td>
     <td>$homework</td>
</tr>

<tr>  <!-- Email Address  -->
     <td>* Email Address:</td>
     <td>$email</td>
</tr>

<tr>  <!-- Alternate Address  -->
     <td>* Alternate Address:</td>
     <td>$altemail</td>
</tr>
<tr><td><strong>&nbsp;</strong></td></tr>
<tr>  <!-- Username -->
     <td>* Username:</td>
     <td>$username</td>
</tr>
<tr>  <!-- Password -->
     <td>* Password:</td>
     <td>$password</td>
</tr>
<tr>  <!-- Terms and Conditions -->
     <td colspan="2">$tc Agreed to the <a href="http://www.test.html">Terms & Conditions</a>.</td>
     <td></td>
</tr>
</table>
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

3 个答案:

答案 0 :(得分:2)

你到处都缺少<?php echo $variable ?>。 PHP代码仅在<?php ?>括号对内保存时才会执行。否则它只是“文本”并直接输出。表单中没有任何变量被视为PHP代码,因此以文本形式出现,而不是其内容。

答案 1 :(得分:0)

也许每个var都这样做:

<?php echo $varname; ?>

答案 2 :(得分:0)

所有人的一个例子:

<span style="font-family: Arial; font-size:12pt; color: #595959;"> $fullname has filled in the following information:<p>

这是错误的。

变量需要打印(在服务器端的PHP脚本中使用echo或print函数)。

但是,如果您的网络服务器上的php设置允许,您可以使用简短形式&lt;?= $ fullname?&gt;而不是$ fullname,这是没有输入echo / print函数的语法糖。