为了账号安全,请及时绑定邮箱和手机立即绑定

Swift_TransportException 无法与主机 :stream_socket

Swift_TransportException 无法与主机 :stream_socket

PHP
largeQ 2022-09-17 15:58:25
我正在尝试在用户注册拉拉维尔后发送电子邮件。但是当我试图发送电子邮件时,我得到以下错误。我已经在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<?phpnamespace 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_TransportExceptionConnection could not be established with host :stream_socket_client(): unable to connect to ssl://:0 (The requested address is not valid in its context. )
查看完整描述

2 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

首先验证文件中的值是否正确。特别是,请检查以下内容:.env


MAIL_DRIVER=smtp

MAIL_PORT=465

MAIL_ENCRYPTION=ssl

无论何时对文件进行更改,请务必运行.env


php artisan cache:clear && php artisan config:cache

因此,缓存的配置已更新,并且不使用旧值!


查看完整回答
反对 回复 2022-09-17
?
慕哥9229398

TA贡献1877条经验 获得超6个赞

首先验证通过请求发送的电子邮件(您应该验证所有内容)


public function storeUsers(Request $request)

{

    $this->validate($request, [

        'email' => 'required|email'

    ]);

    $user = new User;

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

        'rounds' => 12

    ]);

    $user = new User;

    $user->name = $request->user_name;

    $user->email = $request->email;

    $user->password = $hashedRandomPassword;

    $user->save();

    Mail::to($user->email)->send(new WelcomeMail($user));

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

}

并确保您不需要可能引发 SSL 错误的 TLS 连接


MAIL_ENCRYPTION=null

并公开用户属性,并通过依赖关系注入将其传递到构造函数


<?php


namespace App\Mail;


use App\User;

use Illuminate\Bus\Queueable;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;



class WelcomeMail extends Mailable

{

    use Queueable, SerializesModels;


    public $user;


    /**

     * Create a new message instance.

     *

     * @return void

     */

    public function __construct(User $user)

    {

        $this->user = $user;

    }


    /**

     * Build the message.

     *

     * @return $this

     */

    public function build()

    {

        // $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...... 

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

    }

}

并尝试您的邮件的日志驱动程序,以确保这不是互联网连接问题.env


MAIL_DRIVER=log

并清除配置缓存


php artisan config:clear

希望这有帮助


查看完整回答
反对 回复 2022-09-17
  • 2 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号