Answer the question
In order to leave comments, you need to log in
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>
<?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);
});
}
}
Answer the question
In order to leave comments, you need to log in
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question