php - 无法在 Laravel 中发送邮件

标签 php email laravel-5.7

我有一个表格,可以在其中获得 , 来自 , 主题 留言 作为输入。提交时,它会向指定的客户端( )发送一封电子邮件。

我创建了一个 可邮寄 并在 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/

相关文章:

javascript - 在 wordpress 主题中插入 javascript 背景

php - 为什么 Web 服务中的尾部斜杠如此重要?

php - 将json导入mysql

html - 在电子邮件中使用什么版本的 HTML?

php - laravel homestead 上的 sendmail 504 网关超时

php - Laravel 任意将退出状态码设置为自定义命令

php - 在 sql 结果中从特定用户中选择固定数量的记录

email - Spring Integration 异常时重新发送电子邮件

php - 您的要求无法解析为一组可安装的软件包。 - 拉维尔 5.7

laravel - 在 Eloquent 模型中使用时间字段