简体   繁体   中英

Laravel mail attachment issue

I haven't used Laravel's mail class before and i'm having a little trouble attaching files via a contact form. This is what I have:

public function mailer(){

    $fromEmail = Input::get('email');
    $fromName = Input::get('name');
    $phone = Input::get('phone');
    $subject = 'New Enquiry';
    $contactMessage = Input::get('message');
    $path = Input::get('files');

    $data = array('name'=>$fromName, 'email'=>$fromEmail, 'comment'=>$contactMessage, 'phone' => $phone);

    Mail::send('contactemail', $data, function($message) use ($fromEmail, $fromName, $subject, $path)
    {
        $message->to('stack-example@gmail.com', 'Enquiries');

        $message->from($fromEmail, $fromName);

        $message->subject($subject);
        $message->attach($path);
    });
}

It's throwing me the error:

fopen(stack-example-file.jpg): failed to open stream: No such file or directory

Mailer functions as it should without attachment. Any ideas as to where I am going wrong?

SOLVED

I had

Input::get('files');

instead of

Input::file('files');

Which resulted in a string rather than a file object. I had also moved from blade templating to vanilla html for the form tag and forgot to add

enctype="multipart/form-data"

to replace

'files' => true

File object was null as a result and therefore not attachable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM