Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 752
sid heart Avatar asked Oct 06 '17 12:10

sid heart


3 Answers

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
like image 80
Sonal Kumawat Avatar answered Oct 23 '22 17:10

Sonal Kumawat


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

like image 37
sid heart Avatar answered Oct 23 '22 18:10

sid heart


$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.

like image 15
marijnz0r Avatar answered Oct 23 '22 17:10

marijnz0r