*發送email:
*申請mailgun:
https://www.mailgun.com
SMTP Hostname: smtp.mailgun.org
Port: 587 (recommended)
Username: postmaster@sandbox42524a1092a24a9e9359cca0db28d.mailgun.org
Default Password: d42ce872a1ed3273373a8ee76aa1d24d-acb0b40c-8f8f9
1.安裝套件:
composer require guzzlehttp/guzzle
2.設定.env檔案:
設定.env檔案後,要重新 php artisan serve
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbox42524a1092a24a9e9359cca0db28d.mailgun.org
MAIL_PASSWORD=d42ce872a1ed3273373a8ee76aa1d24d-acb0b40c-8f8f9
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=eric@eric.com
MAIL_FROM_NAME=eric
5.設計controller:
*方法1:
use Mail;
public function signUpProcess()
{
//寄送email: 純文字內容
Mail::raw('測試 Mailgun 寄信服務', function($message){
$message->from('eric@eric.com', 'eric');
$message->to('ericarc99@gmail.com');
$message->subject('測試 Mailgun');
});
}
參考:
laravel中文:
https://laravel.tw/docs/5.0/mail
*方法2:
use Mail;
public function signUpProcess()
{
//寄送email: html內容
// $input = request()->all();
$mail_binding = [
// 'nickname' => $input['nickname']
'nickname' => 'eric'
];
Mail::send('email.signUpEmailNotification', $mail_binding,
// function($mail) use ($input){
function($message){
// $message->to($input['email']);
$message->from('eric@eric.com', 'eric');
$message->to('ericarc99@gmail.com');
$message->subject('註冊成功');
});
}
view:
resources\views\email\signUpEmailNotification.blade.php
<h1>恭喜 {{ $nickname }} Shop Laravel 註冊成功</h1>
參考:
laravel中文:
https://laravel.tw/docs/5.0/mail
*方法3:
*Mailable:
php artisan make:mail Warning
app\Mail\Warning.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Warning extends Mailable
{
use Queueable, SerializesModels;
public $params;
/**
* Create a new message instance.
*
* @return void
*/
//讓外部可以把參數指定進來
public function __construct($paramsx)
{
$this->params = $paramsx;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
//透過with把參數指定給view
return $this->subject("警告訊息")
->view('email.warning')
->with([
'params' => $this->params,
]);
}
}
*view:
resources\views\email\warning.blade.php
<p>{{ $params['say'] }}</p>
*controller:
php artisan make:controller WarningController
app\Http\Controllers\WarningController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\Warning;
use Mail;
class WarningController extends Controller
{
public function send(){
//收件者務必使用 collect 指定二維陣列,每個項目務必包含 "name", "email"
// $to = collect([
// ['name' => 'eric', 'email' => 'ericarc99@gmail.com']
// ]);
$to = 'ericarc99@gmail.com';
//提供給view的參數
$params = [
'say' => '測試內容'
];
//內容顯示在瀏覽器(不送出email)
// echo (new Warning($params))->render(); die;
//送出email
Mail::to($to)->send(new Warning($params));
}
}
*route:
routes\web.php
//寄email
Route::get('/warning','WarningController@send');
參考:
laravel 5.7 發送 E-mail 方法:
php – Laravel – 透過 Gmail 發送 E-mail 信件
https://jsnwork.kiiuo.com/archives/2917/php-laravel-%E9%80%8F%E9%81%8E-gmail-%E7%99%BC%E9%80%81-e-mail-%E4%BF%A1%E4%BB%B6/
laravel:
https://laravel.com/docs/5.7/mail
----------------
*會員註冊controller:
app\Http\Controllers\UserAuthController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
Use Hash;
use App\Shop\Entity\User;
use Mail;
class UserAuthController extends Controller
{
/*
使用者註冊頁面 /user/auth/sign-up GET UserAuthController@signUpPage
使用者資料新增 /user/auth/sign-up POST UserAuthController@signUpProcess
使用者登入頁面 /user/auth/sign-in GET UserAuthController@signInpage
使用者登入處理 /user/auth/sign-in POST UserAuthController@signInProcess
使用者登出 /user/auth/sign-out GET UserAuthController@signOut
*/
public function signUpPage(){
$binding = [
'title' => trans('shop.auth.sign-up'),
];
return view('auth.signUp', $binding);
}
//http://127.0.0.1:8000/user/auth/sign-up
public function signUpProcess()
{
$input = request()->all();
$rules = [
'nickname' => [
'required',
'max:50',
],
'email' => [
'required',
'max:150',
'email',
],
'password' => [
'required',
'same:password_confirmation',
'min:6',
],
'password_confirmation' => [
'required',
'min:6',
],
'type' => [
'required',
'in:G,A',
],
];
$validator = Validator::make($input, $rules);
if($validator->fails()){
return redirect('/user/auth/sign-up')
->withErrors($validator)
->withInput();
}
$input['password'] = Hash::make($input['password']);
$Users = User::Create($input);
$mail_binding = [
'nickname' => $input['nickname'],
'email' => $input['email'],
];
//寄出email
// SendSignUpMailJob::dispatch($mail_binding)->onQueue('high');
//寄送email: 純文字內容
// Mail::raw('測試 Mailgun 寄信服務', function($message){
// $message->from('eric@eric.com', 'eric');
// $message->to('ericarc99@gmail.com');
// $message->subject('測試 Mailgun');
// });
//寄送email: html內容
$mail_binding = [
'nickname' => $input['nickname']
// 'nickname' => 'eric'
];
Mail::send('email.signUpEmailNotification', $mail_binding,
function($message) use ($input){
// function($message){
$message->from('eric@eric.com', 'eric');
$message->to($input['email']);
// $message->to('ericarc99@gmail.com');
$message->subject('註冊成功');
});
return redirect('/user/auth/sign-in');
}
public function signInPage()
{
}
public function signInProcess()
{
}
public function signOut()
{
}
}
沒有留言:
張貼留言