简体   繁体   English

无法在 Laravel 中发送邮件

[英]Can't send mail in Laravel

I have a form in which I get to , from , subject and message as input.我有一个表格中,我才能主题消息作为输入。 On submit, it sends an email to the specified client ( to ).在提交时,它会向指定的客户端 ( to ) 发送一封电子邮件。

I created a Mailable and in controller doing something like我创建了一个Mailable并在控制器中做类似的事情

Mail::to($request['to'])->send(new SendMail($request['from'], $request['subject'], $request['message']));

Here is my Mailable _constructor这是我的 Mailable _constructor

public function __construct($from, $sub, $msg)
{
        $this->from = $from;
        $this->sub = $sub;
        $this->msg = $msg;
}

Here is the build method这是构建方法

 public function build()
 {
          return $this->from($address = $this->from, $name='Company Support')
                      ->subject($this->sub)
                      ->view('emails.sendmail');
 } 

In my log file I am getting an error like在我的日志文件中,我收到类似的错误

[] 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) .... [] 运算符不支持字符串 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): [] 运算符不支持 C:\\xampp\\htdocs\\shopvel_\\vendor 的字符串\\laravel\\framework\\src\\Illuminate\\Mail\\Mailable.php:582) ....

I can't figure out where I am going wrong.我不知道我哪里出错了。 I had tried many solutions but it didn't work.我尝试了很多解决方案,但没有奏效。 For example, removing from in build method then it shows例如,构建方法中删除然后它显示

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) 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)

EDIT 1: Here is my email view编辑 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> 

EDIT 2: When I pass two variables in the constructor method it is working fine like编辑 2:当我在constructor方法中传递两个变量时,它工作正常,就像

Mail::to($request['to'])->send(new SendMail($request['from'], $request()->all()));

Is there any limit for passing the variable in the constructor method??在构造函数方法中传递变量有什么限制吗??

(I don't think so since constructor can take any number of variables) (我不这么认为,因为构造函数可以接受任意数量的变量)

The $from parameter in your constructor is clashing with (and overwriting) the $from public property in the Mailable class.构造函数中的 $from 参数与 Mailable 类中的 $from 公共属性冲突(并覆盖)。

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

Change it to something like this...把它改成这样...

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

and it will work fine它会正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM