一、基本配置
// 代码演示 // 配置可以选择在config/mail.php中配置也可以选择在.env中配置,这里选择在.env中配置 MAIL_FROM_ADDRESS=555666@qq.com
MAIL_FROM_NAME=发件人称呼 MAIL_DRIVER=smtp MAIL_HOST=smtp.qq.com MAIL_PORT=25 MAIL_USERNAME=555666@qq.com MAIL_PASSWORD=zklafekkjkabbmlc MAIL_ENCRYPTION=null
二、邮件通知
1、创建通知
php artisan make:notification InvoicePaid
代码:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; class InvoicePaid extends Notification { use Queueable; /** * Create a new notification instance. * * @return void */ public function __construct() { // } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['mail']; } /** * Get the mail representation of the notification. * * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\MailMessage */ public function toMail($notifiable) { return (new MailMessage) ->line('The introduction to the notification.') ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } }
2、自定义接受者
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; /** * 邮件通道通知的路由。 * * @param \Illuminate\Notifications\Notification $notification * @return array|string */ public function routeNotificationForMail($notification) { // 只返回邮件地址... return "555.qq.com" } }
3、发送通知
public function send(User $user) { $user->notify(new InvoicePaid()); }
三、Markdown 邮件通知
1、修改上面toMail方法
public function toMail($notifiable) { return (new MailMessage)->markdown('mail.invoice.paid'); }
2、添加mail/invoce/paid.blade.php模板
@component('mail::message') # Introduction The body of your message. @component('mail::button', ['url' => '']) Button Text @endcomponent Thanks,<br> {{ config('app.name') }} @endcomponent