2013-02-05 44 views
0

在這裏我有一個代碼,如從index.php獲取數據,如果用戶輸入數據,它將顯示如表格,以及它應該去電子郵件到一些預定義的郵件。但我在這裏獲取數據動態,我試圖用$ _REQUEST和$ _POST兩種方法來獲取數據,但在郵件功能我試圖改變消息參數,但由PHP標記它沒有采取和顯示一些語法錯誤。如何發送HTML動態表格作爲郵件在PHP?

請到通過代碼在這裏

<?php 
$to="[email protected]"; 
$fn="Fisrt Name"; 
$ln="Last Name"; 
$name=$fn.' '.$ln; 
$from="[email protected]"; 
$subject = "Welcome to Website"; 

include('newsmtp/smtpwork.php'); 

?> 
<?php 

$message = 'Dear $firstName, 


Your Welcome Message.'.' 
<table border=1> 

    <tr> 
     <td>First Name:</td> 
     <td><?php $firstName=$_POST['firstname']; 
     echo $firstName;?></td> 
    </tr> 

    <tr> 
     <td>Last Name:</td> 
     <td><?php $lastname=$_POST['lastname']; 
     echo $lastname;?></td> 

    </tr> 

    <tr> 

     <td>Title:</td> 
     <td><?php $title=$_POST['title']; 
     echo $title;?></td> 
    </tr> 

    <tr> 

     <td>Address:</td> 
     <td><?php $address=$_POST['address']; 
     echo $address;?></td> 
    </tr> 

    <tr> 

     <td>Phone Number:</td> 
     <td><?php $phone=$_POST['phone']; 
     echo $phone;?></td> 
    </tr> 


    <tr> 

     <td>Course Name:</td> 
     <td><?php $course=$_POST['coursename']; 
     echo $course;?></td> 

    </tr> 

    <tr> 

     <td>Website:</td> 
     <td><?php $website=$_POST['website']; 
     echo $website;?></td> 

    </tr> 

</table> 

Thanks 
xxxxxxxxxxxx 
'; 
?> 
+0

使用[phpMailer](http://code.google.com/a/apache-extras.org/p/phpmailer/)。網站上提供的說明。 – SDC

回答

2

試試這個方法,分配貼值的變量,在$消息變量之外,打印您的$消息變量中的變量

$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$title  = $_POST['title']; 
$address = $_POST['address']; 
$phone  = $_POST['phone']; 
$course  = $_POST['course']; 
$website = $_POST['website']; 


    $message = 'Dear '.$firstName.', 
    Your Welcome Message.'.' 
    <table border=1> 
     <tr> 
      <td>First Name:</td> 
      <td>'.$firstname.' </td> 
     </tr> 
     <tr> 
      <td>Last Name:</td> 
      <td>'.$lastname.'</td> 
     </tr> 
    <tr> 
     <td>Title:</td> 
     <td>'.$title.'</td> 
    </tr> 
    <tr> 
     <td>Address:</td> 
     <td>'.$address.'</td> 
    </tr> 
    <tr> 
     <td>Phone Number:</td> 
     <td>'.$phone.'</td> 
    </tr> 
    <tr> 
     <td>Course Name:</td> 
     <td>'.$course.'</td> 
    </tr> 
    <tr> 
     <td>Website:</td> 
     <td>'.$website.'</td> 
    </tr> 
</table> 
Thanks 
xxxxxxxxxxxx 
'; 
?> 




$to  ="[email protected]"; 
$subject = "Welcome to Website"; 
$headers = 'From: xxxxxx <[email protected]>' . "\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\r\n"; 

$sent = @mail($to,$subject,$message,$headers); 

     if ($sent) { 
      return true; 
     } else { 
      return false; 
     } 

也添加必要的標題發送HTML郵件爲Vinoth Babu,Naveen建議

+0

但它顯示郵件中的總表,表示尊敬的$ firstname, 您的歡迎信息。 <表邊界= 1> \t \t \t \t \t \t \t​​名字: \t \t \t​​$的firstName。 \t \t \t \t \t \t \t 感謝 XXXXXX – user1696874

+0

對於只需要添加標題,跟隨其他兩個AN –

+0

我沒有得到頭在這下面的代碼 – user1696874

0

發送HTML內容/桌子,你必須添加以下在該郵件的標題,

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

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

請參考下面的鏈接,

Mail Function

1
$to = "[email protected]"; 
$subject = $name ." is contact "; 

$headers = "MIME-Version: 1.0" . "\n"; 
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\n"; 
@mail($to,$subject,$message,$headers); 

套頭以MIME版本&類型的信息,如文本/ HTML的