【Laravel實戰】在 Laravel 應用內發信
如果想使用Laravel 內建的 Mail 方法寄送 Email,這邊以一般常見簡易的意見回饋表單為例子。
步驟1
在 routes\web.php 新增一個路由,該路由負責回傳 feedback 視圖
//routes\web.php
Route::get('/feedback', function () {
return view('feedback');
});
步驟2
在 App\Http\Controllers 資料夾內,建立子資料夾 Api ,裏頭建立一個 MailController
建立控制器的指令如下:
指令為:php artisan make:controller Api/MailController
內容如下:
//App\Http\Controllers\Api\MailController.php
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function sendMail(Request $request)
{
$data = $request->all();
//Laravel Mail 使用方法可參考:https://laravel.com/docs/5.1/mail#sending-mail
Mail::send('email.mail', $data, function ($message) use ($data) {
$message->from(env('MAIL_USERNAME', 'my@google.com'), $data['name']);
$message->to(env('MAIL_SUPPORT', 'my@google.com'))->subject('Feedback Mail');
});
return "success";
}
}
步驟3
在 routes\api.php 新增一個 Api 路由,該路由負責寄送 Email
//routes\api.php
Route::post('/sendmail', 'App\Http\Controllers\Api\MailController@sendMail');
步驟4
在 resources\views 資料夾內建立 Feedback 視圖的內容如下
//resources\views\feedback.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Feeback</title>
<!-- Bootstrap CDN-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Jquery CDN -->
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<!-- Font awesome CDN -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
<!-- CSS -->
<style>
.page{
display : inline-flex;
justify-content : center;
}
form{
width : 500px;
margin : 2rem 0;
display : flex;
flex-direction : column;
align-items : center;
}
input,textarea{
margin : 1rem 0;
}
.title{
color : gray;
font-size : 24px;
font-weight : bold;
}
.success{
width : 500px;
margin : 5rem 0;
text-align : center;
}
</style>
</head>
<body>
<div class = "container-fluid page">
<form class="form-group" id = "feedback">
<div class = "title">Feedback</div>
<input class = "form-control" type = "text" name = "name" placeholder = "Name"/>
<input class = "form-control" type = "text" name = "email" placeholder = "Email"/>
<textarea class = "form-control" name="content" cols="50" rows="10" placeholder = "Feeback"></textarea>
<button class="btn btn-primary">
Send
<i class="fa fa-spinner fa-spin"></i>
</button>
</form>
<div class = "success">Thank you for your feedback!</div>
</div>
</body>
<script>
$('.success').hide();
$('.fa-spin').hide();
$('#feedback').submit(function(event){
event.preventDefault();
$('.fa-spin').show();
$.post('{{ url("/api/sendmail") }}',$("#feedback").serialize(),function(data){
$('#feedback').hide();
$('.success').show();
})
})
</script>
</html>
步驟5
編輯 .env 環境變數如下:
//.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_SUPPORT=寄件信箱
MAIL_USERNAME=寄件信箱帳號
MAIL_PASSWORD=寄件信箱的應用密碼
MAIL_ENCRYPTION=tls
注意:因為範例使用 Gamil 所以 MAIL_HOST 的設定為 smtp.gmail.com
步驟6
接著要建立信件內容視圖,在resources\views下建立一個子資料夾 email ,並在裡面新增一個檔案,名為 mail.blade.php,內容如下:
//resources\views\email\mail.blade.php
<p>This is Feedback from {{ $name }}</p>
<p>Sender mail is : {{ $email }}</p>
<p>{{ $content }}</p>
步驟7
因為使用 Gmail Server 來發信的關係,需要修改 Mail 設定,開啟 config 資料夾的 mail.php ,在 smtp 區塊加入 'verify_peer' => false 如下所示:
//config\mail.php
'smtp' => [
...
'verify_peer' => false
],
步驟8
到 Google 帳號內去申請應用密碼