SymfonyCasts / reset-password-bundle

Need a killer reset password feature for your Symfony? Us too!
https://symfonycasts.com
MIT License
468 stars 65 forks source link

Emails not send if use MailerInterface in private method proccessSendingResetEmail #262

Closed pminetti closed 1 week ago

pminetti commented 1 year ago

Hi,

My MAILER_ DSN is smtp://user:pass@domain:port

When I use the original function processSendingPasswordResetEmail with MailerInterface, no email is sent, but if I change to TransportInterface, the email is sent.

I think the problem is that MailerInterface can handle async and sync modes. and by default is using async. As I can read in the documentation, if my dsn is smtp, the sync mode is enabled, so I don't need to configure nothing more.

I can't figure what I'm missing.

Thanks

Gregb342 commented 1 year ago

Hi !

I had the same issue here, can't figure why.

And I solve it with the help of your message @pminetti ! Thanks !

So sometimes, processSendingPasswordResetEmail is working better with TransportInterface than with MailerInterface.

Miguel-HM commented 1 week ago

Hey just wanted to say, it's a year later and I had the same exact problem and the same exact solution worked! Swap MailerInterface with TransportInterface and everything started working.

In fact, something weird happened, in case it helps pinpoint the issue here are the details:

Then I saw this issue, changed Mailer for Transport, tested and the email came through in seconds.

Hope the extra info helps, and thanks @pminetti for the unexplicable yet effective fix!

bocharsky-bw commented 1 week ago

This morning I saw an email from last night with my expired reset password link

So the email was actually sent by MailerInterface but it was sent with the delay? Am I understand it correct? Sounds like a possible misconfiguration of the Messenger. Do you use Messenger in your app?

I think the problem is that MailerInterface can handle async and sync modes. and by default is using async. As I can read in the documentation, if my dsn is smtp, the sync mode is enabled, so I don't need to configure nothing more.

@pminetti Could you please link to the docs where you see this?

bocharsky-bw commented 1 week ago

Hello guys! Thank you for taking the time to report it.

I did some research on this issue. Yes, that's true, if you're using MailerInterface - it indeed may add emails to the Messenger's queue instead of sending it immediately. And if you change to TransportInterface - it will send immediately. But in order to add the email to the queue - you need to have Messenger Component installed and configured in your app. Moreover, you have to have Messenger routing enabled for the SendEmailMessage to have it handled async, something like this:

framework:
    messenger:
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            'Symfony\Component\Mailer\Messenger\SendEmailMessage': async

If you don't have that SendEmailMessage: async - the email will not be sent via Messenger, i.e. it will be sent immediately anyway.

So, I believe this issue comes to your specific configuration. And if you're using Messenger in your app - I suppose you're aware of how it works and how to use it correctly. You just need to have the worker running in the background to consume the messages in the queue and send the emails. Even with handling emails via Messenger, you can achieve the email being sent in a few seconds, but of course, it depends on your website load, i.e. how many messages you have in the queue. But probably if you have a high-load project - you would want to send it async instead of sending it immediately anyway :)

So I'm closing this issue - IMO nothing to fix here and everything works as expected, that's just a user config problem. If you still have this issue but don't have Messenger installed in your app - than yes, it would be weird I think. But I can't reproduce it myself, so please, create a simple bug reproducer so that we could take a look at your specific case. Here's the explanation of how to create the reproducer: https://symfonycasts.com/blog/symfony-reproducer

Cheers!