Laravel:如何使用sendmail发送电子邮件

时间:2020-07-04 07:48:50

标签: laravel laravel-7 laravel-mail

我正在使用Laravel 7,并且想通过Laravel Mail Facade使用 sendemail 驱动程序发送电子邮件,因为当我使用php邮件功能但我想使用Laravel Mail Facade时,它可以工作。 >

这是我的.env文件电子邮件配置

MAIL_DRIVER=sendmail
MAIL_SENDMAIL='/usr/sbin/sendmail -t -i'

这是我在config / mail.php中的默认邮件

'default' => env('MAIL_MAILER', 'sendmail'),
'mailers' => [
    'smtp' => [
        'transport' => 'smtp',
        'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
        'port' => env('MAIL_PORT', 587),
        'encryption' => env('MAIL_ENCRYPTION', 'tls'),
        'username' => env('MAIL_USERNAME'),
        'password' => env('MAIL_PASSWORD'),
    ],
    'ses' => [
        'transport' => 'ses',
    ],
    'sendmail' => [
        'transport' => 'sendmail',
        'path' => '/usr/sbin/sendmail -bs',
    ],
    'log' => [
        'transport' => 'log',
        'channel' => env('MAIL_LOG_CHANNEL'),
    ],
    'array' => [
        'transport' => 'array',
    ],
],

什么是正确的配置使其如何工作?

2 个答案:

答案 0 :(得分:1)

从您的问题中,我无法确切地看到您已经走了多远,所以我将尽力为您解释。

您需要使用Artisan创建电子邮件:

php artisan make:mail MailName

Laravel中的邮件基本上只是视图,因此在邮件的build()函数中,引用如下视图:

$this->view('folder.view');

您需要配置发送电子邮件的电子邮件地址,您可以在邮件文件中进行设置:

$this->from('youremail@domain.com');

或者您可以在mail.php文件中全局设置一个:

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

要发送邮件,请使用以下行:

use Illuminate\Support\Facades\Mail; // Put this at the top of your controller

Mail::to($recipient)->send(new MailName);

如果您在视图中使用变量,则需要将其传入,您可以这样操作:

use Illuminate\Support\Facades\Mail; // Put this at the top of your controller
    
Mail::to($recipient)->send(new MailName($variables));

有关CC,附件等其他信息,请参见the docs

答案 1 :(得分:0)

选中How to send email with Laravel using sendmail?,您将获得完整的主意。