Laravel mail: pass string instead of view

不羁岁月 提交于 2019-11-28 04:52:30

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
});

For Html emails

Mail::send(array(), array(), function ($message) use ($html) {
  $message->to(..)
    ->subject(..)
    ->from(..)
    ->setBody($html, 'text/html');
});

The Mailer class passes a string to addContent which via various other methods calls views->make(). As a result passing a string of content directly won't work as it'll try and load a view by that name.

What you'll need to do is create a view which simply echos $content

// mail-template.php
<?php echo $content; ?>

And then insert your string into that view at runtime.

$content = "Hi,welcome user!";

$data = [
    'content' => $content
];

Mail::send('mail-template', $data, function() { });

If you were using mailables. You can do something like this in the build method :

public function build()
{

    return $this->view('email')
        ->with(['html'=>'This is the message']);
}

And you just go ahead and create the blade view email.blade.php in your resource folder.

Then in the blade you can reference your string using laravel blade syntax

 <html>
    <body>
      {{$html}}
    </body>
  </html>

or

 <html>
            <body>
              {!!$html!!}
            </body>
          </html>

If your raw text contains html mark up I hope this works for those who have templates stored in the database and wants to take advantage of the Mailables class in Laravel.

public function build()
{
    $message = 'Hi,welcome user!'
    return $this->html($message)->subject($message);
}

It is not directly related to the question, but for the ones that search for setting the plain text version of your email while keeping the custom HTML version, you can use this example :

Mail::raw([], function($message) {
    $message->from('contact@company.com', 'Company name');
    $message->to('johndoe@gmail.com');
    $message->subject('5% off all our website');
    $message->setBody( '<html><h1>5% off its awesome</h1><p>Go get it now !</p></html>', 'text/html' );
    $message->addPart("5% off its awesome\n\nGo get it now!", 'text/plain');
});

If you would ask "but why not set first argument as plain text ?", I made a test and it only takes the html part, ignoring the raw part.

If you need to use additional variable, the anonymous function will need you to use use() statement as following :

Mail::raw([], function($message) use($html, $plain, $to, $subject, $formEmail, $formName){
    $message->from($fromEmail, $fromName);
    $message->to($to);
    $message->subject($subject);
    $message->setBody($html, 'text/html' ); // dont miss the '<html></html>' or your spam score will increase !
    $message->addPart($plain, 'text/plain');
});

Hope it helps you folks.

To send raw html, text etc using Laravel Mailables you can

override Mailable->send() in your Mailable and in there, use the method in previous responses:

send([], [], function($message){ $message->setBody() } )

No need to call $this->view() at your build function at all.

as you know

Only mailables may be queued.

meaning, if you use ShouldQueue interface

1) first, you should always do

php artisan queue:restart

2) second, in your mailable you can use html method (tested in laravel 5.8)

public function build(): self
{
    return $this
            ->html('
                <html>
                    <body>
                        ForwardEmail
                    </body>
                </html>
            ')
            ->subject(config('app.name') . ' ' . 'email forwarded')
            ->attachData($this->content, 'email.eml', [
                'mime' => 'application/eml',
    ]);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!