In Mailer class there is no option for sending raw (string) mail as html. Suppose, that I want to get the body of the mail from a template stored in database, and it is html. I can't use raw as it will treat, as a text not html.
protected function addContent($message, $view, $plain, $raw, $data)
{
if (isset($view)) {
$message->setBody($this->getView($view, $data), 'text/html');
}
if (isset($plain)) {
$message->addPart($this->getView($plain, $data), 'text/plain');
}
if (isset($raw)) {
$message->addPart($raw, 'text/plain');
}
}
What you can do is just implement your own mailer. That's what I've done.
That's one of the reasons Laravel has contracts.
Ok, Thanks
Sending raw html is a headache in Laravel
The easiest way to send a raw html email is to use:
Mail::send('customrawtemplate', array('html' => 'your raw html goes here')...)
and create customrawtemplate.blade.php that just has:
{!! $html !!}
OK, the way I did it was:
Mail::send([], [], function($message) use ($data) {
$message->from($data['from']);
$message->to($data['to']);
$message->subject($data['subject']);
$message->setBody($data['content'], 'text/html');
});
This solved the issue of needing an empty template that was never going to be used.
Being stuck with blade make no sense --'
I send a number of csv/pdf/other generated file reports at regular intervals to web admins who don't care about anything in the email except for the attachment so I simply use Mail::raw('see attached', function($message) { ... })
for those instances. My only disappointment is you can't do this with a Mailable class.
thanks bro... code working fine.
https://github.com/laravel/framework/pull/22809
You can now use the html()
method on both Mailer and Mailable.
@abellion , how do we pass data to the html()
method?
for instance when I am building an email using view()
I can use the ->with([ 'data', => $data ])
on the end and I will have access to that data in the view..
is there a way to do this using the html()
method?
Note: using laravel version 5.6 currently...
@boldstar when using the html()
method the given _plain_ content doesn't pass through any template engine. I think you should use a view (i.e. a blade template) if you want to format its content.
@abellion , yeah I was trying to make templates that could be pulled from the database and be rendered with data. When I run the view()
(i.e. blade template) it looks for a view which is obviously not available since it is from the database and throws an alarm, unless maybe there is a way I can create views on the fly before passing it in to the view()
method?
Most helpful comment
OK, the way I did it was:
This solved the issue of needing an empty template that was never going to be used.