Send mail with fake data

Posted 1 month ago by plasmic

In my application I have a UI that allows an admin to modify the text content of the email templates. Each of these templates are tied to a mailable class. One of my requirements is to allow a preview of the template and also to send a test email of the template.

To show a preview of the template with data replacements, I use reflection to resolve instances of type hinted models in the mailable's constructor via faker. Doing this works fine with no problems.

I tried doing the same thing again to send a test email out but it's not working. My mailables implement the ShouldQueue interface so that they will be queued automatically in a production environment utilizing queues. The problem I'm facing is that even though I'm able to resolve the model's with faker for the mailable, I'm getting a no query results found for the model error since the model I'm sending to the mailable doesn't actually exist.

Removing the ShouldQueue interface fixes the problem but that is not desired. Even if I use the sendNow method when sending the mailable test in the controller, Laravel is still trying to query for the non-existent model for some reason. I'm not sure how I can get it to not do that while still allowing me to implement the ShouldQueue interface on my mailables.

Please sign in or create an account to participate in this conversation.