使用mailgun发送Email时,尝试获取非对象的属性

时间:2017-06-28 03:52:46

标签: php laravel

我正在使用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');
    }
}

这里是我的.env文件

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

这是我的路线

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

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

3 个答案:

答案 0 :(得分:1)

为什么要在线转换为数组?

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

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

即可
    $user = User::find(1);

或以下

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

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

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

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

            }); 

将数组内的用户参数作为Mail :: send()

中的第二个参数传递