0

I have a php mail() that works just fine. 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.

My code goes as :

$to = $email;
$from = '[email protected]'; 
$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. 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. Any help is greatly appreciated.

2
  • 3
    for($num = 1; $num <= 3; $num++) { $htmlContent .= ' <td ... Commented Jul 29, 2020 at 8:31
  • You are just creating output there in your loop, that will be added straight to the output buffer, resp. get send to the client. You need to keep concatenating this to your variable $htmlContent. Commented Jul 29, 2020 at 8:32

1 Answer 1

1

You need to concat the string inside the for-loop to $htmlContent!

...
$htmlContent = '<table>...<tr>; 
for($num = 1; $num <= 3; $num++) { 
    $htmlContent .= '<td>...</td>';
}
$htmlContent .= '</tr></tbody></table>';
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.