我试图在运行Laravel 5.4的旧网站上修改电子邮件模板
我最终确实打算至少更新到Laravel 5.5,甚至可能更新到Laravel 5.7-但我现在不想这样做,除非绝对必要(这将涉及对我的某些控制器和 很多 的额外测试)
我跑了
php artisan vendor:publish --tag=laravel-mail
此文件在resources/views/vendor/mail
然后我编辑了这些文件并尝试发送消息。没有变化。
然后我编辑了vendor/laravel/framework/src/Illuminate/Mail/resources/views/
中的文件并发送了一条消息-显示了新模板。
因此,尽管存在resources/views/vendor/mail
文件夹,但是Laravel在运行vendor/
之后仍从php artisan vendor:publish
文件夹中读取。我该如何解决?我在做什么错了?
一些其他信息,以备不时之需。这是我的邮件模板(resources/views/mail/email-a-friend.blade.php
):
@component('mail::message')
Your friend, {{ $senderName }}, has sent you information about a property they feel you might be interested in.
This property is listed by {{ config('app.name') }}. To view this property and more like it, please click the link below.
@if($agent->id !== $property->agent->id)
[{{ url($property->url()) }}?agent={{ $agent->first_name }}-{{ $agent->last_name }}]({{ url($property->url()) }}?agent={{ $agent->first_name }}-{{ $agent->last_name }})
@else
[{{ url($property->url()) }}]({{ url($property->url()) }})
@endif
@if($text != "")
They also sent this message:
@component('mail::panel')
{{ $text }}
@endcomponent
@endif
@endcomponent
这里是将电子邮件排队的控制器(app/http/Controllers/AjaxController.php
-仅是相关功能):
public function emailAFriend(Request $request)
{
$property = \App\Models\Property\Property::find($request->input('property-id'));
$agent = $property->agent;
if ($request->input('agent-id') !== $agent->id) {
$agent = \App\User::find($request->input('agent-id'));
}
Mail::to($request->input('send-to'))
->queue(new \App\Mail\EmailAFriend($property, $agent, $request->input('name'), $request->input('reply-to'), $request->input('text')));
return Response::json("success", 200);
}
这是可邮寄(app/Mail/EmailAFriend.php
):
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Property\Property;
use App\User;
class EmailAFriend extends Mailable
{
use Queueable, SerializesModels;
public $subject = "Someone sent you a property!";
public $property;
public $agent;
public $senderName;
public $senderEmail;
public $text;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Property $property, User $agent, $name, $email, $text)
{
$this->subject = "$name sent you information about a property";
$this->property = $property;
$this->agent = $agent;
$this->senderName = $name;
$this->senderEmail = $email;
$this->text = $text;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.email-a-friend')
->replyTo($this->senderEmail, $this->senderName)
->attachData(
$this->property->generatePdf(['agent' => $this->agent])->inline(),
"{$this->property->details->lot_size} acres in {$this->property->location->county} county.pdf",
[
'mime' => 'application/pdf'
]
);
}
}
出于测试目的,我正在使用sync
QueueDriver,因此它在发出AJAX请求后立即发送。在生产环境中,我使用database
QueueDriver。
组件:
resources/views/vendor/mail/html/message.blade.php
:
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
<img src="{{ url('/img/layout/logo.png') }}" alt="{{ config('app.name') }}" />
@endcomponent
@endslot
{{-- Body --}}
{{ $slot }}
{{-- Subcopy --}}
@if (isset($subcopy))
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endif
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. All rights reserved.
@endcomponent
@endslot
@endcomponent
resources/views/vendor/mail/markdown/message.blade.php
:
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
![{{ config('app.name') }}]({{ url('/img/layout/logo.png') }})
@endcomponent
@endslot
{{-- Body --}}
{{ $slot }}
{{-- Subcopy --}}
@if (isset($subcopy))
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endif
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. All rights reserved.
@endcomponent
@endslot
@endcomponent
这两个和默认组件(vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/message.blade.php
和markdown等效组件)之间的区别在标头中:
{{ config('app.name') }}
replaced with:
<img src="{{ url('/img/layout/logo.png') }}" alt="{{ config('app.name') }}" />
我正尝试用其徽标替换公司名称。当我进入vendor/laravel/framework/src/Illuminate/Mail/resources/views/markdown/message.blade.php
并直接编辑此文件时,我 做 会在生成的电子邮件中看到徽标。因此,尽管存在已发布的组件,但仍要从vendor/
目录中读取(并且编辑vendor/
目录是不好的,因为这样更改将不会在生产中持久存在)
答案 0 :(得分:0)
因此,在挖掘Laravel来源超过一个小时之后,我终于明白了。
componentPaths
变量中加载组件componentPaths
变量由loadComponentFrom()
设置loadComponentsFrom
在Markdown渲染器的构造函数中调用,并传递$options['paths']
知道这一点后,我开始研究“ Laravel markdown选项路径”并发现以下内容:https://stackoverflow.com/a/44264874/436976
我更新了config/mail.php
并添加了推荐的行,它运行良好!我觉得vendor:publish
应该为我完成了此操作,或者应该至少在official Laravel documentation中提到了此步骤,但是幸运的是我在一天之内就想出了这一点-总是很不错
事实证明,在Laravel官方文档中提到了这个 ,只是没有达到我的预期。
我的网站最初是一个Laravel 5.1网站,在上线之前先升级到5.2,再升级到5.3,然后最终又升级到5.4(我从未更新到5.5,因为一旦上线了,我想尽量减少对基础内容的更改框架)
在每次Laravel升级时,我都会继续从config/
目录中滚动旧文件,并且由于遵循的是很清楚的方法,显然在遵循升级指南方面做得很差:
https://laravel.com/docs/5.4/upgrade
新配置选项
为了提供对Laravel 5.4的新Markdown邮件组件的支持,您应该在邮件配置文件的底部添加以下配置块:
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
如果我按照指示更新了配置文件,我将永远不会遇到这些问题。