Laravel Mail: Pass String Instead of View

Laravel mail: pass string instead of view

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

How to send mail without including html in laravel?

Try this in your .blade.php file: (View)

{!! $name !!}

Instead of this : (This will render your HTML)

{{ $name }}

How do I send an email with Laravel 4 without using a view?

As mentioned in an answer on Laravel mail: pass string instead of view, you can do this (code copied verbatim from Jarek's answer):

Mail::send([], [], function ($message) {
$message->to(..)
->subject(..)
// here comes what you want
->setBody('Hi, welcome user!');
});

You can also use an empty view, by putting this into app/views/email/blank.blade.php

{{{ $msg }}}

And nothing else. Then you code

Mail::queue('email.blank', array('msg' => 'This is the body of my email'), function($message)
{
$message->to('foo@example.com', 'John Smith')->subject('This is my subject');
});

And this allows you to send custom blank emails from different parts of your application without having to create different views for each one.

How to send Plain text body in email in laravel 5.1?

You can use the raw method to send the plain text in mail.
Here is the example:

Mail::raw('This is the content of mail body', function ($message) {
$message->from('fromemail@gmail.com', 'Social Team');
$message->to('randomemail@gmail.com');
$message->subject('App - Forget Password');
});

how to send an email in my api function with laravel

You are using Mail::raw, so your message (i mean text) should be first argument. And you have to pass @email via use:

$msg = 'Un evement...';
Mail::raw($msg, function ($message) use ($email) {
$message->to($email);
$message->subject('Planning');
});

Data array not send in email view

I think I've understand the problem (there are two problems all in all) :

If data is passed to the view with an associative array :

$data = array('k1' => 'v1', 'k2' => 'v2')
Mail::queue('view.email', $data , function($message){...});

You should access the values in views with :

echo $k1;
echo $k2;

And there MUST NOT be any $message keys in the $data array because the closure's $message variable is also passed to the view.



Related Topics



Leave a reply



Submit