您好,我的这个项目与laravel一起使用Mailtrap发送电子邮件 这是我的sendemail控制器
<?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.');
}
}
}
和此功能可将批准的学生共享到StudentController中
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'),
],
这是我写教师电子邮件以发送电子邮件的表格
@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
这是我要发送的邮件的内容 这是(resources \ views \ SendEmail \ Request \ mail.blade.php)中的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设置了.env,如我在邮件陷阱中的帐户所示
答案 0 :(得分:0)
好的,让我们从路线开始。您为GET和POST请求指向相同的方法:
Route::post('/sendemail','SendemailController@start');
Route::get('/start','SendemailController@start');
结果,邮件字段Mail::to($request->email)
变为空。这可能是失败的原因。因此,请尝试使用其他方法来处理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.');
}