猿问

Swift_TransportException 无法与主机 :stream_socket

我正在尝试在用户注册拉拉维尔后发送电子邮件。但是当我试图发送电子邮件时,我得到以下错误。我已经在env文件和配置文件/邮件.php文件中添加了所有配置。


以下是我在控制器中的代码


 public function storeUsers(Request $request)

    {


        $postdata = $request->all();

        $user = new User;

        $hashedRandomPassword = Hash::make('password', [

            'rounds' => 12

        ]);

       $user = new User;

       $user->name=$postdata['user_name'];

       $user->email=$postdata['email'];

       $user->password= $hashedRandomPassword;

       $user->save();

        Mail::to($postdata['email'])->send(new WelcomeMail($user));

       return back()->with('success','thanks for contacting us');

       exit(ok);

        //Mail::to($postdata['email'])->send(new WelcomeMail($user));

       // return $user;

       //return redirect('users');

    }

首先,我使用命令创建了邮件文件。以下是欢迎邮件中的代码.phpphp artisan make:mail WelcomeMail


<?php


namespace App\Mail;


use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;



class WelcomeMail extends Mailable

{

    use Queueable, SerializesModels;


    /**

     * Create a new message instance.

     *

     * @return void

     */

    public function __construct($user)

    {

        $this->user = $user;

    }


    /**

     * Build the message.

     *

     * @return $this

     */

    public function build()

    {

        //return $this->from('xavierissac94@gmail.com')->subject('new customer registration')->view('emails.welcome');

       return $this->view('emails.welcome')->with('data',$this->user);

    }

}

为什么我得到这个错误请帮我


我收到以下错误


Swift_TransportException

Connection could not be established with host :stream_socket_client(): unable to connect to ssl://:0 (The requested address is not valid in its context.

 )


largeQ
浏览 188回答 2
2回答

慕田峪9158850

首先验证文件中的值是否正确。特别是,请检查以下内容:.envMAIL_DRIVER=smtpMAIL_PORT=465MAIL_ENCRYPTION=ssl无论何时对文件进行更改,请务必运行.envphp artisan cache:clear && php artisan config:cache因此,缓存的配置已更新,并且不使用旧值!

慕哥9229398

首先验证通过请求发送的电子邮件(您应该验证所有内容)public function storeUsers(Request $request){&nbsp; &nbsp; $this->validate($request, [&nbsp; &nbsp; &nbsp; &nbsp; 'email' => 'required|email'&nbsp; &nbsp; ]);&nbsp; &nbsp; $user = new User;&nbsp; &nbsp; $hashedRandomPassword = Hash::make('password', [&nbsp; &nbsp; &nbsp; &nbsp; 'rounds' => 12&nbsp; &nbsp; ]);&nbsp; &nbsp; $user = new User;&nbsp; &nbsp; $user->name = $request->user_name;&nbsp; &nbsp; $user->email = $request->email;&nbsp; &nbsp; $user->password = $hashedRandomPassword;&nbsp; &nbsp; $user->save();&nbsp; &nbsp; Mail::to($user->email)->send(new WelcomeMail($user));&nbsp; &nbsp; return back()->with('success', 'thanks for contacting us');}并确保您不需要可能引发 SSL 错误的 TLS 连接MAIL_ENCRYPTION=null并公开用户属性,并通过依赖关系注入将其传递到构造函数<?phpnamespace App\Mail;use App\User;use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;class WelcomeMail extends Mailable{&nbsp; &nbsp; use Queueable, SerializesModels;&nbsp; &nbsp; public $user;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Create a new message instance.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct(User $user)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->user = $user;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Build the message.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return $this&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function build()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // $user object will be automatically passed to the view, so you might want to replace $data in your Blade view with $user and get rid of the ->with('data......&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return $this->view('emails.welcome')->with('data', $this->user);&nbsp; &nbsp; }}并尝试您的邮件的日志驱动程序,以确保这不是互联网连接问题.envMAIL_DRIVER=log并清除配置缓存php artisan config:clear希望这有帮助
随时随地看视频慕课网APP
我要回答