【发布时间】:2020-01-04 08:29:56
【问题描述】:
我有一个表单,我可以在其中获得 to、from、subject 和 message 作为输入。提交时,它会向指定的客户(to)发送一封电子邮件。
我创建了一个 Mailable 并在控制器中执行类似的操作
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');
}
在我的 log 文件中,我收到类似
的错误字符串 {"exception":"[object] 不支持[] 运算符 (Symfony\Component\Debug\Exception\FatalThrowableError(代码:0): 字符串不支持 [] 运算符 C:\xampp\htdocs\shopvel_\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:582) ....
我不知道我哪里出错了。我尝试了很多解决方案,但没有奏效。例如,在 build 方法中删除 from 然后它显示
local.ERROR: Illegal string offset 'address' {"exception":"[object] (ErrorException(代码:0):非法字符串偏移“地址”在 C:\xampp\htdocs\shopvel_\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:318)
编辑 1:这是我的电子邮件视图
<!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()));
在构造方法中传递变量有限制吗?
(我不这么认为,因为构造函数可以接受任意数量的变量)
【问题讨论】:
-
在您的代码的第一行中,
request方法是一个集合,而不是一个数组,因此您可以像这样使用它$request->from而不是$request['from'] -
@Joseph 但
$request['from']和$request->from都返回类似support@website.com的字符串 -
可能是问题
return $this->from($address = $this->from, $name='Company Support')编辑为return $this->from($this->from, 'Company Support') -
已经试过了但是没用????
-
你能编辑你的帖子并添加你的邮件视图吗?!
标签: php email laravel-5.7