我正在尝试修改一个旧网站上的电子邮件模板,运行Laravel5.4
我最终计划至少更新到Laravel 5.5,可能还会更新到Laravel 5.7 --但我现在不想这样做,除非严格来说是必要的(这将涉及对我的一些控制器进行一些重要的重写,以及一个lot的额外测试)。
我跑了:
php artisan vendor:publish --tag=laravel-mail这在resources/views/vendor/mail中创建了文件
然后我编辑了这些文件,并试图发送一条消息。没有变化。
然后我在vendor/laravel/framework/src/Illuminate/Mail/resources/views/中编辑了这些文件并发送了一条消息--新模板出现了。
因此,尽管存在resources/views/vendor/mail文件夹,但是在运行php artisan vendor:publish之后,Laravel仍然在读取vendor/文件夹。我该怎么解决这个问题?我做错了什么?
更新
一些额外的信息,以防有帮助。以下是我的邮件模板(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。
更新2
构成部分:
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
@endcomponentresources/views/vendor/mail/markdown/message.blade.php
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
 }})
@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和标记等价)之间的区别在标题中:
{{ 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并直接编辑这个文件时,我do在结果的电子邮件中看到了徽标。因此,尽管存在已发布的组件,但它仍在从vendor/目录中读取(编辑vendor/目录是不好的,因为这样的更改将不会在生产中持续存在)
发布于 2019-02-23 20:43:55
经过一个多小时的挖掘,我终于明白了这一点。
componentPaths变量加载组件componentPaths变量由loadComponentFrom()设置loadComponentsFrom并传递$options['paths']知道了这一点之后,我开始研究"Laravel选项路径“,并发现了以下内容:https://stackoverflow.com/a/44264874/436976
我更新了config/mail.php并添加了推荐的行,它运行得很好!我觉得vendor:publish应该为我做这件事,或者至少应该在官方Laravel文件中提到这个步骤,但幸运的是,我在一天之内就弄清楚了--所以这总是很好的。
说明(进一步研究)
事实证明,这个是在官方的Laravel文档中提到的,而不是我所期望的那样。
我的网站最初是一个Laravel5.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'),
],
],如果我按照指示更新我的配置文件,我就不会遇到这些问题。
https://stackoverflow.com/questions/54844711
复制相似问题