Reputation: 15057
I want to implement a feature in my newsletter app for admin, so that when the admin opens a tab "Create New Email" he gets this page where he can make a new email or paste some text with styling from Word or whatever.
I don't know how to save that file in Laravel as a view like other pages inside views/emails folder in format *.blade.php, please help
Upvotes: 0
Views: 2743
Reputation: 328
It is better to save into database the input from CKEditor. This way you can edit / delete / create variants.
You can do it this way : 1. Create a form with CKEditor, the form has to use the POST method
<form method="POST" action="/admin/email/store">
<div class="form-group">
<textarea name="email_content" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<script>
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'editor1' );
</script>
</div>
The route to trigger the controller:
Route::post('/admin/email/store', 'EmailController@store');
The controller
public function store(Request $request) {
$email= new Email;
$email->email_content = $request->email_content;
$email->save();
return back();
}
In this example, I assume that you have created the Email model with at least a column 'email_content'. You can have other columns such as 'email_title' that you could populate from the form.
Upvotes: 0
Reputation: 324
I think you can do it better by saving that content to database and next send it using email template.
For example take a look:
Mail::send('emails.email-template', ['data' => $data], function ($m) use ($data) {
$m->from('[email protected]', $data['name']);
$m->replyTo($data['email'], $data['name']);
$m->to($data['to'])->subject($data['subject']);
});
And your email template:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
</head>
<body>
{!! $data['content'] !!}
</body>
</html>
Upvotes: 0
Reputation: 7152
You should be able to take the HTML generated from the form POST
in your controller and do something simple like:
file_put_contents(resource_path('views/emails/email.blade.php'), $request->html)
Obviously you'll need to adjust the above accordingly to reference just the html
portion.
There is nothing special going on to just name a file that is blade compatible.
If you need dynamic variable/placeholder support it's a little more complicated but still totally doable.
Upvotes: 1