I
I
Ivan2021-06-18 22:01:37
Laravel
Ivan, 2021-06-18 22:01:37

How to make sure that the provider for sending mail is selected depending on the APP_ENV key?

Good evening. Now the project is sent to different places either by a configured smtp server via Mail::to('[email protected]'), or to the right places via the sendgrid Mail::mailer('sendgrid')->to('[email protected]'). For the sandgrid, of course, there are the necessary keys in env. And so, it is necessary to transfer everything to sandgrid. Adding everywhere is mailer('sendgrid')->not quite suitable, since then sending to mailtrap does not work during local development and testing, since it needs to be there Mail::to. And sending real letters with a sandgrid during development is somehow undesirable. That's actually the question, how to make it so that Mail::towhen APP_ENV=productionsending goes through the sendgrid, but with local and develop configured smpt from mailtrap? Or can it be done in some other way?
You cannot add it everywhere before sending ifand check APP_ENVit.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pLavrenov, 2021-06-18
@Djonson86

Replace the driver line in the config with the condition with env()
In general, the mail driver is specified in the env file, and the .env files on the production server and the local one are different and the file should be excluded from the deployment, change the value on the server to the desired one.

V
Vladimir Kudrya, 2021-06-19
@Mugenzo

Well, offhand, you can make your own class that will check env and choose from it what to send the letter through

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question