gpt4 book ai didi

php - Laravel 5.1 Mailer 试图获取非对象的属性

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:14 27 4
gpt4 key购买 nike

在 Mailer.php 第 33 行中获取 ErrorException:尝试获取非对象的属性

我的 Mailer.php:

<?php

namespace App\Mailers;

use Illuminate\Contracts\Mail\Mailer as Mail;

abstract class Mailer
{
/**
* @var Mail
*/
protected $mail;

/**
* @param Mail $mail
*/
public function __construct(Mail $mail)
{
$this->mail = $mail;
}

/**
* @param $to
* @param $subject
* @param $from
* @param $view
* @param null $data
*/
public function mailTo($to, $subject, $from, $view, $data = null)
{
$this->mail->send($view, $data, function($message) use ($to, $from, $subject)
{
$message->to($to->email)->subject($subject)->from($from);
});
}
}

我的 SiteMailer.php 扩展了我的 Mailer.php 抽象类

<?php

namespace App\Mailers;

class SiteMailer extends Mailer
{
/**
* @param $data
*/
public function sendEmailMessageToSupport($data)
{
$from = env('MAIL_NOREPLY', 'SUPPORT');
$to = env('MAIL_NOREPLY', 'SUPPORT');
$subject = 'Activate Your Account';
$view = 'auth.emails.support';

$this->mailTo($to, $subject, $from, $view, $data);
}
}

还有我的 SupportController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Mailers\SiteMailer;
use App\Http\Controllers\Controller;
use App\Http\Requests\SupportRequest;

class SupportController extends Controller
{
public function create()
{
return view('pages.support');
}

public function store(SupportRequest $request, SiteMailer $mail)
{
$mail->sendEmailMessageToSupport($request->all());

return redirect()->back()->with('alert-success', 'Thanks for contacting us!');
}
}

我的支持申请表申请

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class SupportRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'message_content' => 'required',
];
}
}

然后是邮件 View

<p>
A prospective customer named {{ $name }} <small>{{ $email }}</small>
has submitted an inquiry through Our Site.
</p>

<p>
{{ $message_content }}
</p>

找不到问题所在。

最佳答案

我建议问题出在这里:

 $message->to(**$to->email**)->subject($subject)->from($from);

$to 是一个电子邮件地址的字符串,由此判断:

 $to = env('MAIL_NOREPLY', 'SUPPORT');

所以简单地去掉 ->email 位,因为字符串没有属性/特性,例如:

 $message->to($to)->subject($subject)->from($from);

关于php - Laravel 5.1 Mailer 试图获取非对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31501812/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com