我正在尝试使用Laravel中的smtp.mailtrap.io发送电子邮件。我按照Youtube教程操作,但一直收到以下错误信息:
PagesController
下面是我的代码:无法与主机smtp.mailtrap.io建立连接
PagesController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use Mail;
use Session;
class PagesController extends Controller {
public function getIndex() {
$posts = Post::orderBy('created_at', 'desc')->limit(4)->get();
return view('welcome')->withPosts($posts);
}
public function getAbout() {
$first = 'Niamh';
$last = 'Flannery';
$fullname = $first . " " . $last;
$email = 'niamh3516@hotmail.co.uk';
$data = [];
$data['email'] = $email;
$data['fullname'] = $fullname;
return view('pages.about')->withData($data);
}
public function getContact() {
return view('contact');
}
public function postContact(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min:10']);
$data = array(
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('niamh3516@hotmail.co.uk');
$message->subject($data['subject']);
});
Session::flash('success', 'Your Email was Sent!');
return redirect('/');
}
}
Contact.blade.php
<div class="row">
<div class="col-md-12">
<h1>Contact Me</h1>
<hr>
<form action="{{ url('contact') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label name="email">Email:</label>
<input id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label name="subject">Subject:</label>
<input id="subject" name="subject" class="form-control">
</div>
<div class="form-group">
<label name="message">Message:</label>
<textarea id="message" name="message" class="form-control">Type your message here...</textarea>
</div>
<input type="submit" value="Send Message" class="btn btn-success">
</form>
</div>
</div>
web.php
Route::get('contact', 'PagesController@getContact');
Route::post('contact', 'PagesController@postContact');
contact.php
<h3> You have a new contact via the contact form </h3>
<div>
{{$bodyMessage}}
</div>
<p> Sent Via {{$email}}</p>
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=c388d55897e620
MAIL_PASSWORD=f3d7fa90a9c6ab
MAIL_ENCRYPTION=null
port
,有些使用 SMTP 的 SSL,类似于465
,如果我没记错的话。 - ArtisticPhoenix