Call to a member function getRealPath() on string when try to send an email

1.1k views Asked by At

I am try to sent an pdf with via mail in my vue frontend and laravel backend project I can send email successfully when there is no attach method but when I add attach part in my complainMail.php it gives me an error like

Call to a member function getRealPath() on string

here is my mail configuration part

MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=claimPaul1234
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

And this is my plotForm.vue file


  <v-form
    ref="form"
    lazy-validation
    v-on:submit.prevent='sendMail'
    enctype="multipart/form-data"
  >
     <v-text-field
    
    label="Name"
    outlined
    clearable
    prepend-icon="mdi-account"
    v-model="mail.name"
    ></v-text-field>

     <v-text-field
    
    label="email"
    outlined
    clearable
    prepend-icon="mdi-at"
    v-model="mail.email"
    ></v-text-field>

    <v-textarea
    label="Complain"
    auto-grow
    outlined
    rows="3"
    row-height="25"
    shaped
    prepend-icon="mdi-comment-quote"
    v-model="mail.complain"
    ></v-textarea>
    <input type="file"class="input-file" name='file' @change="getPdf($event)">

    <v-btn
      
      color="blue"
      class="mr-4"
      fab
    >
      <v-icon large @click="sendMail">mdi-send-circle</v-icon>
    </v-btn>

    
  </v-form>
</template>


<script>
import Axios from '../../../baseURL'
  export default {
    data: () => ({

      mail:{
        name:'',
        email:'',
        complain:'',
        file:''
      }
      
    }),

    methods: {
      getPdf(e)
      {
        var fileReader=new FileReader();

        fileReader.readAsDataURL(e.target.files[0]);

        fileReader.onload=(e)=>{

            this.mail.file=e.target.result;
        };

        console.log(this.mail.file);
      },

      sendMail(){

          Axios.post('send_mail',this.mail).then(()=>{

               

            }).catch(error=>{
                
                console.log(error.response.data.error);
          
            });

      }
      
    },
  }
</script>

mailController.php


namespace App\Http\Controllers;
use Illuminate\Support\Facades\Mail;
use App\Mail\complainMail;
use Illuminate\Http\Request;

class mailController extends Controller
{
    public function sendMail(Request $rs)
    {   
        $data=array(
            'name'=>$rs->name,
            'complain'=>$rs->complain,
            'file'=>$rs->file
        );
        Mail::to($rs->email)->send(new complainMail($data));
    }
}

complainMail.php

<?php

namespace App\Mail;

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

class complainMail extends Mailable
{
    use Queueable, SerializesModels;
    protected $data;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data=[])
    {
        $this->data=$data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('view_complain')->view('view_complain')->with('data',$this->data)
        ->attach($this->data['file']->getRealPath(),
    [
        'as'    => 'file' . $data['file']->getClientOriginalExtension(),
        'mime'  => $data['file']->getMimeType()        
    ]);
    }
}

Finally the blade file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email</title>
</head>
<body>
    <h1>you have new Complain from {{$data['name']}} </h1>
    <p>complain is <strong>{{$data['complain']}}</strong></p>
    <p>Thank you</p>
</body>
</html>

I was stuck and unable to what the matter of this code. I appreciate if some one can give me solution for this.

1

There are 1 answers

1
Kamlesh Paul On

you can call getRealPath() which is instance of Illuminate\Http\File class so you can do like this

use Illuminate\Http\File;
public function build()
{
   $file = new File($this->data['file']);

   return $this->subject('view_complain')->view('view_complain')->with('data', $this->data)
       ->attach(
           $file->getRealPath(),
           [
               'as'    => 'file' . $file->getClientOriginalExtension(),
               'mime'  => $file->getMimeType()
           ]
       );
}