Truy cập vào link: https://myaccount.google.com/security, chọn 2-Step Verification, chọn tiếp App passwords
Khai báo cấu hình trong .env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=refactortop@gmail.com
MAIL_PASSWORD=cpgputhkveggwuet
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="refactortop@gmail.com"
php artisan make:mail MailableSender
class MailableSender extends Mailable{
public function __construct(public $subject, private $content, public $view){
}
public function envelope(): Envelope{
return new Envelope(
subject: $this->subject,
);
}
public function content(): Content
{
return new Content(
view: $this->view,
with: ['content' => $this->content, 'subject' => $this->subject]
);
}
}
function contact(Request $req){
if($req->isMethod('post')){
try{
Mail::to($req->post('email'))->send(new MailableSender($req->post('subject'), $req->post('content'), 'mail.index'));
dd('Success');
}catch(Exception $ex){
dd($ex);
}
}
return view('home.contact');
}