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('test@gmail.com')->subject('Welcome to laravel');

        $message->to('test8@gmail.com');

    });

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('test@gmail.com', 'Your Application');
    $message->to('test8@gmail.com', $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
  • hey thnku so much for ur rply bt i got solution Mail::send([], ['USER' =>'savi' ], function ($message) use ($template) { $message->from('savita.w3i@gmail.com')->subject('Welcome to ebiz'); $message->to('savitadhadwad08@gmail.com'); $message->setBody(htmlentities($template),'text/html'); }); where htmlentities($template) is data which is coming from database.now the problem is data contain html which is not rendenring properly.it is display as it is in mail – savita dhadwad Aug 23 '18 at 12:26
  • `$template` contains html? – Chukwuemeka Inya Aug 23 '18 at 12:40
  • $template=
    Hello {{!! USER !!}} Nice to meet you
    – savita dhadwad Aug 23 '18 at 12:55
  • ...and what are you getting in the email? You can drop a link. – Chukwuemeka Inya Aug 23 '18 at 13:01
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);
    });

}