Laravel is sending duplicate emails!

So… I wrote a previous post about how to prevent duplicates, but this post is about a different type of duplication problem. I encountered this issue about 18 months ago, and asked around, and few people seemed to know what I was talking about, and I don’t specifically see it mentioned in the docs. So.. here goes.

// Laravel 5.7 and above, IIRC
// haven't checked on older versions
$mailable = new MyMailableClass($data);
Mail::to("s@kimsal.com")->send($mailable);
Mail::to("j@kimsal.com")->send($mailable);

If you run the above code, the system will send an email to s@kimsal.com. The system will then send a second email with to both j@kimsal.com *and* s@kimsal.com. Why?

Behind the facade, the $mailable class has a ‘to’ list of recipients, and every call to Mail::to will *add* to the mailable’s recipient list, not clear and start over. This is certainly not intuitive, and is likely the root cause of a lot of the posts I’ve seen about “duplicate email” behavior. Certainly there can be other causes, but I’ve hit this one personally, and know it was a bugger to figure out.

The fix? I only know of two fixes.

  1. If you’re really sending the exact same thing to multiple addresses, you can send to one array of addresses. The downside is that each person will see the other recipients’ addresses. In some cases, this won’t matter. In others, it may be a big privacy issue.
  2. You can also just create a new mailable each time.

I help this helps someone

Similar Posts

  • Open Source TechFinder

    Inspired by the AUTM conference, I got inspired to look at some of the common processes techtransfer folks do. Main idea was to try to develop something relatively ‘standalone’ that might address a use case I learned about, so I decided on building a web-based open source techfinder tool to publish licensable technologies. The notion…

  • PHP Quality Tools

    Curious about checking out the quality of your PHP project, but don’t know where to start? https://github.com/jakzal/phpqa is a project providing docker images of various tools to help measure aspects of your PHP code. will run the phploc tool on your current folder But… you can alias the tool, then simply run $ phpqa <toolname>…

  • Bad I9 PDF form

    Have been needing to programmatically fill out an I9 PDF, retrieved from gov site. Should be fairly straightforward, right? Well… the field names are… a mess. Field names like topmostSubform[0].Page1[0].U\.S\._Social_Security_Number__Last_4_numbers_[0]topmostSubform[0].Page1[0].expiration_date__if_applicable__mm_dd_yyyy[0] topmostSubform[0].Page2[0].Employers_Business_or_Organization_Address_Street_Number_and_Name[0] and so on make it pretty… not straightforward to create a usable key/value combination to search and replace. But… today, I noticed it got…

Leave a Reply

Your email address will not be published. Required fields are marked *