mardi 27 octobre 2020

Sending temporary attachment to email not working in Laravel 5.8

I'm trying to send attachment from form input via Notification email but I keep getting "Serialization of 'Illuminate\Http\UploadedFile' is not allowed" error.

This is my controller function:

/**
 * @param InterviewRequest $request
 * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
 */
public function interviewPost(InterviewRequest $request)
{
    $contactForm = $request->all();
    $contactForm['country_code'] = config('country.code');
    $contactForm['country_name'] = config('country.name');
    $contactForm = ArrayHelper::toObject($contactForm);

    if ($request->hasFile('filename')) {
        $contactForm->filename = $request->file('filename');
    }

    // Send Contact Email
        if (config('settings.app.email')) {
            Notification::route('mail', config('settings.app.email'))->notify(new InterviewSent($contactForm)); 
   }
}

This is my Notification function:

/**
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    $mailMessage = (new MailMessage)
        ->replyTo($this->msg->email, $this->msg->name)
        ->subject(trans('mail.contact_form_title', ['country' => $this->msg->country_name, 'appName' => config('app.name')]))
        ->line(t('Country') . ': <a href="' . lurl('/?d=' . $this->msg->country_code) . '">' . $this->msg->country_name . '</a>')
        ->line(t('Name') . ': ' . $this->msg->name);

    if (isset($this->msg->email) && $this->msg->email!='') {
        $mailMessage->line(t('Email Address') . ': ' . $this->msg->email);
    }

    if (isset($this->msg->phone) && $this->msg->phone!='') {
        $mailMessage->line(t('Phone Number') . ': ' . $this->msg->phone);
    }

    if (isset($this->msg->filename)  && (!empty($filename))) {
        return $mailMessage->attachData(
            $this->msg->filename->path(),
            $this->msg->filename->getClientOriginalName(),
            [
            'mime' => $this->msg->filename->getMimeType(),
        ]);
    }

    return $mailMessage;
}

I also tried to use the attach() method but it's still the same result.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire