G
G
GAS-ART2021-03-20 18:04:17
Laravel
GAS-ART, 2021-03-20 18:04:17

How to send a file to email in Laravel?

There is a feedback form.

<form action="{{ route('send') }}" method="post" enctype="multipart/form-data">
                    @csrf
                    <div class="form-group">
                        <label for="name">Ваше имя</label>
                        <input type="text" name="name" placeholder="Введите имя">
                    </div>

                    <div>
                        <label for="email">Ваша почта</label>
                        <input type="text" name="email" placeholder="Напишите вашу почту">
                    </div>

                    <div >
                        <label for="phone">Ваш номер телефона</label>
                        <input type="text" name="phone" placeholder="Напишите ваш номер телефона">
                    </div>
                    <div>
                        <label for="filename">Загрузить файл</label>
                        <input type="file" name="filename" class="form-control">
                    </div>
                    <button type="submit">Отправить</button>
                </form>

there is a controller:
<?php

namespace App\Http\Controllers;

use App\Contact;
use App\Mail\mailsendform;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use Mail;
use App\UserInfo;
use Illuminate\Support\Facades\Storage;

class ContactController extends Controller
{
   public function send (Request $req){
if ($req->isMethod('post') && $req->file('filename')) {
$file = $req->file('filename');
$upload_folder = 'public/mail_fils/';
$fileName = $file->getClientOriginalName(); // image.jpg
Storage::putFileAs($upload_folder, $file, $fileName);
}
/*ДАННЫЕ ИЗ ФОРМЫ*/
$firstname = $req->input('name');
$city = $req->input('city');
$email = $req->input('email');
$phone = $req->input('phone');
  
$upload_folder = 'C:\Program Files\Ampps\www\laravel\storage\app\public\mail_fils\\'; // АДРЕС ГДЕ ХРАНИТСЯ ФАЙЛ

   mail::send(['html' => 'mailtest'], ['firstname' => $firstname, 'city' => $city, 'email' => $email, 'phone' => $phone, ], function($message) use ($upload_folder, $fileName){
    $message->to('[email protected]', 'ART-GAS to')->subject('test email')->attachData($upload_folder, $fileName);
    });
  }
}

The data is being sent, sending to the mail is in progress, but the file comes with a size of 1kb. I tried to change the path to the file and put it wrong, but I got exactly the same situation. My assumption is that I am not correctly passing the path to the file to the program. But I can't figure out what it should be? If I insert this path in Windows Explorer, the file is located. Please tell me what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GAS-ART, 2021-03-20
@Gavr_Gavr

It turned out to solve this:
Changed the method
attachData($upload_folder, $fileName);
to the method:
attach($upload_folder)
In $upload_folder I wrote this:

$upload_folder = 'C:\Program Files\Ampps\www\laravel\storage\app\public\mail_fils\\' . $fileName;

P
part_os, 2021-03-20
@part_os

Most likely the path is not correct, first try to debug and check with the file_exists function, wrap it all in dd () to see what is there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question