2017-01-13 58 views
1
<?php  
    while($row = $result->fetch_assoc()) 
    { 
     echo "<br> ". $row["description"]. " <br>"; 
    } 
?> 

當用戶輸入名稱並根據該名稱提交時,結果將從數據庫顯示在單獨的頁面中。我添加了郵件功能。郵件功能正在工作。但是,如何發郵件,PHP如何通過郵件從數據庫發送提取數據

顯示該結果(從獲取數據庫中的數據)
$subject2 = "Copy of your form submission"; 
$message = $row["description"]; 

mail($to,$subject,$message,$headers); 
  • 郵件在發送空。它不顯示從數據庫中獲取數據。
+4

'$ subject2 =/= $ subject'難道這個簡單 – RiggsFolly

+0

那你有一個名爲'$ headers'的變量,如果有的話它包含什麼 – RiggsFolly

+0

@RiggsFolly $ headers =「From:」。從$; $ subject2 =「表單提交副本」; $ to ='mymailid'; $ message = $ row [「description」];郵件($到,$主題,$消息,$頭);這裏的郵件功能正在工作。郵件中未添加提取數據的方式。它作爲空郵件 –

回答

-1

第一:

var_dump($result->fetch_assoc());exit; 

如果數組包含一個值,然後用這個去:

<?php  
    while($row = $result->fetch_assoc()) 
    { 
     echo "<br> ". $row["description"]. " <br>"; 
    } 
? 

在這裏,你得到的結果一個關聯數組。在這樣的變量中存儲它:

//store data in Description variable 
$description=''; 
<?php  
    while($row = $result->fetch_assoc()) 
    { 
     $description=$description."<br>".$row["description"]."<br>"; 
    } 
?> 

//send mail 
mail($to,$subject,$description,$headers); 
+0

'$ message'中的內容; –

+0

謝謝。我在上面添加了兩個代碼表示結果正在顯示。但不發送到郵件。如果我添加$ description =''; <?php while($ row = $ result-> fetch_assoc()) { $ description = $ description。「
」。$ row [「description」]。「
」; } ?> - MAil正在發送,但結果不顯示。但我需要的結果應顯示以及需要發送郵件也。 –

1

嘗試用這種

$to = '[email protected]'; 
$subject = "Copy of your form submission"; 

$headers = "MIME-Version: 1.0\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
$headers .= "Content-Transfer-Encoding: 8bit\n"; 
$headers .= "From: [email protected]\n"; 
$message = $row["description"]; 

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

我已經試過這個代碼。但郵件是空的發送。它沒有采取MySQL獲取data.data正在顯示。 $ message = $ row [「description」];但不發送到郵件ID。 $消息不發送到郵件。 –

0

您可以使用另一個和發送電子郵件的最佳方法,swiftmailer可以在作曲工作,包括髮送電子郵件類。 您可以使用SMTP發送,並在所有服務谷歌,雅虎,...點收到垃圾郵件文件夾,只是在收件箱中

//Create the Transport. 
    $transport = \Swift_SmtpTransport::newInstance('smtp.ddd.com', 587, 'tls') 
     ->setUsername('[email protected]') 
     ->setPassword('iFv0(W6Ltl=r') 
     ->setStreamOptions(array(
      'ssl' => array(
       'verify_peer' => false, 
       'verify_peer_name' => false, 
       'allow_self_signed' => true 
      ) 
     ) 
     ); 

    //Create the message 
    $message = \Swift_Message::newInstance(); 

    //Give the message a subject 
    $message->setSubject($title) 
     ->setFrom('[email protected]') 
     ->setTo($email) 
     ->setBody($title, 'text/html') 
     ->addPart($body, 'text/html'); 

    //Create the Mailer using your created Transport 
    $mailer = \Swift_Mailer::newInstance($transport); 

    //Send the message 
    $result = $mailer->send($message); 

    if ($result) { 
     echo "Email sent successfully"; 
    } 
    else 
    { 
     echo "Email failed to send"; 
    } 
相關問題