emchooo/laravel-flymail

使用自定义配置在Laravel中发送邮件,您可以在运行时设置这些配置

v1.0.1 2020-03-04 22:16 UTC

This package is auto-updated.

Last update: 2024-09-05 08:23:27 UTC


README

关于

Laravel Flymail 为 Laravel\Mail 添加了 Mail::config() 方法。配置方法可以在运行时设置邮件配置数据。如果您需要使用存储在数据库或其他配置中的动态凭证发送邮件,请使用 Flymail。由于它只是扩展了 Laravel 的 Mail 并添加了 config 方法,其他所有内容保持不变。

安装

composer require emchooo/laravel-flymail

用法

use Illuminate\Support\Facades\Mail;

// get config file from DB or other source

// SES config
$config = [ 
	'driver' => 'ses', 
	'key' => 'xxx', 
	'secret' => 'xxx', 
	'from' => [ 
		'address' => 'address@mail.com', 
		'name' => 'Name' 
		] 
	];
// SMTP config
$config = [
	'driver' => 'smtp',
	'host' => 'smtp.xxx.io', 
	'port' => 2525,
	'username' => 'xxx',
	'password' => 'xxx',
	'encription' => 'null'
	'from' => [ 
		'address' => 'address@mail.com', 
		'name' => 'Name' 
		]
	];

Mail::config($config)->to($request->user())->send(new OrderShipped($order));

关于队列的注意事项

如果您使用 Mail::config,则不要使用 ->queue();,使用 ->send();。如果您在 Mail 上使用队列,则它将在发送时使用 /config/mail.php 中的配置。也许有一天会尝试修复这个问题。

如果您需要使用自定义配置队列邮件,请使用作业。

	SendEmail::dispatch($config, $order);
	/**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($config, $order)
    {
        $this->config = $config;
        $this->order = $order;
    }
    
	 /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::config($this->config)
        ->to($this->order->email)
        ->send(new OrderShipped($this->order));
    }

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件