可邮寄渲染设置在3个成对的引号之间

时间:2019-06-02 17:49:41

标签: laravel laravel-5

我无法在视图上呈现Mailable。我可以清楚地看到(在望远镜中)我的帖子请求中有一个使用数组键“ preview”的会话,但是浏览器中没有任何显示。

在调试响应的值时,我发现我的视图数据的预览键设置为在另外两个引号(“”)之间的正确的字符串化渲染视图

例如:

[
  "preview": """<html></html>"""
]

这是我的代码:

查看: 在哪里检查$ preview是否已在控制器$res变量中定义。

    </div>
    @isset($preview)
        <hr>
        <div class="form-group row">
            <div class="col">
                <h4>Preview:</h4>
                <div class="form__preview">
                    {{ $preview }}
                </div>
            </div>
        </div>
    @endisset
</form>

控制器: 在我的操作中,我执行sendMail操作

public function mail(PotentialClientRequest $request)
{
    $sendMail = new PotentialClientAction();
    $res = $sendMail->execute($request);

    return back()->with($res);
}

操作: 在这里,我们返回视图或发送邮件(并返回成功消息)。

<?php

namespace App\Http\Actions;

use App\Mail\PotentialClientMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Http\FormRequest;

class PotentialClientAction
{
    public function execute(FormRequest $request): array
    {
        $contact = $this->getData($request);
        $mail = new PotentialClientMail($contact);
        $response = [];

        switch ($request->input('action')) {
            case 'mail':
                $response = $this->mail($mail);
                break;
            case 'preview':
                $response = $this->preview($mail);
                break;
            default:
                break;
        }

        return $response;
    }

    private function mail(PotentialClientMail $mail): array
    {
        Mail::to(env('MAIL_FROM_ADDRESS'))->queue($mail);

        return [
            'success' => 'Mail successfully sent!'
        ];
    }

    private function preview(PotentialClientMail $mail): array
    {
        return [
            'preview' => $mail->render()
        ];
    }

    private function getData(FormRequest $request): array
    {
        $data = $request->validated();
        $contact = [
            'email' => $data['email'] ?? '-',
            'email_cc' => $data['email_cc'] ?? '-',
            'subject' => $data['subject'] ?? '-',
            'body' => $data['body'] ?? '-'
        ];

        return $contact;
    }
}

可邮寄:

public function build()
{
    return $this
        ->subject($this->data['subject'])
        ->markdown('backend.mails.client')
        ->with([
            'email' => $this->data['email'],
            'email_cc' => $this->data['email_cc'],
            'subject' => $this->data['subject'],
            'body' => $this->data['body']
        ]);
}

邮件视图:

@component('mail::message')
<p>Hi Foo,</p>

@component('mail::panel')
<p>
<strong>My email is:</strong> <a href="mailto:{{ $email }}">{{ $email }}</a><br>
<strong>My email_cc is:</strong> <a href="mailto:{{ $email_cc }}">{{ $email_cc }}</a><br>
<strong>My subject is:</strong> {{ $subject }}<br>
<strong>It's about:</strong>
</p>
<p>{{ $body }}</p>
@endcomponent

@component('mail::button', ['url' => 'mailto:' . $email])
Reply
@endcomponent

Company name,<br>
{{ config('app.name') }}
@endcomponent

0 个答案:

没有答案