0

TLDR:
When large amounts of files (array values) are in the array, the loop will iterate through all the values but only execute part of the foreach statement for some of them. When that happens, it will always affect the same files (in file order, not file name).

Detailed version:
Recently, I have written a script which scans webserver folders for new files (invoices) by name. Through using arrays the script separates the new files from the old files and inserts them as links into the body of an email to be sent out to a client at the end of the day. Each client receives one email containing all of their invoices every day. Although the loop iterates through all the values in the array without a problem, it does not execute the inside of the foreach construct correctly for each value, although it always executes it to some degree. There is no error in the php log and the server runs on an 8-core Xeon CPU and 12 GB or RAM.

Thinking that there might be too much going on at the same time, I’ve tried adding two 2-second timeouts before and after the statement and separating the long variable into separate ones with no luck. The results were still the same. During troubleshooting using different files, different methods of launching the script and running it for only one client at a time I discovered that the links which display incorrectly will always come in the same order i.e. from top down, it will be the 44th, 73rd and 102nd file every time. Really weird.

The foreach statement is simply adding a line to the email per each new invoice. The way that it doesn’t always work is that some lines (links) appear in html without the closing </a> tag or appear as a link with the right file name but without a href. Below I’ll include the php code, contents of the email and source from firefox.

Code:

if (!empty($newfiles)) {

    $subject = 'New invoice from company';

    $message = "
    <html>
    <body>
    <p>Hello ".$dirname.",</p>
    <p>Please select a number below to access the relevant invoice:</p>
    <p>
    ";

    foreach ($newfiles as $emailfile){
        $invoice = pathinfo($emailfile);
        $message .= '<a href="'.$link.'/'.$emailfile.'">'.$invoice['filename'].'</a><br/>';
    }

    $message .= "
    </p>
    <p>Thank you for your business.</p>
    <p>Kind regards,<br/>Company</p>
    </body>
    </html>";

    //Setting content-type
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'Bcc: tester@itcompany.com.au' . "\r\n";

    // From address
    $headers .= 'From: <client@client.com.au>' . "\r\n";

    mail($emailaddress, $subject, $message, $headers);

    }

Email output:

New invoice from company
noreply@company.com.au
Thu 3/12/2015 9:06 PM
To:
client@client.com.au;
Hello CLIENT,
Please select a number below to access the relevant invoice:
167205
167206
167209
167210
167213
167216
167226
167227
167232
167252
167266
167273
167277
167283
167285
167308
167321
167328
167333
167366
167368
167352
167356
167365
167370
167371
167373
167385
167388
167397
167600
167613
167615
167617
167619
167620
167627
167630
167633
167639
167660
167661
167662
167663
167665
167666
< a href="http://company.com.au/CLIENT/167667.pdf">167667
167651
167653
167656
167657
167658
167659
167667
167669
167673
167679
167682
167686
167687
167692
167695
167696
167503
167511
167526
167526
167527
167536
167560
167562
167553
167566
167566
167573
167576
167576
167577
167579
167589
167590
167596
167600
167601
167623
167626
167626
167633
167636
167635
167661
167662
167658
167661
167687
167760
167783
167793
167801
167810
167836
167862
167866
167956
167958
168013
168101
168103
168106
168118
168125
168133
168139
168160
168803
168808
168809
168810
168811
168815
168816
168817
168818
168820
168821
168826
168826
168827
168828
168829
168836
168838
168861
157567
Thank you for your business.
Kind regards,
Company

Email in html through firefox:

<div style="margin-top:16pt;margin-bottom:16pt;">Hello CLIENT,</div>

<div style="margin-top:16pt;margin-bottom:16pt;">Please select a number below to access the relevant invoice:</div>

<div style="margin-top:16pt;margin-bottom:16pt;"><a href="http://company.com.au/CLIENT/167205.pdf" target="_blank">167205</a><br>

<a href="http://company.com.au/CLIENT/167206.pdf" target="_blank">167206</a><br>

<a href="http://company.com.au/CLIENT/167209.pdf" target="_blank">167209</a><br>

<a href="http://company.com.au/CLIENT/167210.pdf" target="_blank">167210</a><br>

<a href="http://company.com.au/CLIENT/167213.pdf" target="_blank">167213</a><br>

<a href="http://company.com.au/CLIENT/167216.pdf" target="_blank">167216</a><br>

<a href="http://company.com.au/CLIENT/167226.pdf" target="_blank">167226</a><br>

<a href="http://company.com.au/CLIENT/167227.pdf" target="_blank">167227</a><br>

<a href="http://company.com.au/CLIENT/167232.pdf" target="_blank">167232</a><br>

<a href="http://company.com.au/CLIENT/167252.pdf" target="_blank">167252</a><br>

<a href="http://company.com.au/CLIENT/167266.pdf" target="_blank">167266</a><br>

<a href="http://company.com.au/CLIENT/167273.pdf" target="_blank">167273</a><br>

<a href="http://company.com.au/CLIENT/167277.pdf" target="_blank">167277</a><br>

<a href="http://company.com.au/CLIENT/167283.pdf" target="_blank">167283</a><br>

<a href="http://company.com.au/CLIENT/167285.pdf" target="_blank">167285</a><br>

<a href="" target="_blank">167308</a><br>

<a href="http://company.com.au/CLIENT/167321.pdf" target="_blank">167321</a><br>

<a href="http://company.com.au/CLIENT/167328.pdf" target="_blank">167328</a><br>

<a href="http://company.com.au/CLIENT/167333.pdf" target="_blank">167333</a><br>

<a href="http://company.com.au/CLIENT/167366.pdf" target="_blank">167366</a><br>

<a href="http://company.com.au/CLIENT/167368.pdf" target="_blank">167368</a><br>

<a href="http://company.com.au/CLIENT/167352.pdf" target="_blank">167352</a><br>

<a href="http://company.com.au/CLIENT/167356.pdf" target="_blank">167356</a><br>

<a href="http://company.com.au/CLIENT/167365.pdf" target="_blank">167365</a><br>

<a href="http://company.com.au/CLIENT/167370.pdf" target="_blank">167370</a><br>

<a href="http://company.com.au/CLIENT/167371.pdf" target="_blank">167371</a><br>

<a href="http://company.com.au/CLIENT/167373.pdf" target="_blank">167373</a><br>

<a href="http://company.com.au/CLIENT/167385.pdf" target="_blank">167385</a><br>

<a href="http://company.com.au/CLIENT/167388.pdf" target="_blank">167388</a><br>

<a href="http://company.com.au/CLIENT/167397.pdf" target="_blank">167397</a><br>

<a href="http://company.com.au/CLIENT/167600
 .pdf" target="_blank">167600</a><br>

<a href="http://company.com.au/CLIENT/167613.pdf" target="_blank">167613</a><br>

<a href="http://company.com.au/CLIENT/167615.pdf" target="_blank">167615</a><br>

<a href="http://company.com.au/CLIENT/167617.pdf" target="_blank">167617</a><br>

<a href="http://company.com.au/CLIENT/167619.pdf" target="_blank">167619</a><br>

<a href="http://company.com.au/CLIENT/167620.pdf" target="_blank">167620</a><br>

<a href="http://company.com.au/CLIENT/167627.pdf" target="_blank">167627</a><br>

<a href="http://company.com.au/CLIENT/167630.pdf" target="_blank">167630</a><br>

<a href="http://company.com.au/CLIENT/167633.pdf" target="_blank">167633</a><br>

<a href="http://company.com.au/CLIENT/167639.pdf" target="_blank">167639</a><br>

<a href="http://company.com.au/CLIENT/167660.pdf" target="_blank">167660</a><br>

<a href="http://company.com.au/CLIENT/167661.pdf" target="_blank">167661</a><br>

<a href="http://company.com.au/CLIENT/167662.pdf" target="_blank">167662</a><br>

<a href="http://company.com.au/CLIENT/167663.pdf" target="_blank">167663</a><br>

<a href="http://company.com.au/CLIENT/167665.pdf" target="_blank">167665</a><br>

<a href="http://company.com.au/CLIENT/167666.pdf" target="_blank">167666</a><br>

&lt; a href="http://company.com.au/CLIENT/167667.pdf"&gt;167667<br>

<a href="http://company.com.au/CLIENT/167651.pdf" target="_blank">167651</a><br>

<a href="http://company.com.au/CLIENT/167653.pdf" target="_blank">167653</a><br>

<a href="http://company.com.au/CLIENT/167656.pdf" target="_blank">167656</a><br>

<a href="http://company.com.au/CLIENT/167657.pdf" target="_blank">167657</a><br>

<a href="http://company.com.au/CLIENT/167658.pdf" target="_blank">167658</a><br>

<a href="http://company.com.au/CLIENT/167659.pdf" target="_blank">167659</a><br>

<a href="http://company.com.au/CLIENT/167667.pdf" target="_blank">167667</a><br>

<a href="http://company.com.au/CLIENT/167669.pdf" target="_blank">167669</a><br>

<a href="http://company.com.au/CLIENT/167673.pdf" target="_blank">167673</a><br>

<a href="http://company.com.au/CLIENT/167679.pdf" target="_blank">167679</a><br>

<a href="http://company.com.au/CLIENT/167682.pdf" target="_blank">167682</a><br>

<a href="http://company.com.au/CLIENT/167686.pdf" target="_blank">167686</a><br>

<a href="http://company.com.au/CLIENT/167687.pdf" target="_blank">167687</a><br>

<a href="http://company.com.au/CLIENT/167692.pdf" target="_blank">167692</a><br>

<a href="" target="_blank">167695</a><br>

<a href="http://company.com.au/CLIENT/167696.pdf" target="_blank">167696</a><br>

<a href="http://company.com.au/CLIENT/167503.pdf" target="_blank">167503</a><br>

<a href="http://company.com.au/CLIENT/167511.pdf" target="_blank">167511</a><br>

<a href="http://company.com.au/CLIENT/167526.pdf" target="_blank">167526</a><br>

<a href="http://company.com.au/CLIENT/167526.pdf" target="_blank">167526</a><br>

<a href="http://company.com.au/CLIENT/167527.pdf" target="_blank">167527</a><br>

<a href="http://company.com.au/CLIENT/167536.pdf" target="_blank">167536</a><br>

<a href="http://company.com.au/CLIENT/167560.pdf" target="_blank">167560</a><br>

<a href="http://company.com.au/CLIENT/167562.pdf" target="_blank">167562</a><br>

<a href="http://company.com.au/CLIENT/167553.pdf" target="_blank">167553</a><br>

<a href="http://company.com.au/CLIENT/167566.pdf" target="_blank">167566</a><br>

<a href="http://company.com.au/CLIENT/167566.pdf" target="_blank">167566</a><br>

<a href="http://company.com.au/CLIENT/167573.pdf" target="_blank">167573</a><br>

<a href="http://company.com.au/CLIENT/167576.pdf" target="_blank">167576</a><br>

<a href="http://company.com.au/CLIENT/167576.
 pdf" target="_blank">167576</a><br>

<a href="http://company.com.au/CLIENT/167577.pdf" target="_blank">167577</a><br>

<a href="http://company.com.au/CLIENT/167579.pdf" target="_blank">167579</a><br>

<a href="http://company.com.au/CLIENT/167589.pdf" target="_blank">167589</a><br>

<a href="http://company.com.au/CLIENT/167590.pdf" target="_blank">167590</a><br>

<a href="http://company.com.au/CLIENT/167596.pdf" target="_blank">167596</a><br>

<a href="http://company.com.au/CLIENT/167600.pdf" target="_blank">167600</a><br>

<a href="http://company.com.au/CLIENT/167601.pdf" target="_blank">167601</a><br>

<a href="http://company.com.au/CLIENT/167623.pdf" target="_blank">167623</a><br>

<a href="http://company.com.au/CLIENT/167626.pdf" target="_blank">167626</a><br>

<a href="http://company.com.au/CLIENT/167626.pdf" target="_blank">167626</a><br>

<a href="http://company.com.au/CLIENT/167633.pdf" target="_blank">167633</a><br>

<a href="http://company.com.au/CLIENT/167636.pdf" target="_blank">167636</a><br>

<a href="http://company.com.au/CLIENT/167635.pdf" target="_blank">167635</a><br>

<a href="http://company.com.au/CLIENT/167661.pdf" target="_blank">167661</a><br>

<a href="http://company.com.au/CLIENT/167662.pdf" target="_blank">167662</a><br>

<a href="http://company.com.au/CLIENT/167658.pdf" target="_blank">167658</a><br>

<a href="http://company.com.au/CLIENT/167661.pdf" target="_blank">167661</a><br>

<a href="http://company.com.au/CLIENT/167687.pdf" target="_blank">167687</a><br>

<a href="http://company.com.au/CLIENT/167760.pdf" target="_blank">167760</a><br>

<a href="http://company.com.au/CLIENT/167783.pdf" target="_blank">167783</a><br>

<a href="http://company.com.au/CLIENT/167793.pdf" target="_blank">167793</a><br>

<a href="http://company.com.au/CLIENT/167801.pdf" target="_blank">167801</a><br>

<a href="http://company.com.au/CLIENT/167810.pdf" target="_blank">167810</a><br>

<a href="http://company.com.au/CLIENT/167836.pdf" target="_blank">167836</a><br>

<a href="http://company.com.au/CLIENT/167862.pdf" target="_blank">167862</a><br>

<a href="http://company.com.au/CLIENT/167866.pdf" target="_blank">167866</a><br>

<a href="http://company.com.au/CLIENT/167956.pdf" target="_blank">167956</a><br>

<a href="http://company.com.au/CLIENT/167958.pdf" target="_blank">167958</a><br>

<a href="http://company.com.au/CLIENT/168013.pdf" target="_blank">168013</a><br>

<a href="http://company.com.au/CLIENT/168101.pdf" target="_blank">168101</a><br>

<a href="" target="_blank">168103</a><br>

<a href="http://company.com.au/CLIENT/168106.pdf" target="_blank">168106</a><br>

<a href="http://company.com.au/CLIENT/168118.pdf" target="_blank">168118</a><br>

<a href="http://company.com.au/CLIENT/168125.pdf" target="_blank">168125</a><br>

<a href="http://company.com.au/CLIENT/168133.pdf" target="_blank">168133</a><br>

<a href="http://company.com.au/CLIENT/168139.pdf" target="_blank">168139</a><br>

<a href="http://company.com.au/CLIENT/168160.pdf" target="_blank">168160</a><br>

<a href="http://company.com.au/CLIENT/168803.pdf" target="_blank">168803</a><br>

<a href="http://company.com.au/CLIENT/168808.pdf" target="_blank">168808</a><br>

<a href="http://company.com.au/CLIENT/168809.pdf" target="_blank">168809</a><br>

<a href="http://company.com.au/CLIENT/168810.pdf" target="_blank">168810</a><br>

<a href="http://company.com.au/CLIENT/168811.pdf" target="_blank">168811</a><br>

<a href="http://company.com.au/CLIENT/168815.pdf" target="_blank">168815</a><br>

<a href="http://company.com.au/CLIENT/168816.pdf" target="_blank">168816</a><br>

<a href="http://company.com.au/CLIENT/168817.pdf" target="_blank">168817</a><br>

<a href="http://company.com.au/CLIENT/168818.p
 df" target="_blank">168818</a><br>

<a href="http://company.com.au/CLIENT/168820.pdf" target="_blank">168820</a><br>

<a href="http://company.com.au/CLIENT/168821.pdf" target="_blank">168821</a><br>

<a href="http://company.com.au/CLIENT/168826.pdf" target="_blank">168826</a><br>

<a href="http://company.com.au/CLIENT/168826.pdf" target="_blank">168826</a><br>

<a href="http://company.com.au/CLIENT/168827.pdf" target="_blank">168827</a><br>

<a href="http://company.com.au/CLIENT/168828.pdf" target="_blank">168828</a><br>

<a href="http://company.com.au/CLIENT/168829.pdf" target="_blank">168829</a><br>

<a href="http://company.com.au/CLIENT/168836.pdf" target="_blank">168836</a><br>

<a href="http://company.com.au/CLIENT/168838.pdf" target="_blank">168838</a><br>

<a href="http://company.com.au/CLIENT/168861.pdf" target="_blank">168861</a><br>

<a href="http://company.com.au/CLIENT/157567.pdf" target="_blank">157567</a><br>

</div>

Also on StackOverflow

Edited by Jaklins: added link

3
Contributors
2
Replies
16
Views
2 Years
Discussion Span
Last Post by diafol
0

$message .= '<a href="'.$link.'/'.$emailfile.'">'.$invoice['filename'].'</a><br/>';

Are all your variables html/url encoded? If they are not, some may break your html.

0

Posting the same problem on multiple forums, while good for you, is not so great for everybody else. You'll find that you may get many similar responses and contributors will be wasting their time trying to help you as your solution may come from a different forum, whithout them being aware of it. Some friendly advice - pick a forum, stick with it for a short while and if the solution doesn't present itself, pick another. In addition, posting duplicate content on many sites will make them look as though they're scraping content from elsewhere and impact badly on them with regard to search engine rankings.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.