Search code examples
phpemaillaravellaravel-4swiftmailer

Laravel mail: pass string instead of view


I want to send a confirmation e-mail using laravel. The laravel Mail::send() function only seems to accept a path to a file on the system. The problem is that my mailtemplates are stored in the database and not in a file on the system.

How can I pass plain content to the email?

Example:

$content = "Hi,welcome user!";

Mail::send($content,$data,function(){});

Solution

  • update on 7/20/2022: For more current versions of Laravel, the setBody() method in the Mail::send() example below has been replaced with the text() or html() methods.

    update: In Laravel 5 you can use raw instead:

    Mail::raw('Hi, welcome user!', function ($message) {
      $message->to(..)
        ->subject(..);
    });
    

    This is how you do it:

    Mail::send([], [], function ($message) {
      $message->to(..)
        ->subject(..)
        // here comes what you want
        ->setBody('Hi, welcome user!'); // assuming text/plain
        // or:
        ->setBody('<h1>Hi, welcome user!</h1>', 'text/html'); // for HTML rich messages
    });