drccfl9407 2018-03-25 16:03
浏览 40

too long

I am trying to send a message to an email using smtp.mailtrap.io in Laravel. I have followed a youtube tutorial and keep getting the following error.

Connection could not be established with host smtp.mailtrap.io

I have attached my code below for this.

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
  • 写回答

2条回答 默认 最新

  • douyouchou1085 2018-03-25 20:16
    关注

    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)

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大