Laravel中怎么发送电子邮件
导读:在Laravel中发送电子邮件非常简单,首先需要配置你的邮件驱动和邮件服务器信息,可以在.env文件中设置相关配置信息,例如: MAIL_DRIVER=smtp MAIL_HOST=smtp.example.com MAIL_PORT=58...
在Laravel中发送电子邮件非常简单,首先需要配置你的邮件驱动和邮件服务器信息,可以在.env
文件中设置相关配置信息,例如:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your_email@example.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls
接下来,可以使用Laravel提供的Mail
facade 来发送电子邮件。首先创建一个邮件类,可以使用Artisan命令生成:
php artisan make:mail OrderShipped
在生成的OrderShipped
类中,可以设置邮件的内容和模板:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class OrderShipped extends Mailable
{
use Queueable, SerializesModels;
protected $order;
public function __construct($order)
{
$this->
order = $order;
}
public function build()
{
return $this->
view('emails.orders.shipped')
->
with([
'orderName' =>
$this->
order->
name,
'orderPrice' =>
$this->
order->
price,
]);
}
}
然后,在需要发送邮件的地方,可以使用Mail
facade 发送邮件:
use App\Mail\OrderShipped;
use Illuminate\Support\Facades\Mail;
$order = new stdClass();
$order->
name = 'Product A';
$order->
price = 100;
Mail::to('recipient@example.com')->
send(new OrderShipped($order));
以上代码将会发送一个包含订单信息的邮件给recipient@example.com
。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Laravel中怎么发送电子邮件
本文地址: https://pptw.com/jishu/670305.html