【问题标题】:Can't send mail in Laravel无法在 Laravel 中发送邮件
【发布时间】:2020-01-04 08:29:56
【问题描述】:

我有一个表单,我可以在其中获得 tofromsubjectmessage 作为输入。提交时,它会向指定的客户(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-&gt;from 而不是$request['from']
  • @Joseph 但$request['from']$request-&gt;from 都返回类似support@website.com 的字符串
  • 可能是问题return $this-&gt;from($address = $this-&gt;from, $name='Company Support') 编辑为return $this-&gt;from($this-&gt;from, 'Company Support')
  • 已经试过了但是没用????
  • 你能编辑你的帖子并添加你的邮件视图吗?!

标签: php email laravel-5.7


【解决方案1】:

构造函数中的 $from 参数与 Mailable 类中的 $from 公共属性冲突(并覆盖)。

public function __construct($from, $sub, $msg)

把它改成这样...

public function __construct($fromAddress, $sub, $msg)

它会正常工作

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 2020-01-13
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多