Step by step of creating a text file and emailing in is within this folder which will be saved the created files .txt from some arraythe /questions/tagged/php .Step 2:Now is to create a text file (.txt) with some values of a array, example:$array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
$t = "";
foreach ($array as $i)
{
$t .= $i . PHP_EOL;
}
and then after to save in folder storage the text file do:\Illuminate\Support\Facades\Storage::disk('public')->put('400.txt', $t);
note that Facade Storage has one disk with the name public that is the configuration contained in app\config\filesystems.php'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
this setting is responsible for telling the storage location that is the same as that of link folder created storage, and don't worry that's already set up.The method put finally creates a text file with data from array which are now contained in the variable $t.Step 3:Enter the command line: php artisan make:mail MailTxtis created a class and configuration for sending the email, follows:<?php namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MailTxt extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this->view('email_txt')
->from('email@com.pt')
->attach(public_path('/storage/400.txt'));
}
}
that within the method build() we will set up the following methods:view: view name which is the main layout of your emailfrom: who is sending the email would be the email addressattach: the text file that was created earlier that will be attachedwith these 3 steps an email is sent with the corresponding text attachment.Full code:$array = [
1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20
];
$t = "";
foreach ($array as $i)
{
$t .= $i . PHP_EOL;
}
\Illuminate\Support\Facades\Storage::disk('public')->put('400.txt', $t);
\Illuminate\Support\Facades\Mail::to("parapua@sapo.pt")->send(new \App\Mail\MailTxt());
References https://laravel.com/docs/5.5/filesystem https://laravel.com/docs/5.3/mail