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));
You may set "to", "cc", and "bcc" recipients within a single, chained method call:
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" is the name of the view that contains the template for email content.
<h1>Hi, {{ $name }}</h1> <p>This is email content, sent with Laravel.</p>To send a plain-text email, use the text() method in build():
public function build(){ return $this->text('view_name'); }
- Via Public Properties
Any public property defined on your mailable class will automatically be made available to the view. So, you may pass data into your mailable class' constructor and set that data to public properties defined on the class: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'); } }Once the data has been set to a public property, it will automatically be available in your view:
<h1>Hi, {{ $data['name'] }}</h1> <p>What appears and disappears does not really exist.</p>
- Via the with() method
Another way to pass data into the view is to use the with() method 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']); } }Then, in your 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. After changing the .env file, execute the below command in Command-Line-Interface to clear the cache.
<?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/addons/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. We create a controller called MailController, to display the page and to send the 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. Create a folder called "mails" in resources/views/ directory.
<!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. Now we create the template for email content.
<h1>Hi, {{ $name }}</h1> <p>Sending Mail from Laravel.</p> @isset($img) <img src="{{ $message->embed($img) }}"> @endisset8. Add the following lines in routes/web.php:
Route::get('/sendmail','MailController@index'); Route::name('sendmail')->post('/sendmail','MailController@sendEmail');9. Access the following address in your browser:
$when = Carbon\Carbon::now()->addMinutes(10); 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://coursesweb.net/css/</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