2015-09-29 42 views
0

因此,我想通過電子郵件將消息發送給使用phpmailer的用戶。當這是電子郵件時,它需要具有可變數量的行。我在這裏發現了一些代碼,這些代碼指向了正確的方向,但是當我將這些代碼添加到消息中時,會發生一些事情。在phpmailer中使用php創建帶有變量行的html表格

<thead> 
    <tr> 
     <th>Item Name</th> 
     <th>SKU Number</th> 
     <th>Price</th> 
    </tr> 
</thead> 
<tbody> 
    <?php foreach($stuff as $trow) : ?> 
    <tr> 
    <td><?php echo $trow->$name; ?></td> 
    <td><?php echo $trow->$sku; ?></td> 
    <td><?php echo $trow->$price; ?></td> 
    </tr> 
    <?php endforeach; ?> 
</tbody> 

首先foreach循環正在關閉在第一行的末尾,至少我是這樣認爲,因爲我的第一個後,沒有其他行。其次,當發送電子郵件時,我收到電子郵件,但分號,問號和右括號與每個單元格中的數據都一樣。我得到的第一行數據是準確的。只是無所事事而已。我是否錯過了這段代碼?我應該發佈我的所有代碼以幫助縮小範圍嗎?我發佈了我的phpmailer代碼。這可能是我試圖調用兩次php?儘管我會認爲這不重要,因爲$消息內容是發送到電子郵件地址的html電子郵件。

to_name="$firstname $lastname"; 
$to="[email protected]"; 
$subject="Your store order for: $date"; 
$headers="MIME-VERSION1.0\r\n"; 
$headers .="Content-Type: text/html; charset=ISO-8859-1\r\n"; 

$message="Dear $custfirstname $custlastname, Thank you for choosing this store for your purchase. You purchsed the following items on $date at store $store.<br>"; 

$message .="<table border=\"1\"> 
<thead> 
    <tr> 
     <th>Item Name</th> 
     <th>SKU Number</th> 
     <th>Price</th> 
    </tr> 
</thead> 
<tbody> 
    <?php foreach($stuff as $trow) : ?> 
    <tr> 
    <td><?php echo $trow->name; ?></td> 
    <td><?php echo $trow->sku; ?></td> 
    <td><?php echo $trow->price; ?></td> 
    </tr> 
    <?php endforeach; ?> 
</tbody> 
</table>"; 


$message .="Please fill out these surveys below to tell us how your   experience was with our company."; 

$from_name="from name"; 
$from = "[email protected]"; 

$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->Host ="mailhost"; 
$mail->Port =25; 
$mail->SMTPAuth = false; 
$mail->Username="mailboxsendingmessage"; 
$mail->Password="passwordforsendingmailbox"; 
$mail->FromName=$from_name; 
$mail->From=$from; 
$mail->AddAddress($to, $to_name); 
$mail->Subject = $subject; 
$mail->isHTML(true); 
$mail->Body = $message; 

$result = $mail->Send(); 
echo $result ? 'Sent' : 'Error'; 
$mailsend = NULL; 
$mailsend = $result; 

echo "$mailsend"; 

編輯:添加我的PHP郵件程序代碼。

+0

「<?php var_dump($ stuff)」的輸出是什麼? ?>'?這會告訴你$ stuff變量的結構是否正確,並且有你想要打印的所有行。除此之外,語法對我來說看起來是正確的。 – amklose

+0

我懷疑你在'echo'語句裏面有這樣的代碼,而不是在一段HTML代碼中。 – Barmar

+0

請參閱http://stackoverflow.com/questions/32831134/php-mysql-php-echo-r​​owvariable/32831570#32831570 – Barmar

回答

0

您可以使用HTML這樣

$message .='<table border="1"> 
<thead> 
    <tr> 
     <th>Item Name</th> 
     <th>SKU Number</th> 
     <th>Price</th> 
    </tr> 
</thead> 
<tbody>'; 

    <?php foreach($stuff as $trow) { ?> 

    $message .='<tr> 
    <td>'.$trow->name.'</td> 
    <td>'.$trow->sku.'</td> 
    <td>'.$trow->price.'</td> 
    </tr>'; 
    <?php } ?> 
$message .='</tbody> 
</table>'; 
+0

這工作完美。我必須進行更改,以確保我的數據顯示由於我的錯誤,但否則這是完美的。謝謝。 – DarthAbingdon

0

我發現混淆循環的方式,我總是喜歡heredoc
這就是我通常做:

<thead> 
    <tr> 
     <th>Item Name</th> 
     <th>SKU Number</th> 
     <th>Price</th> 
    </tr> 
</thead> 
<tbody> 
<?php 

foreach($stuff as $trow){ 
echo <<< LOL 
<tr> 
<td>{$trow->$name}</td> 
<td>{$trow->$sku}</td> 
<td>{$trow->$price}</td> 
</tr> 
LOL; 
} 

?> 
</tbody> 

最終你會行動將更少的代碼,因此誤差較小。

+0

這將直到phpmailer的html消息的內容中工作嗎?去嘗試一下,看看它是如何做到的。 – DarthAbingdon

+0

我以前用'PHPMailer'使用'heredoc',它也能正常工作,並且確保你設置了正確的MIME類型來處理你的電子郵件中的html。 –

+0

不幸的是,'heredoc'沒有工作。該表現在返回爲空,<<顯示價格下方,然後單元格將包含ROW;}?> – DarthAbingdon

0

隨着額外的代碼問題變得清晰。

您正在將PHP語句插入字符串中,然後通過電子郵件發送該字符串。如果您查看該電子郵件的來源,則可能會看到未執行的PHP代碼。

您不能在字符串內插入foreach循環。相反,您將需要使用循環遞增地構建您的字符串,如

$message .="<table border=\"1\"> 
<thead> 
    <tr> 
     <th>Item Name</th> 
     <th>SKU Number</th> 
     <th>Price</th> 
    </tr> 
</thead> 
<tbody>"; 

foreach($stuff as $trow) : 
    $message .= "<tr> 
    <td>".$trow->name."</td> 
    <td>".$trow->sku."</td> 
    <td>".$trow->price."</td> 
    </tr>"; 
endforeach; 
$message .= "</tbody> 
</table>";