1

I m new to laravel.i have table email_template and want to send mail to user when user forgot password.i m fetching content dynamically from database but i dont know how to pass it to mail function in laravel.

Mail::send($posts['email_template'], ['USER' =>$post['user] ], function($message) {

        $message->from('[email protected]')->subject('Welcome to laravel');

        $message->to('[email protected]');

    });

where $posts['email_template'] is a content which i want to send and user is a variable which i want to replace in content

  • Possible duplicate of [Laravel mail: pass string instead of view](https://stackoverflow.com/questions/26139931/laravel-mail-pass-string-instead-of-view) – miken32 Oct 08 '19 at 18:28

2 Answers2

1
Mail::send('emails.template', ['user' => $user, 'data' => $data], function ($message) use ($user, $data) {
    $message->from('[email protected]', 'Your Application');
    $message->to('[email protected]', $user->name)->subject('Welcome to laravel');
});

emails.template is your view - template.blade.php file - /resources/views/emails/template.blade.php Now, in your view i.e emails.template, you can do:

{{ $user->name }}, {{ $data->address }}
Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23
0

You can Define ADMIN_EMAIL and CC_EMAIL in the constant file in the config folder

            $emailData = array(
                'name'=>'toName',
                'toEmail'=>$request->email
            );
            $this->sendEmail($emailData);

Email Function

function sendEmail($emailData){
    $this->adminEmail = config('constant.ADMIN_EMAIL');
    $this->ccEmail = config('constant.CC_EMAIL');
    $this->toEmail = $emailData['toEmail'];
    $this->emailTemplate = $emailData['emailTemplate'];
    $data['emailInfo'] = array(
        'name'=>$emailData['name']
    );
    Mail::send('emails.yourTemplate', $data, function ($message) {
        //$message->attach($pathToFile);
        $message->from($this->adminEmail, 'Laravel Email Test');
        $message->to($this->toEmail)->cc($this->ccEmail);
    });

}