Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mprythero's avatar

Problem with Multiple Mailings of Mailable

I have, what appears to me, to be the oddest error and I am hoping someone here can sniff out where my faux pas in my code is.

I have a table of records on a blade, where each record is a row with a checkbox (the name being send[]).

This is not where I am having my issue, but to describe it, when I click a button, it sends off the values of the checkboxes to my controller function, these values are all records values from the default ids in my MySQL table.

NOW, my actual problem lies somewhere in the controller or somewhere I cannot see, but there is an issue that shouldn't be happening no matter what.

This is the function I am referring to:

public function sendNow(Request $request)
    {
        $now = Carbon::now();
        $sendNowShipment = array();
        $method = array();
        $sendNowShipment = request('send');
            foreach($sendNowShipment as $SNS){
                $shipment = Shipment::findOrFail($SNS);
                $shipment->billedAt = Carbon::now();
                $shipment->save();
                if($shipment->billtoAccount->billingMethods->label == "Mail"){
                $timestamp = Carbon::now()->timestamp;
                $shipment_details = $shipment->shipment_details;
                    $path = 'temp/freightbill_'.$shipment->pro_number.'-'.$timestamp.'.pdf';
                    $sessionPath[] = $path;
                    $pdf = PDF::loadView('shipments.pdf', compact('shipment', 'shipment_details'))
                        ->save($path);
                }elseif($shipment->billtoAccount->billingMethods->label == "Email"){
                    $billToAccount =   $shipment->billtoAccount;
                    $billToAccountUsers = $billToAccount->users;
                        if ($billToAccount->car_billingEmail){
                            $billToEmail[] = $billToAccount->car_billingEmail;
                        }
                        if ($billToAccountUsers->count() > 0){
                            foreach ($billToAccountUsers as $billToAccountUser){
                                $billToEmail[] = $billToAccountUser->email;
                            }
                        }
                        foreach ($billToEmail as $bte){
                            Mail::to($bte)->send(new newBillToShipment($shipment));
                        }
                }
            }

////////Everything below doesn't appear to affect the issue//////
        $sendShipment = array();
        $method = array();
        $sendShipment = request('send');
            foreach($sendShipment as $ss){
                $shipmentSend = Shipment::findOrFail($ss);
                $method[] = $shipmentSend->billtoAccount->billingMethod;
            }
        
        $counts = array_count_values($method);
        if (array_key_exists(1, $counts)) {
            $countsPrint = $counts[1];
        
        
        if($countsPrint > 0){
            $pdf = new PDFMerger();

            // Add Generated PDFs to the final PDF
            foreach($sessionPath as $sp){
            $pdf->addPDF($sp, 'all');
            }

            // Merge the files and retrieve its PDF binary content
            $timestamp = Carbon::now()->timestamp;
            $binaryContent = $pdf->merge('download', $timestamp."-printBatch.pdf");

            // Return binary content as response
            }
        }
        return back();
    }

To be clear, if the problem is in the function, then it must be between the top of the code and about the middle where I place the note.

Now the problem pops up around this line $shipment->billtoAccount->billingMethods->label == "Email", as it's all focused on the email aspect of this function. If you look through the code, you'll see something about if the billtoAccount has users to pull their emails in as well.

However, this is not the problem. For whatever reason, and I can share screenshots if necessary, I will go through the timeline of events.

I've known about this issue for a while but was only made aware of it after the most recent update I did of Laravel and wonder if it's something there.

I go to click the send now of four (4) records that are email only. Their respective numbers are 9433, 9437, 9438 and 9439.

The button only sends out the one request along with the array of four record ids.

Each one of these belongs to the same account, which has no users included in it, so that portion about the users shouldn't count here and they do have only one billing email (the car_billingEmail field).

After a few seconds, the screen returns back to the original screen.

However, while 8 emails should have been sent out (4 to the customer, one for each invoice, and 4 to me for archival purposes), if I go to Sparkpost (where I handle the emails through), it reports that it sent out about 12 emails.

If I go to my inbox, I see that for two invoices I received one email each, one invoice received two and one received three emails.

Now, given that these are all the same customer, I would've understood receiving the same amount for each invoice (even if it was more than one), but given that the numbers are scattered, I am extremely confused.

What's more is that I know this is not Sparkpost's doing, as I send out other emails through them for other purposes on the site and I don't have these issues.

If you need anymore information or code or whatever, feel free to ask, I'd just really like to fix this issue before I am driven to insanity trying to make sense of it.

Thanks!!!

Matt

0 likes
2 replies
Snapey's avatar

two lines like this

$billToEmail[] = $billToAccountUser->email;

add to the $billToEmail array but you never reset it, so it accumulates more emails as you go around the loop

By the way, you must realise that lack of answers here is because of the state of the code. A lot will take one look and think you have bigger issues to worry about.

You have way too much going on in the controller, and its really hard for anyone to see what is happening with so many similarly named variables.

Think about how you can break this up into functions and use OOP to structure the code better

1 like
mprythero's avatar

@Snapey - I kind of figured, at the moment I'm trying to clean up the code but have been a bit overworked on fixing the state of the project all around the place.

I've never broken controller code up before though, this is the only case that I have seen in this project where the controller function stretches to perform more than one task. Do you have any suggestions of where to look for examples for breaking it up properly?

Thank you for your suggestions and comments though, I appreciate them.

Please or to participate in this conversation.