laravel 使用mailgun发送电子邮件时尝试获取非对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44793369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:13:34  来源:igfitidea点击:

Trying to get property of non-object when send Email with mailgun

phplaravel

提问by Nguy?n Minh Huy

i'm doing send email with mailgun but get trouble. Here my controller

我正在用mailgun发送电子邮件,但遇到了麻烦。这是我的控制器

<?php

namespace App\Http\Controllers;

use Mail;
use App\User;
use App\Mail\TestMail;
use Illuminate\Http\Request;
use Auth;
use DB;
use Carbon\Carbon;

class EmailController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function mail()
    {
        $user = User::find(1)->toArray();
        Mail::send('emails.active', $user, function($message) use ($user) {
            $message->to($user->email);
            $message->subject('Mailgun Testing');
        });
        dd('Mail Send Successfully');
    }
}

here my .env file

这是我的 .env 文件

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbox3ae9e61e75da41cab7a649e0f06ac340.mailgun.org
MAIL_PASSWORD=xxxe90fdef17344b3505ab04d369xxx
MAIL_ENCRYPTION=tls

and here my route

这是我的路线

Route::get('/sendmail','EmailController@mail');

and i get trouble is Trying to get property of non-object EmailController.php line 29 ( $message->to($user->email); ) somebody help me and thanks!!

我遇到的麻烦是试图获取非对象 EmailController.php 第 29 行的属性( $message->to($user->email); )有人帮助我,谢谢!

采纳答案by Exprator

change this line if you are converting it toArray();

如果您要转换它,请更改此行 toArray();

$message->to($user->email);

to

$message->to($user['email']);

回答by Sagar Gautam

Why are you converting to array in line ?

你为什么要转换成数组?

        $user = User::find(1)->toArray();

toArray()converts $userto associative array. Just change this line to following

toArray()转换$user为关联数组。只需将此行更改为以下

    $user = User::find(1);

or following line

或以下行

        $message->to($user->email);

to

        $message->to($user['email']);

回答by Rohit Dalal

    $mail = \Mail::send('emails.active', array('receiver'=>$user,'sender'=>$sender), 
        function($message) use ($user)
            {

                $message->to($user->email, $user->name)->subject('Reset Password!');

            }); 

Pass user parameter inside an array as second parameter in Mail::send()

将数组内的用户参数作为 Mail::send() 中的第二个参数传递