2014-12-02 130 views
0

我目前使用下面的代碼從php中的mysql表郵寄數據,它使用下面的代碼,它完美的工作,但問題是它發送每行數據作爲一個單一的郵件,我想發送一個郵件中的所有數據行,如何合併所有數據行並將其作爲單個郵件發送?請幫幫我!!如何將所有數據通過電子郵件發送到一個郵箱?

<?php 
include('header.php'); 
session_start(); 
?> 
<html> 
<head> 
<title>Sending email using PHP</title> 
</head> 
<body> 
<?php 
    $sql="SELECT * FROM products"; 
    $result = mysql_query($sql); 
while($row = mysql_fetch_array($result)){  
    $to = "[email protected]"; 
    $subject = "d2dn-viewcart"; 
    $id_s = $row["id"]; 
    $name_s = $row["description"]; 
    $price_s = $row["price"] ; 
    $message = $id_s . " " . $name_s . " " . $price_s." "; 
    $header = "From:d2dn"; 
    $retval = mail ($to,$subject,$message,$header); 
} 
    if($retval == true) 
    { 
     echo "Message sent successfully..."; 
    } 
    else 
    { 
     echo "Message could not be sent..."; 
    } 
?> 
</body> 
</html> 
<?php 
include('footer.php'); ?> 
+0

你會發現,如果'($重估)'是** **完全相同一樣'如果($ RETVAL ==真)'和更快的輸入。 – zanderwar 2014-12-02 07:31:42

回答

1

您只需拼接的內容和發送一次電子郵件:

<?php 
include('header.php'); 
session_start(); 
?> 
<html> 
<head> 
<title>Sending email using PHP</title> 
</head> 
<body> 
<?php 
    $sql="SELECT * FROM products"; 
    $result = mysql_query($sql); 
    $to = "[email protected]"; 
    $subject = "d2dn-viewcart"; 
    $header = "From:d2dn"; 
    $message = ''; 
    while($row = mysql_fetch_array($result)){  

     $id_s = $row["id"]; 
     $name_s = $row["description"]; 
     $price_s = $row["price"] ; 
     $message .= $id_s . " " . $name_s . " " . $price_s." "; 

    } 
    $retval = mail ($to,$subject,$message,$header); 
    if($retval == true) 
    { 
    echo "Message sent successfully..."; 
    } 
    else 
    { 
    echo "Message could not be sent..."; 
    } 
?> 
</body> 
</html> 
<?php 
include('footer.php'); ?> 
+0

它完美的工作,現在我明白了邏輯謝謝你 – vivek 2014-12-02 07:42:15

0

定義第一外循環$message = '';

那麼你就需要追加字符串如

$message .= $id_s . " " . $name_s . " " . $price_s." "; 

,然後保持你的mail()外環

$retval = mail ($to,$subject,$message,$header); 
+1

請檢查更新 – 2014-12-02 07:29:24

+0

謝謝我會檢查這個解決方案 – vivek 2014-12-02 07:33:00

+0

這個邏輯的工作非常感謝你 – vivek 2014-12-02 07:42:45

0
<?php 
include('header.php'); 
session_start(); 
?> 
    <html> 
    <head> 
     <title>Sending email using PHP</title> 
    </head> 
    <body> 
    <?php 
    $sql="SELECT * FROM products"; 
    $result = mysql_query($sql); 
    $to = "[email protected]"; 
    $subject = "d2dn-viewcart"; 
    while($row = mysql_fetch_array($result)){ 
     $id_s = $row["id"]; 
     $name_s = $row["description"]; 
     $price_s = $row["price"] ; 
     $message .= $id_s . " " . $name_s . " " . $price_s." \r\n"; 
    } 
    $header = "From:d2dn"; 
    $retval = mail ($to,$subject,$message,$header); 

    if($retval == true) 
    { 
     echo "Message sent successfully..."; 
    } 
    else 
    { 
     echo "Message could not be sent..."; 
    } 
    ?> 
    </body> 
    </html> 
<?php 
include('footer.php'); ?> 

這應該工作

+0

考慮提供解​​釋給你的代碼 – arghtype 2014-12-02 07:39:49