php - 在 HTML 邮件中使用 PHP 循环
问题描述
我有一个 php mail() 可以正常工作。但是,当我尝试在 HTML 内容中添加一个 for 循环来复制我需要的 n 次数据时,它只会给出一封空白邮件。
我的代码如下:
$to = $email;
$from = 'frommail@gmail.com';
$fromName = 'site name';
$subject = "subject goes here";
$htmlContent = ' <table width="640" cellspacing="0" cellpadding="0" border="0" align="left" style="vertical-align: central; background: white;">
<tbody>
<tr>
'; for($num = 1; $num <= 3; $num++) { '
<td width="30%" bgcolor="#ffffff" align="left" style="font: 13px Arial, sans-serif; text-decoration: none; vertical-align: top; padding: 6px 16px 0 6px; line-height: 18px;">
<table width="100%" cellspacing="0" cellpadding="0" border="0" bgcolor="#ffffff">
<tbody>
<tr>
<td valign="bottom" align="left" style="vertical-align: left; padding: 4px 6px 4px 6px;">
<a href="" target="_blank" data-saferedirecturl="">
<img
src="https://ci5.googleusercontent.com/proxy/zZrFmHNgHyBIZxLE-YgAoaSXo3azfzJOrKm6F_o7OrDOzp9-bCDcU6ycfbAHWRHUBWPkJDu33r2UcOn7iTZ4KiMnw2ukGVh3CMLatAX1kg-hF1hIeKt9WFSukKfN_wKZHkE=s0-d-e1-ft#https://images-eu.ssl-images-amazon.com/images/I/81PeG59W8fL._AC_SR115,115_.jpg"
width="115"
height="115"
alt="Bourge Mens Loire-99 Running Shoes"
border="0"
class="CToWUd"
/>
</a>
</td>
</tr>
<tr align="left" style="padding: 0; display: block; line-height: 16px;">
<td align="left">
<span style="font: 13px Arial, sans-serif; text-decoration: none; color: #868686;">
<a
href="https://www.amazon.in/gp/r.html?C=I16HQS27WPED&K=3UKDG356NSZ9O&M=urn:rtn:msg:202007280401268c0c8b33b2024157b4ff1cfed740p0eu&R=14QEUUMSABYUR&T=C&U=https%3A%2F%2Fwww.amazon.in%2Fgp%2Fproduct%2FB07QLYVBD5%2Fref%3Dpe_3025041_189395861_pd_te_s_mr_ti%3F_encoding%3DUTF8%26pd_rd_i%3DB07QLYVBD5%26pd_rd_r%3DKCVKENDXQHECNTMTBWK7%26pd_rd_w%3DjGq5W%26pd_rd_wg%3DlS1ag&H=1KJFSIL6U5VSBGX1K9KQZ3Z8KKEA&ref_=pe_3025041_189395861_pd_te_s_mr_ti"
style="font: 13px Arial, sans-serif; text-decoration: none; color: #868686;"
target="_blank"
data-saferedirecturl="https://www.google.com/url?q=https://www.amazon.in/gp/r.html?C%3DI16HQS27WPED%26K%3D3UKDG356NSZ9O%26M%3Durn:rtn:msg:202007280401268c0c8b33b2024157b4ff1cfed740p0eu%26R%3D14QEUUMSABYUR%26T%3DC%26U%3Dhttps%253A%252F%252Fwww.amazon.in%252Fgp%252Fproduct%252FB07QLYVBD5%252Fref%253Dpe_3025041_189395861_pd_te_s_mr_ti%253F_encoding%253DUTF8%2526pd_rd_i%253DB07QLYVBD5%2526pd_rd_r%253DKCVKENDXQHECNTMTBWK7%2526pd_rd_w%253DjGq5W%2526pd_rd_wg%253DlS1ag%26H%3D1KJFSIL6U5VSBGX1K9KQZ3Z8KKEA%26ref_%3Dpe_3025041_189395861_pd_te_s_mr_ti&source=gmail&ust=1596092101507000&usg=AFQjCNHNa6zlhX3HN9z7bHYgdFFUaOEZkQ"
>
Bourge Mens Loire-99 Running Shoes
</a>
</span>
</td>
</tr>
<tr align="left" style="padding: 0; display: block; line-height: 16px;">
<td valign="middle" align="left">
<span style="font: 13px Arial, sans-serif; text-decoration: none; color: #868686; vertical-align: 3px;"> Rs. 629.00 </span>
<span style="vertical-align: 0px;"></span>
</td>
</tr>
</tbody>
</table>
</td>
'; } '
</tr>
</tbody>
</table>
';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$fromName.'<'.$from.'>' . "\r\n";
if(mail($to, $subject, $htmlContent, $headers)){
header("location:index?alert=mailsent");
}else{
header("location:index");
}
php mail() 工作得很好。但是,当我尝试在 HTML 内容中添加一个 for 循环来复制我需要的 n 次数据时,它只会给出一封空白邮件。任何帮助是极大的赞赏。
解决方案
您需要将 for 循环内的字符串连接到$htmlContent!
...
$htmlContent = '<table>...<tr>;
for($num = 1; $num <= 3; $num++) {
$htmlContent .= '<td>...</td>';
}
$htmlContent .= '</tr></tbody></table>';
...
推荐阅读
- java - 如何在java中返回具有特定值的json对象?
- java - 在这种情况下可以使用序列生成器吗?
- drools - 为什么drools 7.51.0.Final的实现函数报错?
- java - 您认为 Vert.x 中的垂直设计指南是什么?
- maven - Avro 模式:如何创建 Maven 依赖项
- pine-script - 如何在特定时间戳添加注释(箭头)?
- coordinate-transformation - Azure Kinect 将 mm 转换为像素深度图像
- arduino - 从 AT 命令中获取值
- django - 是否可以创建一个特定网站只能访问而其他网站无法访问的api?
- flutter - 如何在颤动中增加 carousel_slider 的高度