简体   繁体   中英

PHP for loop not iterating fully inside HTML email

I have a PHP script through which I send HTML emails. I'm using a 'for' loop to collect data and storing it in a variable. The loop is instructed to run 25 times. The problem is that it is looping only 19 times. I checked for any unlcosed tags or typos in syntax but didn't find any. I'm posting the for loop section in case any of you can spot what I couldn't. It is really frustrating as I think the solution is very simple and yet I'm unable to spot the problem.

My headers for the 'mail()' function are fine. Here they are just in case

$headers = "From: $from \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

And here is my for loop:

$abc ="<table width='600'>";
        $abc .= "<tr>
                        <td bgcolor='#d6bf86'><span style='color:#9c2a00'>Number of Products</span></td>
                        <td align='center' bgcolor='#fde4d0'>$pagess</td>
                        <td align='center' bgcolor='#c3deb9'>$pagese</td>
                        <td align='center' bgcolor='#bee7f8'>$pagesu</td>
                    </tr>";


        for($i=1; $i<=25; $i++)
        {
            $abc .="<tr>";
            if($i % 2 == 0)     //  EVEN ROW
            {       
                    $abc .= "<td bgcolor='#d6bf86' width='260'><span style='color:#9c2a00'>".${f.$i}."</span></td>";
            }
            else                //  ODD ROW
            {
                    $abc .= "<td bgcolor='#fffbd0' width='260'><span style='color:#9c2a00'>".${f.$i}."</span></td>";
            }

                    if(isset(${s.$i}))      
                    {
                        $abc .= "<td bgcolor='#fde4d0' align='center'>Yes</td>";
                        ${s.$i} = "Yes";
                    }
                    else
                    {
                        $abc .= "<td bgcolor='#fde4d0' align='center'>No</td>";
                        ${s.$i} = "No";
                    }
                    if(isset(${e.$i}))
                    {
                        $abc .= "<td bgcolor='#c3deb9' align='center'>No</td>";
                        ${e.$i} = "Yes";
                    }
                    else
                    {
                        $abc .= "<td bgcolor='#c3deb9' align='center'>Yes</td>";
                        ${e.$i} = "No";
                    }
                    if(isset(${u.$i}))
                    {
                        $abc .= "<td bgcolor='#bee7f8' align='center'>No</td>";
                        ${u.$i} = "Yes";
                    }
                    else
                    {
                        $abc .= "<td bgcolor='#bee7f8' align='center'>Yes</td>";
                        ${u.$i} = "No";
                    }
            $abc .="</tr>";
        }


        if(isset($_POST['dscs']))   //  DISCOUNT HAS BEEN APPLIED
        {

            $abc .= "<tr>
                            <td>Base Price</td>
                            <td align='center'>$sums</td>
                            <td align='center'>$sume</td>
                            <td align='center'>$sumu</td>
                        </tr>";
            $abc .= "<tr>
                            <td>Discount Offered</td>
                            <td align='center'>$discount% </td>
                            <td align='center'>$discount% </td>
                            <td align='center'>$discount% </td>
                        </tr>";
            $abc .= "<tr>
                            <td>Effective Price</td>
                            <td align='center'>$dscs</td>
                            <td align='center'>$dsce</td>
                            <td align='center'>$dscu</td>
                      </tr>";
        }
        else
        {
            $dscs = $sums;
            $dsce = $sume;
            $dscu = $sumu;
            $abc .= "<tr>
                            <td>Total Price</td>
                            <td align='center'>$sums</td>
                            <td align='center'>$sume</td>
                            <td align='center'>$sumu</td>
                        </tr>";
        }

        $abc .="</table>";

I can attach the screenshot of the email that is being sent to give an idea on how the code is breaking. Let me know if you want the screenshot too.

PS : I copy-pasted this code in a separate file and ran it and it was working fine. The loop iterated 25 times. This makes me believe that there is a problem putting it inside HTML email.

Also adding in the email script if that helps:

$to = $clem;
$subject = "Something goes here";
$message = "<html>
<head></head>
<body style='background-color:#ffffff; font-family:Lucida Sans Unicode, Lucida Grande, sans-serif;'>
    <table width='600'>
        <tr>
            <td>$abc</td>
        </tr>
    </table>
</body>
</html>";

$from = "$logged_user";
$headers = "From: $from \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$res=mail($to,$subject,$message,$headers);

Thanks in advance, Nisar

Nevermind, I found what was wrong. Here it is for people with similar problems.

I learnt that html emails have a 990 character limit per line. That was the reason why my code was breaking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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