类Illuminate \ Mail \ Message的对象无法转换为字符串

时间:2017-10-06 12:01:59

标签: php laravel email swiftmailer laravel-5.5

我正在使用Laravel 5.5尝试发送电子邮件但收到错误

  

类Illuminate \ Mail \ Message的对象无法转换为字符串

这是我的控制器

public function contactreply($contact, Request $request){
    $reply = new Reply;
    $reply->subject = $request->subject;
    $reply->message = $request->message;
    $reply->email = $contact;
    $reply->save();
    $mail = Mail::to($contact)->send(new ContactReply($reply));
    return Redirect::back()->with('status', 'Email Sent Success');
}

这是我的ContactReply.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactReply extends Mailable
{
    use Queueable, SerializesModels;

    protected $reply;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($reply)
    {
       $this->reply = $reply; //dd($reply) passing all value here
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('admin.contact.reply')
                    ->subject($this->reply->subject)
                     ->with([
                        'message' => $this->reply->message,
                    ]);
    }
}

我的观看文件

<div>
  {!! Markdown::parse($message) !!}<!-- Using Markdown Package -->
</div>

我想我错过了一些东西,因为我在我的旧项目中做了同样的事情,并且工作正常。

3 个答案:

答案 0 :(得分:13)

我不知道为什么它是一个laravel的错误但是$ message被列入黑名单以便查看邮件
当我尝试{{ $message }}收到错误而不发送邮件时 当我将$ message更改为{{ $content }}之类的任何名称时,其工作正常 是的,它的工作正常,感谢所有贡献

答案 1 :(得分:7)

$message variable is not available in markdown messages.

来源:https://laravel.com/docs/5.6/mail#writing-mailables

我认为这是因为$ message代表了Illuminate \ Mail \ Mailable实例本身。

答案 2 :(得分:0)

更改联系人控制器中的消息输入名称

$input = $request->all();

    Contact::create($input);


     \Mail::send('contact.contactMail', array(
        'name' => $input['name'],
        'email' => $input['email'],
        'phone' => $input['phone'],
        'subject' => $input['subject'],
        'message' => $input['message'], // change in this line

到这里 -->

$input = $request->all();

    Contact::create($input);


     \Mail::send('contact.contactMail', array(
        'name' => $input['name'],
        'email' => $input['email'],
        'phone' => $input['phone'],
        'subject' => $input['subject'],
        'form_message' => $input['message'], //after change it will work