[英]Can't send mail in Laravel
我有一个表格中,我才能到,从,主题和消息作为输入。 在提交时,它会向指定的客户端 ( to ) 发送一封电子邮件。
我创建了一个Mailable并在控制器中做类似的事情
Mail::to($request['to'])->send(new SendMail($request['from'], $request['subject'], $request['message']));
这是我的 Mailable _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');
}
在我的日志文件中,我收到类似的错误
[] 运算符不支持字符串 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): [] 运算符不支持 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:这是我的电子邮件视图
<!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 function __construct($from, $sub, $msg)
把它改成这样...
public function __construct($fromAddress, $sub, $msg)
它会正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.