sid heart
sid heart

Reputation: 581

Object of class Illuminate\Mail\Message could not be converted to string

I am using Laravel 5.5 trying to send email but getting error

Object of class Illuminate\Mail\Message could not be converted to string

here is my controller

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');
}

here is my 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,
                    ]);
    }
}

my view file

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

I think I missing something because i did same on my old project and that is working fine.

Upvotes: 16

Views: 12277

Answers (4)

Anar Salimkhanov
Anar Salimkhanov

Reputation: 847

You can use laravel-log-dumper to dump any value and send it to log with the function ld().

Installation:

composer require spatie/laravel-log-dumper

Usage (according to your example):

$sentMail = \Mail::send('contact.contactMail', array(
        'name' => $input['name'],
        'email' => $input['email'],
        'phone' => $input['phone'],
        'subject' => $input['subject'],
        'form_message' => $input['message'],
        ...
    )
);

ld($sentMail);

More info about laravel-log-dumper: https://github.com/spatie/laravel-log-dumper

Upvotes: 0

Sonal Kumawat
Sonal Kumawat

Reputation: 21

Change the message input name in your contact controller

$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

to this -->

$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

Upvotes: 2

marijnz0r
marijnz0r

Reputation: 934

$message variable is not available in markdown messages.

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

I think this is because the $message represents the Illuminate\Mail\Mailable instance itself.

Upvotes: 17

sid heart
sid heart

Reputation: 581

i don't know why its a bug of laravel but $message is blacklisted for mail on view
when i tried {{ $message }} getting error and not sending the message
when i change $message to any name like {{ $content }} its working fine yeah its working fine now thanks for contribute all

Upvotes: 42

Related Questions