如何在新 laravel 9 项目中发送 smtp 电子邮件
how to send smtp email in fresh laravel 9 project
我只想通过我的 gmail 帐户发送邮件
我新创建了新鲜的 laravel 9 项目指导我通过简单的步骤发送邮件
我在 laravel 8 中可以做到,但在 9 中就不行了
谢谢大家
首先,您应该在 .env
中设置您的邮件配置
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
然后使用此命令 php artisan make:mail DemoMail
创建邮件 class
将您的 DemoMail Class 更新为此
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class DemoMail extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from Mansjoer@gmail.com')
->view('emails.demoMail');
}
}
现在使用此命令创建您的邮件控制器php artisan make:controller MailController
将其放入您的 MainController 文件
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
class MailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from Mansjoer.com',
'body' => 'This is for testing email using smtp.'
];
Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));
dd("Email is sent successfully.");
}
}
使用此代码创建您的路线
use App\Http\Controllers\MailController;
Route::get('send-mail', [MailController::class, 'index']);
然后让您的 MainController 视图
<!DOCTYPE html>
<html>
<head>
<title>Mansjoer Corporation</title>
</head>
<body>
<h1>{{ $mailData['title'] }}</h1>
<p>{{ $mailData['body'] }}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Thank you</p>
</body>
</html>
现在您可以尝试输入给定的 URL 并查看应用输出
http://localhost:8000/send-mail
我只想通过我的 gmail 帐户发送邮件 我新创建了新鲜的 laravel 9 项目指导我通过简单的步骤发送邮件
我在 laravel 8 中可以做到,但在 9 中就不行了
谢谢大家
首先,您应该在 .env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
然后使用此命令 php artisan make:mail DemoMail
将您的 DemoMail Class 更新为此
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class DemoMail extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from Mansjoer@gmail.com')
->view('emails.demoMail');
}
}
现在使用此命令创建您的邮件控制器php artisan make:controller MailController
将其放入您的 MainController 文件
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
class MailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from Mansjoer.com',
'body' => 'This is for testing email using smtp.'
];
Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));
dd("Email is sent successfully.");
}
}
使用此代码创建您的路线
use App\Http\Controllers\MailController;
Route::get('send-mail', [MailController::class, 'index']);
然后让您的 MainController 视图
<!DOCTYPE html>
<html>
<head>
<title>Mansjoer Corporation</title>
</head>
<body>
<h1>{{ $mailData['title'] }}</h1>
<p>{{ $mailData['body'] }}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Thank you</p>
</body>
</html>
现在您可以尝试输入给定的 URL 并查看应用输出
http://localhost:8000/send-mail