首页主机资讯Laravel中怎么发送电子邮件

Laravel中怎么发送电子邮件

时间2024-05-29 01:44:04发布访客分类主机资讯浏览1521
导读:在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
PHP中怎么检查变量是否已设置 MySQL如何统计多张表的数据量

游客 回复需填写必要信息