laravelyv
4 months ago

Sending Mail with attachments in Laravel 5.7

Posted 4 months ago by laravelyv

I am submitting a form and selecting the attachments from the list of files. In my controller I am sending the mail like below.

in my controller method

$to = $request->email; if ($to!="") { $objDemo = new \stdClass(); $objDemo->from = '[email protected]'; $objDemo->name = $request->name; $objDemo->company = $request->company; $objDemo->date = date('Y-m-d h:i:s'); $objDemo->position = $request->position; $objDemo->comments = $request->message; $objDemo->subject ='TypeFocus: Thank-you email'; $objDemo->action ='Write Thank You Mail'; $file_value = array(); if(isset($request->checkvals) && !empty($request->checkvals)) { $check_values = $request->checkvals; foreach ($check_values as $val) { $file_value[] = public_path() .'/user_files/'.$val; } } if(!empty($file_value)){ $objDemo->attach(implode(",",$file_value),['as' => 'display', 'mime' => $mime]); } Mail::to($to)->send(new Thankyou($objDemo));

Also in my app mail php I have created like this.

public function build() {

    return $this->view('emails.thankyou_email')
    ->with([

    ]);
   
}

resources/views/emails i have created the view like this.

@if($thankyou->action=='Write Thank You Mail')

 

NOTE: to reply to the sender of this email, you have to use their email address, not "[email protected]"

 

Name: {{$thankyou->name}}

Company: {{$thankyou->company}}

Position: {{$thankyou->position}}

Date:{{$thankyou->date}}

Message: {{$thankyou->comments}}

TypeFocus Careers Program.

@endif

Now only added the attachment feature in my controller method and I am getting an error - Call to undefined method stdClass::attach()

Can someone please help me to resolve the issue. If I remove the attachment code from the controller method the mail is working fine. I need to attach the selected files in my mail. Please help me.

Please sign in or create an account to participate in this conversation.