![](/img/trans.png)
[英]send a simple message to an email using laravel 4.2 and mailtrap
[英]Send email in laravel by using mailtrap
您好,這是我的項目 laravel 使用郵件陷阱發送 email 這是我的發送郵件 controller
<?php
namespace App\Http\Controllers;
use App\Model\Sendemail;
use Illuminate\Http\Request;
use Mail;
use App\Mail\TestStarted;
class SendemailController extends Controller
{
public function start(Request $request)
{
$send_email = Mail::to($request->email)->send(new TestStarted);
if ($send_email)
{
return redirect()->back()->with('success', 'Sens email
successfully.');
}
}
}
和這個 function 共享批准學生到學生控制器
public function shareapproval($uniid)
{
$approval = Student :: where ('uniid', $uniid)->firstOrFail();
return view('SendEmail.Request.share',compact('approval'));
}
和郵件文件中的這個 TestStarted.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestStarted extends Mailable
{
use Queueable, SerializesModels;
public function build()
{
return $this->view('SendEmail.Request.mail');
return redirect()->back()->with('success', 'Sens email successfully.');
}
}
這是在 config.mail.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'testgp2@system.com'),//
'name' => env('MAIL_FROM_NAME', 'Example'),
],
這是我寫教練 email 發送 email 的表格
@extends('layouts.app')
@section('content')
<div class="container">
<form method="post" action="/sendemail">
@csrf
<h1> send email </h1>
<br>
<<div class="form-group">
<label for="email">write the instructor email</label><br>
<input type="text" id="email" name="email" class="form-control" >
</div>
<button type="submit" class="btn btn-primary">send </button><br>
</form>
</div>
@endsection
這是我要發送的郵件的內容 這是 mail.blade.php in (resources\views\SendEmail\Request\mail.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-enguiv="X-UA-compatible" content="ie=edge">
<title>document </titel>
</head>
<body background-color: coral>
<h2> thank you for your order </h2>
</body
</html>
最后,這是我的路線
Route::post('/student/share-approval/{uniid}',
'StudentController@shareapproval');
//SendEmail
Route::post('/sendemail','SendemailController@start');
Route::get('/start','SendemailController@start');
我使用 MAIL_USERNAME 和 MAIL_PASSWORD 設置 my.env,如我在 mailtrap 上的帳戶中所示
好吧,讓我們從路線開始。 您為 GET 和 POST 請求指向相同的方法:
Route::post('/sendemail','SendemailController@start');
Route::get('/start','SendemailController@start');
結果,郵件字段Mail::to($request->email)
得到 null。 這可能是失敗的原因。 因此,請嘗試使用不同的方法來處理 GET 和 POST 請求,而不是使用一種方法。
Route::get('/start','SendemailController@start');
Route::post('/sendemail','SendemailController@sendMail');
其次,在下面的代碼中,您將返回兩次。 但在現實生活中,它只會執行第一個而忽略第二個。
public function build()
{
// this is executing
return $this->view('SendEmail.Request.mail');
// this is getting ingorned
return redirect()->back()->with('success', 'Sens email successfully.');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.