简体   繁体   English

在 HTML 邮件中使用 PHP 循环

[英]Using PHP Loop in HTML Mail

I have a php mail() that works just fine.我有一个工作正常的 php mail()。 But when I try to add a for loop in the HTML content to replicate the data n time I need, It just gives a blank mail.但是,当我尝试在 HTML 内容中添加一个 for 循环来复制我需要的数据 n 次时,它只会给出一封空白邮件。

My code goes as:我的代码如下:

$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&amp;K=3UKDG356NSZ9O&amp;M=urn:rtn:msg:202007280401268c0c8b33b2024157b4ff1cfed740p0eu&amp;R=14QEUUMSABYUR&amp;T=C&amp;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&amp;H=1KJFSIL6U5VSBGX1K9KQZ3Z8KKEA&amp;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&amp;source=gmail&amp;ust=1596092101507000&amp;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() that works just fine. php mail() 工作得很好。 But when I try to add a for loop in the HTML content to replicate the data n time I need, It just gives a blank mail.但是,当我尝试在 HTML 内容中添加一个 for 循环来复制我需要的数据 n 次时,它只会给出一封空白邮件。 Any help is greatly appreciated.任何帮助是极大的赞赏。

You need to concat the string inside the for-loop to $htmlContent !您需要将 for 循环内的字符串连接到$htmlContent

...
$htmlContent = '<table>...<tr>; 
for($num = 1; $num <= 3; $num++) { 
    $htmlContent .= '<td>...</td>';
}
$htmlContent .= '</tr></tbody></table>';
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM