{"id":30,"date":"2020-01-16T15:16:00","date_gmt":"2020-01-16T20:16:00","guid":{"rendered":"https:\/\/kimsal.com\/blog\/?p=30"},"modified":"2020-01-16T15:16:00","modified_gmt":"2020-01-16T20:16:00","slug":"laravel-is-sending-duplicate-emails","status":"publish","type":"post","link":"https:\/\/kimsal.com\/blog\/2020\/01\/16\/laravel-is-sending-duplicate-emails\/","title":{"rendered":"Laravel is sending duplicate emails!"},"content":{"rendered":"\n<p>So\u2026 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\u2019t specifically see it mentioned in the docs. So.. here goes.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Laravel 5.7 and above, IIRC\n\/\/ haven't checked on older versions\n$mailable = new MyMailableClass($data);\nMail::to(\"s@kimsal.com\")->send($mailable);\nMail::to(\"j@kimsal.com\")->send($mailable);<\/pre>\n\n\n\n<p>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?<\/p>\n\n\n\n<p>Behind the facade, the $mailable class has a \u2018to\u2019 list of recipients, and every call to Mail::to will *add* to the mailable\u2019s 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\u2019ve seen about \u201cduplicate email\u201d behavior. Certainly there can be other causes, but I\u2019ve hit this one personally, and know it was a bugger to figure out.<\/p>\n\n\n\n<p>The fix? I only know of two fixes.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>If you\u2019re 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\u2019 addresses. In some cases, this won\u2019t matter. In others, it may be a big privacy issue.<\/li><li>You can also just create a new mailable each time.<\/li><\/ol>\n\n\n\n<p>I help this helps someone <\/p>\n","protected":false},"excerpt":{"rendered":"<p>So\u2026 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\u2019t specifically see it mentioned in the docs. So&#8230;..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":0,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"wp:attachment":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}