I'm using the Mail library in Laravel to send html email with custom data passed to a blade view.
The problem born when the mail has to render the html fetched from a row in the database which include a variable that i pass through the view.
This is my build function in my mailable class
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => $this->parameters->text,
]);
}
Then in the blade view:
<div>
{!! $text !!}
</div>
This is what the $text variable looks like:
<p>
<span>This is my text for the mail</span>
Click here to compile
</p>
The link href shoul contain the url variable value instead of not passing the variable name itself
A simple solution would be formating with php:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'text' => str_replace('{{ $url }}','https://google.com',$this->parameters->text)
]);
}
I did not try by myself but you could make an attempt with Blade::compileString(), i.e.:
public function build()
{
return $this->from('hello#test.it')
->view('view')
->with([
'url' => 'https://google.com',
'text' => \Blade::compileString($this->parameters->text),
]);
}
Related
I'm trying to send my validation errors to another method by using redirect
public function save()
{
//validasi input
if (!$this->validate([
'judul' => 'required|is_unique[komik.judul]'
])) {
return redirect()->to('/komik/create')->withInput();
}
this is the create() method
public function create()
{
session();
$data = [
'title' => 'Form Tambah Data Komik',
'validation' => \Config\Services::validation()
];
return view('komik/create', $data);
}
This is a snippet of my create.php view form where I'm trying to validate
<form action="/komik/save" method="post" enctype="multipart/form-data">
<?php d($validation) ?>
<?= $validation->listErrors();; ?>
this is the form
The problem is that the validation errors in save() is not sent to the create() method. But the validation errors exist in the save() method which I can prove by adding
$validation = \Config\Services::validation(); dd($validation);
in save(). This is what happens when I click "Tambah Data" button after I add the code
as you can see there is a validation error, it's just not sent to the create() method
I tried using return view(), this works but it creates another problem. I would like to use return redirect() instead.
This is my routes
$routes->get('/', 'Pages::index');
$routes->get('/komik/create', 'Komik::create');
$routes->get('/komik/edit/(:segment)', 'Komik::edit/$1');
$routes->post('/komik/save', 'Komik::save');
$routes->delete('/komik/(:num)', 'Komik::delete/$1');
$routes->get('/komik/(:any)', 'Komik::detail/$1');
What can I do to solve this problem? Thanks
A. Use Flashdata instead.
CodeIgniter supports “flashdata”, or session data that will only be
available for the next request, and is then automatically cleared.
I.e:
save() method.
public function save()
{
//validasi input
if (!$this->validate([
'judul' => 'required|is_unique[komik.judul]'
])) {
$validation = \Config\Services::validation();
return redirect()
->to('/komik/create')
->with("redirectedErrors", $validation->listErrors())
->with("redirectedInput", $this->request->getVar());
}
}
with()
Adds a key and message to the session as Flashdata.
public with(string $key, array<string|int, mixed>|string $message) : $this
B. Then, modify the create() method to pass on the flashed data to the View.
create() method.
public function create()
{
return view('komik/create', [
'title' => 'Form Tambah Data Komik',
'validation' => \Config\Services::validation(),
'redirectedErrors' => session()->getFlashdata("redirectedErrors") ?: "",
'redirectedInput' => session()->getFlashdata("redirectedInput") ?? [],
]);
}
C. Lastly, in your View file (create.php), you may access the passed-on data normally.
create.php View file.
<form action="/komik/save" method="post" enctype="multipart/form-data">
<?php echo empty($validation->getErrors()) ? $redirectedErrors : $validation->listErrors() ?>
<input id="first_name" name="first_name" type="text" value="<?php echo old('first_name', array_key_exists('first_name', $redirectedInput) ? $redirectedInput['first_name'] : '') ?>"/>
</form>
I found the solution, apparently there's a bug with redirect()->withInput() in CI. You need to use validation_errors(), validation_list_errors() and validation_show_error() to display validation error. Also you don't need to write $validation = \Config\Services::validation();.
Read https://codeigniter4.github.io/CodeIgniter4/installation/upgrade_430.html#redirect-withinput-and-validation-errors
I have some problems with my Laravel app.
I'm trying to send an email, but any time it sends it's not sending details that I need to pass to view.
I'm trying like (view)
Hello <strong>{{ $order['title'] }}</strong>,
<p>{{ $order['body'] }}</p>
But title and body are empty.
This is how controller looks like:
$order = [
'title' => 'title',
'body' => 'test body'
];
\Mail::to($user->email)->send(new OrderCreated($order));
And this is in mail
public $order;
public function build()
{
return $this->subject('Order Created')->view('emails.order');
}
What is wrong here?
Your problem is that you are not setting the $order property on your mailable.
You passing the order in when you do \Mail::to($user->email)->send(new OrderCreated($order)); so you just need to accept and set it in your mailable:
public function __construct($order)
{
$this->order = $order;
}
From there, the order will actually be accessible in your view as it is a public property.
I have two functions, the first one is working fine but the second one is not, in the second ones the variables appearing in the mail are {{$variableName}} unrendered.
I have checked as much other posts on SO as possible but no solution.
In the example below, in the first one the $userName correctly loads in the mail, whereas in the second one all variables show up as the variable name with the dollar sign.
I can confirm the $data variable is correct, and mails are even correctly sending using data from that variable.
public function workingMail($emailAddress,$userName) {
$message = '';
\Mail::send('workingtemplate', [
'userName' => $userName
], function ($message) use ($emailAddress) {
$message->from($this->fromAddress, $this->$this->fromAddressName);
$message->to($emailAddress);
$message->subject('Sample Subject');
});
}
public function notWorkingMail($data) {
$message = '';
foreach($data['namesAndAddress'] as $person) {
\Mail::send('notworkingtemplate', [
'name' => $person['name'],
'link' => $data['link'],
'fileName' => $data['fileName'],
'timeStamp' => $data['timeStamp'],
'action' => $data['action'],
'placeAddress' => $data['placeAddress']
], function ($message) use ($person,$data) {
$message->from($this->fromAddress, $this->fromAddressName);
$message->to($person['email']);
$message->subject($data['placeAddress'].' files have been updated.');
});
}
}
Thank you.
I had forgotten to add .blade to the name of the php file. It worked perfectly as soon as I did.
I want to use a variable value into another one (both are in the same table as keys) and display in a blade layout the container variable value. Both variables are passed using the with method from the controller. Is that possible?
Here a quick demo of what I'm loooking for :
# controller method
public function build()
{
return $this
->view('emails.registration_following')
->with( ['name' => 'Nick', 'text' => 'Hello {{ $name }}'] ) # Here goes the thing
;
}
And the blade.php looks like :
<html>
<body>
<p> {{ $text }} </p>
</body>
</html>
The expect output by me is Hello Nickbut I got Hello {{Nick}}. I know that brackets here, are treated like String but how to circumvine that?
How about defining the variable in the function first:
# controller method
public function build()
{
$name = 'Nick';
return $this
->view('emails.registration_following')
->with( ['name' => $name, 'text' => "Hello $name"] ) # Here goes the thing
;
}
In this way, you have both $name and $text accessible in the view.
In fact, I did not need to pass two variables but just one and put in its value all needed other variables to form my expected text.
It was a bad idea to do what I was asking for. Thank you.
Have u tried like this below?
public function build()
{
$name = 'Nick';
return $this
->view('emails.registration_following')
->with( ['name' => $name , 'text' => 'Hello '.$name] ) # Here goes the thing
;
}
with( ['name' => 'Nick', 'text' => "Hello $name"] ) Please use double quote
i want to send text not view by laravel mail , as i use simple form wth textarea to write the content of message , how to seend text or how to convert this text to view ....... thanks
public function sendmail(){
$sendmessage=Input::get('message');
//$messageView=View::make('messageview')->with('message',$sendmessage);
Mail::send($sendmessage, array('name'=>'hossam'),function($message){
$title=Input::get('title');
$mail=Input::get('mailsender');
$message->from($mail,'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($title);
You can create an email view where the only thing it sends is the contents of a variable:
app/views/emails/nonview.blade.php
{{ $contents }}
Then, you can use this view whenever you want to send an email that doesn't have a view:
public function sendmail() {
$from = Input::get('mailsender');
$subject = Input::get('title');
// 'contents' key in array matches variable name used in view
$data = array(
'contents' => Input::get('message')
);
Mail::send('emails.nonview', $data, function($message) use ($from, $subject) {
$message->from($from, 'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($subject);
});
}