我有一个表格,可以在其中获得 至 , 来自 , 主题 和 留言 作为输入。提交时,它会向指定的客户端( 到 )发送一封电子邮件。
我创建了一个 可邮寄 并在 Controller 中做类似的事情
Mail::to($request['to'])->send(new SendMail($request['from'], $request['subject'], $request['message']));
这是我的邮寄
_constructor
public function __construct($from, $sub, $msg)
{
$this->from = $from;
$this->sub = $sub;
$this->msg = $msg;
}
这是构建方法
public function build()
{
return $this->from($address = $this->from, $name='Company Support')
->subject($this->sub)
->view('emails.sendmail');
}
在我的 日志文件我收到一个错误
[] operator not supported for strings {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): [] operator not supported for strings at C:\xampp\htdocs\shopvel_\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:582) ....
我不知道我哪里出错了。我尝试了很多解决方案,但没有奏效。例如,删除 来自 在 构建 方法然后它显示
local.ERROR: Illegal string offset 'address' {"exception":"[object] (ErrorException(code: 0): Illegal string offset 'address' at C:\xampp\htdocs\shopvel_\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:318)
编辑 1:这是我的电子邮件 View
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Mail</title>
</head>
<body>
{{ $msg }}
</body>
</html>
编辑 2:当我在
constructor
中传递两个变量时方法它工作正常Mail::to($request['to'])->send(new SendMail($request['from'], $request()->all()));
在构造函数方法中传递变量有什么限制吗??
(我不这么认为,因为构造函数可以接受任意数量的变量)
最佳答案
构造函数中的 $from 参数与 Mailable 类中的 $from 公共(public)属性冲突(并覆盖)。
public function __construct($from, $sub, $msg)
把它改成这样...
public function __construct($fromAddress, $sub, $msg)
它会正常工作
关于php - 无法在 Laravel 中发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57737684/