在laravel中创建新用户帐户后无法发送邮件

时间:2019-04-13 12:07:28

标签: php laravel laravel-5

laravel的新用户,如果用户注册成功,它会尝试向用户发送邮件,并在电子邮件中包含一些详细信息

我的应用正在创建一个新帐户或新用户,但电子邮件未发送,它在“网络”标签中显示了此错误

message:    htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs\laravel Projects\bank\iscbank\resources\views\emails\welcome.blade.php)
exception:  ErrorException
file    C:\xampp\htdocs\laravel Projects\bank\iscbank\vendor\laravel\framework\src\Illuminate\Support\helpers.php

这是我的验证码或发送电子邮件

$datty = array(
            'name' => $request->input('name'), 
            'email' => $request->input('email'), 
            'Authenticationkey' => $this->genAutKey, 
            'password' => $this->genPass,
            'AccountNumber' => $this->AccNum,
        );

Mail::send('emails.welcome', $datty, function ($message){
                $message->from(Auth::user()->email, Auth::user()->name);    
                $message->to(Input::get('email'))->subject(Input::get('subject'));
            });

这是welcome.blade.php代码

<?php
    if (!function_exists('url')){
        function url(){
            if(isset($_SERVER['HTTPS'])){
                $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
            }
            else{
                $protocol = 'http';
            }
            return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        }
    }
?>
<div style="background-color: #eeeeef; padding: 50px 0; ">     
    <div style="max-width:640px; margin:0 auto; ">        
        <div style="color: #fff; text-align: center; background-color:#33333e; padding: 30px; border-top-left-radius: 3px; border-top-right-radius: 3px; margin: 0;">            
            <h1>Your account details</h1>        
        </div>        
        <div style="padding: 20px; background-color: rgb(255, 255, 255);">            
            <p style="color: rgb(85, 85, 85); font-size: 14px;"> 
                Hello {{$datty['name']}},<br>
                <br>An account has been created successfully.
            </p>            
            <p style="color: rgb(85, 85, 85); font-size: 14px;"> 
                Please use the following info to login your account:
            </p>            
            <hr>            
            <p style="color: rgb(85, 85, 85); font-size: 14px;">Dashboard URL:&nbsp;
                <a href="{{url()}}" target="_blank">{{url()}}</a>
            </p>            
            <p style="color: rgb(85, 85, 85); font-size: 14px;"></p>            
            <p >
                <span style="color: rgb(85, 85, 85); font-size: 14px; line-height: 20px;">
                    Email: {{$datty['$email']}}
                </span><br>
            </p>            
            <p>
                <span style="color: rgb(85, 85, 85); font-size: 14px; line-height: 20px;">
                    Password:&nbsp;{{$datty['password']}}
                </span>
            </p>          
            <p>
                <span style="color: rgb(85, 85, 85); font-size: 14px; line-height: 20px;">
                    Activation Key:&nbsp;{{$datty['Authenticationkey']}}
                </span>
            </p>            
            <p style="color: rgb(85, 85, 85);"><br></p>            
            <p style="color: rgb(85, 85, 85); font-size: 14px;">Thanks</p>        
        </div>    
    </div>
</div>

这是代码或WelcomeMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class WelcomeMail extends Mailable{
    use Queueable, SerializesModels;
    public $user;

    public function __construct($user){
        $this->user = $user;
    }

    public function build(){
        return $this->view('emails.welcome');
    }
}

请问我该如何解决这个问题,我尝试过在线搜索,但是他们都说相同的话,请

2 个答案:

答案 0 :(得分:0)

您正在通过邮件传递$datty,但没有使用它,您需要使用$datty。 以此更新您的代码。

Mail::send('emails.welcome', ['datty'=>$datty], function ($message) use($datty){
            $message->from(Auth::user()->email, Auth::user()->name);    
            $message->to(Input::get('email'))->subject(Input::get('subject'));
        });

答案 1 :(得分:0)

使用在邮件功能中使用的$ datty数组

Mail::send('emails.welcome', $datty, function ($message) use($datty){
        $message->from(Auth::user()->email, Auth::user()->name);    
        $message->to(Input::get('email'))->subject(Input::get('subject'));
    });

使用下面的代码更新welcome.blade.php文件。仅使用数组索引

<?php
   if (!function_exists('url')){
    function url(){
        if(isset($_SERVER['HTTPS'])){
            $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
        }
        else{
            $protocol = 'http';
        }
        return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }
}

<div style="background-color: #eeeeef; padding: 50px 0; ">     
<div style="max-width:640px; margin:0 auto; ">        
    <div style="color: #fff; text-align: center; background-color:#33333e; padding: 30px; border-top-left-radius: 3px; border-top-right-radius: 3px; margin: 0;">            
        <h1>Your account details</h1>        
    </div>        
    <div style="padding: 20px; background-color: rgb(255, 255, 255);">            
        <p style="color: rgb(85, 85, 85); font-size: 14px;"> 
            Hello {{ $name ]}},<br>
            <br>An account has been created successfully.
        </p>            
        <p style="color: rgb(85, 85, 85); font-size: 14px;"> 
            Please use the following info to login your account:
        </p>            
        <hr>            
        <p style="color: rgb(85, 85, 85); font-size: 14px;">Dashboard URL:&nbsp;
            <a href="{{url()}}" target="_blank">{{url()}}</a>
        </p>            
        <p style="color: rgb(85, 85, 85); font-size: 14px;"></p>            
        <p >
            <span style="color: rgb(85, 85, 85); font-size: 14px; line-height: 20px;">
                Email: {{ $email }}
            </span><br>
        </p>            
        <p>
            <span style="color: rgb(85, 85, 85); font-size: 14px; line-height: 20px;">
                Password:&nbsp;{{ $password }}
            </span>
        </p>          
        <p>
            <span style="color: rgb(85, 85, 85); font-size: 14px; line-height: 20px;">
                Activation Key:&nbsp;{{$Authenticationkey}}
            </span>
        </p>            
        <p style="color: rgb(85, 85, 85);"><br></p>            
        <p style="color: rgb(85, 85, 85); font-size: 14px;">Thanks</p>        
    </div>    
</div>