jeudi 4 octobre 2018

How can I use different emails on certain pages of a laravel app?

I need to configure certain forms within my Laravel App to be sent to different emails. I have the .env file configured to a default one and then in the controller for each page I set up the file to be sent to a different address. But when the user submits the form it always goes to the default address in the .env file.

Here is the example of .env code:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=default@example.com
MAIL_PASSWORD=example
MAIL_ENCRYPTION=ssl

Then I have a view for say job listings that I want to send to another email. In the controller for that file, I specified what email to send to like this: class ContactController extends Controller

{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function contact()
    {
        return view('pages/contact');
    }
    public function store(Request $request) {

        // dd($request->all());

        $this->validate($request, [
            'name' => 'required',
            'companyname' => 'required',
            'email' => 'required|email',
            'message' => 'required|min:10',
            'subject' => 'required|min:10' ]);



        Mail::send('emails.contact', [
            'subject'=>$request->subject,
            'name'=>$request->name,
            'company'=>$request->companyname,
            'msg'=>$request->message,
            'email'=>$request->email
        ], 


            function($contactMessage) use ($request){
            $contactMessage->from($request->email, $request->firstname);

            $contactMessage->to('nate@natescottdesign.com')->subject($request->subject);
        });

        // Session::flash('success', 'Your Email was Sent!');

        return redirect()->back()->with('success', 'Your email has been sent');
     }
} 

Is there a better way to handle this? I will have more than just these two emails to be used throughout the app.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire