What I'm trying to achive is that users can set their own smtp credentials (saved in the database).
Whenever a user sends an email, I'd like to use his/her preset smtp credentials for mailing.
I cannot find a solution as almost all the solutions out there suggest to have a service provider, where I set the mail config dynamically. The problem is, I cannot access the logged in user in a service provider: nor in the register method (auth facade is not found), nor in the boot method (Auth::user() gives null)
@tisuchi I think this is not correct as this would set the mail configuration only for the current (login) request and not for upcoming requests of the user.
Is this a scenario where you have a basic CMS where you manage your clients' credentials to send marketing emails on their behalf or something like that where only you have access to this system and that's why you don't need an authenticated user?
@justinjerez
No. This is a scenario, where users can send emails from within the application, via their gmail accounts.
So I basically just wanna set the smtp username and password dynamically.
I'm setting the remaining credentials (like host, port, encryption etc.) from .env
@kiralyta If is just a one-time thing that doesn't require login or saving those credentials in the database, let's say for testing purposes you can pass the credential in the request, but if you want to store them in the database, you'll need to identify those users and go with the solution from @tisuchi.
And in case is something enterprise related you'll have problem soon as Snapey says when you or the users find out that the emails are being blocked or mark as spam.
Note that you will have deliverability issues with this type of setup unless you also get the user to add an SPF record to their domain saying that your server is permitted to send on their behalf.
Obviously for cloud providers like outlook and gmail you will never be able to set custom spf records.
You may go to a lot of effort to get this setup, only to find that your users are complaining that many (not all) of the messages you send are never received by the end contact.
Example below of what happens when you try to send to a gmail account and don't have a valid SPF record;
<[email protected]>: host gmail-smtp-in.l.google.com[64.233.184.27] said:
550-5.7.26 This message does not pass authentication checks (SPF and DKIM
both 550-5.7.26 do not pass). SPF check for [shop.xxxxxxxxxxxx.co.uk]
does not 550-5.7.26 pass with ip: [159.65.85.119].To best protect our users
from spam, 550-5.7.26 the message has been blocked. Please visit 550-5.7.26
https://support.google.com/mail/answer/81126#authentication for more 550
5.7.26 information. m7-20020a05600c3b0700b003c6eefa0e99si8743231wms.206 -
gsmtp (in reply to end of DATA command)
@Snapey Alright, thank you for your clear answer. I've been testing it without dynamically adding the credentials, and I was getting all emails without any problem.
Also, it seems like a lot of people are doing it: see this.
Am I misunderstanding / miscommunicating something?
I'd like to make my users add their gmail username and app password data to their profile and use those to send emails with their credentials.
@kiralyta Remember that you can only get the 'authenticated user' within a web request. You cannot, for instance get auth()->user in a queued email for instance since the queue is run as a separate process.
The credentials are set correctly, but the smtp authentication fails. I think the problem is that the MailManager has already been initiated and changes the credentials in the config has no effect at this point.