代码之家  ›  专栏  ›  技术社区  ›  calin24

使用mailable在中设置地址和名称

  •  0
  • calin24  · 技术社区  · 7 年前

    在config/mail.php中有一个配置:

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],
    

    但我在.env中重写了

    MAIL_FROM_NAME=test@test.com
    MAIL_FROM_NAME='test user'
    

    当用mailable发送电子邮件时,在构建方法中我有:

    return $this->from($this->from)
                        ->subject($this->emailSubject)
                        ->view('email_template')
                        ->with('data', $this->data);
    

    为什么?

    *更新*

    php artisan config:cache
    php artisan config:clear
    

    现在起作用了。如果已在.env中重写,则不必使用from()方法

    MAIL_FROM_NAME=
    MAIL_FROM_NAME=
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   miken32 Amit D    7 年前

    你不能通过考试 second argument Mailable::from()

    但是,如果应用程序对所有电子邮件使用相同的“发件人”地址,那么调用 from 方法。相反,您可以在文件中指定一个全局“发件人”地址 config/mail.php 配置文件。如果在mailable类中未指定其他“发件人”地址,则将使用此地址:

    'from' => ['address' => 'example@example.com', 'name' => 'App Name'],

    https://laravel.com/docs/5.7/mail#generating-mailables

    换句话说,如果不需要每个环境的返回地址,只需直接在 配置/mail.php

        2
  •  3
  •   simonecosci    7 年前

    删除from方法,如果它与env或use中的不同

    return $this->from(config('mail.from.address'), config('mail.from.name'))
                    ->subject($this->emailSubject)
                    ->view('email_template')
                    ->with('data', $this->data);
    
    推荐文章