[英]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.