无法与主机smtp.mailtrap.io建立连接

时间:2018-03-25 16:03:07

标签: php html laravel email

我正在尝试使用Laravel中的smtp.mailtrap.io向电子邮件发送消息。我已经按照youtube教程并继续收到以下错误。

  

无法与主机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

3 个答案:

答案 0 :(得分:0)

Use the following settings your .env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=c388d55897e620
MAIL_PASSWORD=f3d7fa90a9c6ab
MAIL_ENCRYPTION=tls

And in your config/mail.php add this at the bottom

'stream' => [
      'ssl' => [
          'allow_self_signed' => true,
          'verify_peer' => false,
          'verify_peer_name' => false,
      ],
    ],

and check if this is correct

'sendmail' => '/usr/sbin/sendmail -bs',

Make sure ssl and smtp are enabled and properly configured on your localhost(check your php.ini)

答案 1 :(得分:0)

就我而言,这是SELinux(CentOS 7)的问题,这有助于:

sudo semanage port -a -t http_port_t -p tcp 2525

如果仍然无法正常工作,请检查以下内容:

sudo setsebool -P httpd_can_sendmail 1
sudo setsebool -P httpd_can_network_connect 1

答案 2 :(得分:-1)

如果您正在Cpanel上运行项目,则尝试使用端口 465 作为邮件陷阱提供的其他端口的优先级,而在本地运行时则使用2525。