Book Image

Getting Started with Laravel 4

By : Raphaël Saunier
Book Image

Getting Started with Laravel 4

By: Raphaël Saunier

Overview of this book

<p>PHP powers many of the largest websites on the planet. Yet, even though it was specifically created for the Web, its shortcomings never cease to frustrate developers. This is where a tool like Laravel comes in. Rather than reinventing the wheel, Laravel reuses tried and tested components and principles and bundles them to form a cohesive whole and makes PHP development enjoyable again.</p> <p>Getting Started with Laravel 4 is a practical and concise introduction to the Laravel PHP framework. It covers its fundamental concepts and presents the many features that will boost your productivity when developing web applications. After introducing the key concepts and installing Composer, you will build a CRUD application and add more features to it in each successive chapter.</p> <p>This book introduces you to a different and more enjoyable way of writing PHP applications. You will start by learning about the key principles and the same development practices that Laravel encourages. Then, in subsequent chapters, you will create and successively add more features to a web application.</p> <p>You will learn how to use the arsenal of tools at your disposal and probably pick up some useful techniques along the way. Indeed, everything you will learn in this book is highly transferrable and applicable to other MVC frameworks. Laravel's routing mechanism, templating language, and object-relational mapper will have no more secrets for you. You will learn how to authenticate users, write tests, and create command line utilities that interact with your application with disconcerting ease. In addition to this, you will probably be surprised by the simplicity and expressiveness of your code.</p>
Table of Contents (15 chapters)
Getting Started with Laravel 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sending e-mails


Laravel's Mail class extends the popular Swift Mailer package, which makes sending e-mails a breeze. The e-mail templates are loaded in the same way as views, which means that you can use the Blade syntax and inject data into your templates.

  • To inject some data into a template located inside app/views/email/view.blade.php, we use the following function:

    Mail::send('email.view', $data, function($message){});
  • To send both an HTML and a plain text version, we use the following function:

    Mail::send(array('html.view', 'text.view'), $data, $callback);
  • To delay the e-mail by 5 minutes (this requires a queue), we use the following function:

    Mail::later(5, 'email.view', $data, function($message){});

Inside the $callback closure that receives the message object, we can call the following methods to alter the message that is to be sent:

Some of the less common methods include:

To attach or embed files, you can use the following methods:

  • $message->attach('path/to/attachment.txt');

  • $message->embed(''path/to/attachment.jpg');

If you already have the data in memory, and you do not want to create additional files, you can use either the attachData or the embedData method:

  • $message->attachData($data, 'attachment.txt');

  • $message->embedData($data, 'attachment.jpg');

Embedding is generally done with image files, and you can use either the embed or the embedData method directly inside the body of a message as shown in the following code snippet:

<p>Product Screenshot:</p>
<p>{{$message->embed('screenshot.jpg')}}</p>