【问题标题】:Hardcoding email into Mailable Laravel将电子邮件硬编码到可邮寄的 Laravel
【发布时间】:2018-09-07 14:52:13
【问题描述】:

我有一个会员网站,我需要在用户注册时发送 2 封电子邮件。

  1. 欢迎电子邮件给新用户(这很好)
  2. 向管理员发送通知电子邮件,通知他们新用户。(需要帮助

我正在使用事件和侦听器来执行此操作。我目前可以将两封电子邮件都发送给新注册用户,所以我相信我的问题在于Mail::to function

这是当前工作的内容,但它正在发送给新注册的用户。

我需要它去 admin@mysite.com。如何在该电子邮件中硬编码,或从我的用户表中获取具有管理员权限的用户?

我可以这样做吗:

Mail::to($event->user->username('admin')->email)->send(new NewUserCreated($event->user));

上面给我一个错误:Call to undefined method Illuminate\Database\Query\Builder::username()

监听器: SendNewUserCreated.php

public function handle(NewUser $event)
{
    Mail::to($event->user->email)->send(new NewUserCreated($event->user));
}

邮件: NewUserCreated.php

public function build()
{
    return $this->subject('Custom Subject Here')->markdown('emails.staff.newusercreated');
}

Mail Blade: newusercreated.blade.php

@component('mail::message')
# A new user has registered on Website.

Please check that the following credentials match your records.

@component('mail::panel')
- **User Name:** {{$user->username}}
- **Full Name:** {{$user->first_name}} {{$user->last_name}}
- **Email:** {{$user->email}}
- **4 Digit Pin:** {{$user->pin}}
- **Street:** {{$user->street}}
- **City:** {{$user->city}}
- **State:** {{$user->state}}
- **Zip:** {{$user->zip}}
@endcomponent

@component('mail::button', ['url' => 'http://wesite.oo/admin'])
Grant Access
@endcomponent


Thank You,<br>
*{{ config('app.name') }}, Notifier*
@endcomponent

【问题讨论】:

    标签: php laravel email laravel-5


    【解决方案1】:

    不确定您的桌子是如何设置的,但类似这样:

    User::where('username', 'admin')->first()->email;
    User::where('is_admin', 1)->first()->email;
    

    或者使用配置或环境变量:

    config('myapp.admin_email');
    env('ADMIN_EMAIL');
    

    【讨论】:

    • User::where('username', 'admin')-&gt;first()-&gt;email; 替换侦听器中的Mail::to($event-&gt;user-&gt;email)-&gt;send(new NewUserCreated($event-&gt;user)); 吗?
    • 谢谢。这行得通! Mail::to(User::where('username', 'admin')-&gt;first()-&gt;email)-&gt;send(new NewUserCreated($event-&gt;user)); 还需要确保添加类use App\User;
    猜你喜欢
    • 1970-01-01
    • 2018-10-27
    • 2010-11-23
    • 1970-01-01
    • 2013-04-26
    • 2020-02-16
    • 2015-01-26
    • 2011-01-16
    • 1970-01-01
    相关资源
    最近更新 更多