public function build(){ return $this->from('from_email@example.com', 'Some-Name')->subject('Mail Subject')->view('view_name'); }
use App\Mail\MailableClass;
use Illuminate\Support\Facades\Mail;
//...
Mail::to('to_mail@example.com')->send(new MailableClass($data));
Puteti adauga recipiente "to", "cc", si "bcc" intr-o singura apelare inlantuita:
Mail::to('to_mail@example.com') ->cc('other_mail@example.com') ->bcc('another_mail@example.com') ->send(new MailableClass($data));
public function build(){ return $this->view('view_name'); }- "view_name" e numele view-ului cu template-ul pentru continutul email-ului.
<h1>Hi, {{ $name }}</h1> <p>This is email content, sent with Laravel.</p>Pentru a trimite un email cu text-simplu, se foloseste metoda text() in build():
public function build(){ return $this->text('view_name'); }
- Prin Proprietati ale clasei
Orice proprietate "public" definita in clasa mailable va fi automat valabila in view. Astfel puteti transmite date la constructor-ul clasei si sa le adaugati in proprietati publice ale clasei:class ClassName extends Mailable { public $data; //array with data for mail content ['name'=>'abc', ...] public function __construct($data){ $this->data = $data; } public function build(){ return $this->view('view_name'); } }Dupa ce datele sunt retinute in proprietati publice, pot fi preluate direct in view, prin numele proprietatii:
<h1>Hi, {{ $data['name'] }}</h1> <p>What appears and disappears does not really exist.</p>
- Prin metoda with()
Un alt mod de a trimite date variabile la view e prin folosirea metodei with() in build().class ClassName extends Mailable { protected $data; //array with data for mail content ['name'=>'abc', ...] public function __construct($data){ $this->data = $data; } public function build(){ return $this->view('view_name')->with(['name'=>$this->data['name']); } }Apoi, in view:
<h1>Hi, {{ $name }}</h1> <p> What a peaceful happy day.</p>
public function build(){ return $this->view('view_name')->attach('/path/to/file'); }
Email content. Here is an image: <img src="{{ $message->embed($path_to_file) }}">
MAIL_DRIVER = smtp MAIL_HOST = smtp.gmail.com MAIL_PORT = 587 MAIL_USERNAME = gmail_username@gmail.com MAIL_PASSWORD = gmail_password MAIL_ENCRYPTION = tls2. Dupa ce ati adaugat datele in fisierul .env, executi comanda urmatoare in Command-Line-Interface pt. a reseta cache-ul.
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class SendMail extends Mailable { use Queueable, SerializesModels; protected $data =''; //data with ['etype'=>'email-type', 'name'=>'name-added-in-view'] //Create a new message instance //Receives array public function __construct($data){ $this->data = $data; } //Build email message //return $this public function build(){ $data =['name'=>$this->data['name']]; //data to pass to view //builds email according to $email value if($this->data['etype'] =='text'){ return $this->subject('Laravel Basic Text Mail')->text('mails.mail')->with($data); } else if($this->data['etype'] =='html'){ //as html, with specified from-data return $this->from('from_mail@gmail.com', 'No-One')->subject('HTML Testing Mail')->view('mails.mail')->with($data); } else if($this->data['etype'] =='attachment'){ //image address to be embed in mail template $data['img'] ='https://coursesweb.net/php-mysql/laravel/laravel.png'; return $this->subject('Testing Mail with Attachment')->view('mails.mail') ->attach('https://coursesweb.net/blog/dwl/prayer_the_art_of_believing.pdf') ->with($data); } } }5. Creem un controler numit MailController, care sa afiseze o pagina pt. trimitere email.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Mail\SendMail; use Illuminate\Support\Facades\Mail; class MailController extends Controller { //responds to GET /sendmail //return view public function index(){ return view('mails.index'); } //responds to POST /sendmail //return void|string public function sendEmail(Request $request){ //if 'etype' field received, and its value is: text, html, or attachment if($request->has('etype') && in_array($request->etype, ['text', 'html', 'attachment'])){ $data =['etype'=>$request->etype, 'name'=>'SomeOne']; Mail::to('to_mail@example.com')->send(new SendMail($data)); return 'Email Sent. Check your inbox'; } else return 'Not Valid Request'; } }6. Creati un direct "mails" in directory resources/views/.
<!doctype html> <html lang="{{app()->getLocale()}}"> <head> <meta charset="utf-8"> <title>Laravel E-Mail</title> </head> <body> <h1>Send Emails</h1> <form action='{{route("sendmail")}}' method='post' onsubmit='this["submit"].outerHTML="Sending the Email"'> {{ csrf_field() }} <strong>E-mail type:<strong> <select name='etype'> <option value='text'>As plain text</option> <option value='html'>As HTML</option> <option value='attachment'>With Attachment</option> </select><br> <input type='submit' id='submit' value='Send' /> </form> </body> </html>7. Acum creati template-ul pentru continutul din email.
<h1>Hi, {{ $name }}</h1> <p>Sending Mail from Laravel.</p> @isset($img) <img src="{{ $message->embed($img) }}"> @endisset8. Pt. Rutele de accesare in browser, adaugati aceste linii de cod in routes/web.php:
Route::get('/sendmail','MailController@index'); Route::name('sendmail')->post('/sendmail','MailController@sendEmail');9. Accesati urmatoarea adresa in browser:
$when = Carbon\Carbon::now()->addMinutes(10); //to send after 10 minutes Mail::to('to_mail@example.com')->later($when, new MailableClass($data));
use Illuminate\Contracts\Queue\ShouldQueue; class ClassName extends Mailable implements ShouldQueue { // }
<ul> <li>http://coursesweb.net/html/</li> <li>http://www.marplo.net/html/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net
I`m living here. - Traiesc /Locuiesc aici.
Estoy viviendo aquĆ. - Traiesc /Locuiesc aici.