【发布时间】:2015-09-29 15:31:07
【问题描述】:
所以我正在尝试通过电子邮件向使用 phpmailer 的人发送消息。当这是电子邮件时,它需要可以具有可变数量的行。我在这里找到了一些代码,这些代码为我指明了正确的方向,但是当我将此代码放入消息中时,发生了一些事情。
<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 两次?虽然我认为这并不重要,因为 $message 内容是发送到电子邮件地址的 html 电子邮件。
to_name="$firstname $lastname";
$to="to@email.address";
$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 = "from@email.address";
$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 邮件程序代码。
【问题讨论】:
-
<?php var_dump($stuff); ?>的输出是什么?这将告诉您 $stuff 变量的结构是否正确以及是否包含您要打印的所有行。除此之外,语法对我来说是正确的。 -
我怀疑您在
echo语句中包含此代码,而不是在 HTML 代码块中。 -
如果你硬编码
$stuff然后运行这个循环,它会打印出什么?即制作一个Minimal, Complete, and Verifiable example。我怀疑您的问题出在这个特定的代码块之外,但我们永远不会知道您是否不对其进行测试。 -
如果你将该代码块作为 HTML 传递给浏览器,你会在不同的行上得到
Item Name SKU Number Price和$name; ?> $sku; ?> $price; ?>,看起来和你得到的一样吗?你能提供这段代码生成的 HTML 的相关 sn-p 吗?